使用短码显示自定义帖子类别计数和WordPress类别计数

时间:2018-05-26 作者:Tyree Brown

我试图创建一个快捷码来显示自定义帖子类型以及标准帖子类别的帖子数量。我已经使用来自similar post:

// Add Shortcode to show posts count inside a category
function category_post_count( $atts ) {

    $atts = shortcode_atts( array(
        \'category\' => null
    ), $atts );

    // get the category by slug.
    $term = get_term_by( \'slug\', $atts[\'category\'], \'category\');

    return ( isset( $term->count ) ) ? $term->count : 0;
}
add_shortcode( \'category_post_count\', \'category_post_count\' );
以上代码仅适用于标准wordpress帖子类别,不适用于自定义帖子类型。非常感谢您的帮助。

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

因为你已经通过了”category“在”get_terms_by()“”函数作为第三个参数。

请更换“add_custom_taxonomy_here“使用下面代码中的自定义分类法slug。

// Add Shortcode to show posts count inside a category
function category_post_count( $atts ) {

$atts = shortcode_atts( array(
    \'category\' => null
), $atts );

// get the category by slug.
$term = get_term_by( \'slug\', $atts[\'category\'], \'add_custom_taxonomy_here\');

return ( isset( $term->count ) ) ? $term->count : 0;
}
add_shortcode( \'category_post_count\', \'category_post_count\' );

结束

相关推荐

Remove ID page from wp_count

我试图创建一个页面计数器,排除某些ID页面,但代码不起作用。我正在使用此代码。我怎样才能修复它?谢谢<?php $count_pages = wp_count_posts(\'page\'); echo \"$count_pages->publish\"; $args = array( \'post__not_in\' => array(10010), ); query_posts($args); ?>