按类别显示帖子号

时间:2018-08-16 作者:julien c

我尝试按类别添加自动递增的帖子编号。像cat=“myID”>第1、2、3栏。。。

Display post number not post ID number

在找到这个解决方案时,使用post\\u meta看起来是一个很好的方法。但此解决方案仅适用于所有类别。我试着这样改变它,但它不起作用。

$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts 
             WHERE $wpdb->posts.post_status = \'publish\' 
             AND $wpdb->posts.post_type = \'post\' 
             AND $wpdb->posts.post_cat = \'35\'
             ORDER BY $wpdb->posts.post_date ASC"

2 个回复
最合适的回答,由SO网友:nmr 整理而成

将此代码添加到functions.php. 它应该为给定类别的帖子增加元价值
您可以使用硬编码的类别slug/ID,但最好将其存储在options 表,并允许从仪表板进行更改。

将“ID”/“category\\u slug”更改为您自己的。

/**
 * @param int     $post_ID      Post ID.
 * @param WP_Post $post_after   Post object following the update.
 * @param WP_Post $post_before  Post object before the update.
 */
function updateNumbers($post_id, $post_after, $post_before) {
    global $wpdb;

    if ( $post_after->post_type != \'post\' )
        return;

    $selected_cat_ID = 123; // <-- ID of your category with numbering

    // -- select category ID by slug --
    // $selected_cat = \'category_slug\';
    // $selected_cat_ID = get_terms([\'slug\' => \'category_slug\', \'fields\'=> \'ids\', \'taxonomy\' => \'category\', \'hide_empty\' => false,]);
    // if ( !is_array($selected_cat_ID) || empty($selected_cat_ID) ) 
    //     return; 
    // $selected_cat_ID = $selected_cat_ID[0];
    // -- end: select category ID by slug --

    $incr_number = get_post_meta($post_id, \'incr_number\', true);

    //
    // get posts assigned to your category
    $query = "SELECT p.id FROM {$wpdb->posts} p"
                . " LEFT JOIN {$wpdb->term_relationships} tr ON tr.object_id = p.id"
                . " LEFT JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"
                . " LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"
            . " WHERE p.post_status = \'publish\' AND p.post_type = \'post\'"
            // if you want use slug instead ID change "AND t.term_id = %d" to "AND t.slug = %s"
            . " AND tt.taxonomy = \'category\' AND t.term_id = %d"  
            . " ORDER BY p.post_date ASC";
    $ids = $wpdb->get_results( $wpdb->prepare($query, $selected_cat_ID), OBJECT );

    // is post assigned to category after update
    $in_category = false;
    $assigned_posts = [];

    foreach( $ids as $row ) {
        $assigned_posts[] = $row->id;
        if ( $row->id == $post_id )
            $in_category = true;
    }

    if ( !$in_category && $incr_number === \'\' ) {
        //
        // after the update post is not in the category, before the update was not either
        return;
    }
    else if ($in_category && $incr_number !== \'\' && $post_before->post_date_gmt == $post_after->post_date_gmt ) {
        //
        // after the update, post is still in the category,
        // publish date has not changed,
        // numbering doesn\'t need to be changed
        return;
    }
    else if ( !$in_category && $incr_number !== \'\' ) {
        //
        // post has he been removed from category
        delete_post_meta($post_id, \'incr_number\');
    }

    // update numbering
    if ( !empty($assigned_posts) ) {
        $counts = 0;
        foreach( $assigned_posts as $pid )
            update_post_meta($pid, \'incr_number\', ++$counts);
    }
}
add_action( \'post_updated\', \'updateNumbers\', 10, 3);

SO网友:Tedinoz

wp\\U帖子中没有称为“post\\u cat”的字段。

请尝试“post\\u category”。

结束

相关推荐

Post in multiple categories

我尝试在多个类别中列出帖子。管理面板中一切正常。我进入所有帖子,选择所需的帖子,然后进入编辑并选择一个类别。保存更改后,在“类别”选项卡下会写入旧类别和新类别。现在,当我访问网站并选择更新类别时,只有旧帖子,没有新帖子。例如:我有类别:电影、游戏、最佳和帖子:最佳电影、最佳游戏、最佳,我需要这样:最佳电影属于电影类别最佳游戏属于游戏类别,两者都属于最佳类别我使用日期和职位名称作为永久链接。