【问题标题】:Retrieve number of posts检索帖子数
【发布时间】:2020-02-24 13:26:54
【问题描述】:

我使用 Wordpress 和 Anima 主题。这是菜单代码:

<nav id = "access" role = "navigation" aria-label = "<? php esc_attr_e ('Primary Menu', 'anima')?>" <? php cryout_schema_microdata ('menu'); ? >>
<? php cryout_access_hook ();?>
</nav>

这个洗站:https://simumods.com

我如何确保在 American Truck Simulator 子类别中,Euro Truck Simulator 2 显示哪个类别中有多少帖子?

【问题讨论】:

  • 您的问题是什么?与导航或帖子计数有关?
  • 我希望导航显示在哪个类别中有多少帖子。

标签: wordpress navigation themes


【解决方案1】:

好吧,这并不太难,首先我们将检查菜单项是否为分类,然后获取计数并显示它!

function ggstyle_menu_item_count( $output, $item, $depth, $args ) {
    // Check if the item is a Category or Custom Taxonomy
    if( $item->type == 'taxonomy' ) {
        $object = get_term($item->object_id, $item->object);

        // Check count, if more than 0 display count
        if($object->count > 0)
            $output .= "<span class='menu-item-count'>".$object->count."</span>";
    }    

    return $output;
}
add_action( 'walker_nav_menu_start_el', 'ggstyle_menu_item_count', 10, 4 );

要让项目计数输出到菜单项的&lt;a&gt;,我们必须拆分 $output 并插入我们的内容,然后将其重新组合在一起。

function ggstyle_menu_item_count( $output, $item, $depth, $args ) {
    // Check if the item is a Category or Custom Taxonomy
    if( $item->type == 'taxonomy' ) {
        $object = get_term($item->object_id, $item->object);

        // Check count, if more than 0 display count
        if($object->count > 0) {
            $output_new = '';
            $output_split = str_split($output, strpos($output, '</a>') );
            $output_new .= $output_split[0] . "<span class='menu-item-count'>".$object->count."</span>" . $output_split[1];
            $output = $output_new;
        }
    }    

    return $output;
}
add_action( 'walker_nav_menu_start_el', 'ggstyle_menu_item_count', 10, 4 );

重复帖子:https://wordpress.stackexchange.com/questions/229767/show-posts-count-for-categories-and-tags-in-wp-nav-menu

【讨论】:

  • 这样它就不能正确显示我有多少帖子。例如,在 ATS :: Bus 中,将帖子从 21 删除到 18。另外,我可以将这些数字放在括号中吗?例如 (18) 这样他们就不会像图片中那样坐在下面,因此:ATS :: Bus (17) i.imgur.com/hqksKcd.png
【解决方案2】:

我用这段代码解决了问题

add_filter('the_title', 'wpse165333_the_title', 10, 2);
function wpse165333_the_title($title, $post_ID)
{
    if( 'nav_menu_item' == get_post_type($post_ID) )
    {
        if( 'taxonomy' == get_post_meta($post_ID, '_menu_item_type', true) && 'category' == get_post_meta($post_ID, '_menu_item_object', true) )
        {
            $category = get_category( get_post_meta($post_ID, '_menu_item_object_id', true) );
            $title .= sprintf(' (%d)', $category->count);
        }
    }
    return $title;
}

【讨论】:

    【解决方案3】:

    WP 5.5.1 的更新代码:

    add_filter('the_title', 'prod_count_the_title', 10, 2);
    
    function prod_count_the_title($title, $post_ID) {
      if( 'nav_menu_item' == get_post_type($post_ID) ) { 
    
        if( 'taxonomy' == get_post_meta($post_ID, '_menu_item_type', true) && 'product_cat' == get_post_meta($post_ID, '_menu_item_object', true) ) { 
    
          $cat_ID =  get_post_meta( $post_ID, '_menu_item_object_id', true );  
          $term = get_term($cat_ID , 'product_cat' );   
          
          $title .= sprintf('<strong> %d </strong>', $term->count);
    
        }
    
      }
      return $title;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多