显示菜单中的文章数

时间:2014-12-19 作者:PiotrK

我想显示链接到菜单项的特定类别中的许多文章。它应该看起来像引导程序的“徽章”组件。现在我有:

Current - without

以及我想要实现的目标:

What I want

我是否必须修改WP的代码,或者是否有插件?我试图找到一个,但我看到的没有一个能解决这种特殊情况。

如果计数器只显示已发布的文章,那将是最好的,但这是一件小事——可能是在从数据库检索文章数量时添加一个条件的问题。

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

我认为为您的菜单创建一个定制的助行器会很有用。在下面的代码示例中,我使用了菜单项对象。您可以查看对象输出here.

为每个菜单项指定对象类型。所以我只是比较对象是否是一个类别。如果是,我使用指定的对象ID获取类别+类别计数。最后但并非最不重要的一点是,我只是在链接的末尾用一个范围来包装类别计数。我希望它对你有用。

下面是代码示例:

class Menu_Category_Count extends Walker_Nav_Menu {

    var $number = 1;

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';

        $class_names = $value = \'\';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = \'menu-item-\' . $item->ID;

        $class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';

        $id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
        $id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';

        $output .= $indent . \'<li\' . $id . $value . $class_names .\'>\';

        $atts = array();
        $atts[\'title\']  = ! empty( $item->attr_title ) ? $item->attr_title : \'\';
        $atts[\'target\'] = ! empty( $item->target )     ? $item->target     : \'\';
        $atts[\'rel\']    = ! empty( $item->xfn )        ? $item->xfn        : \'\';
        $atts[\'href\']   = ! empty( $item->url )        ? $item->url        : \'\';

        $atts = apply_filters( \'nav_menu_link_attributes\', $atts, $item, $args );

        $attributes = \'\';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( \'href\' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= \' \' . $attr . \'="\' . $value . \'"\';
            }
        }

        // Get Category + Category Count
        $category_count = "";
        if ($item->object == "category") {
            $category = get_category($item->object_id);
            $category_count = $category->category_count;
        }

        $item_output = $args->before;
        $item_output .= \'<a\'. $attributes .\'>\';
        $item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
        $item_output .= ($item->object == "category") ? \'<span class="count">\'.$category_count.\'</span>\': \'\';
        $item_output .= \'</a>\';
        $item_output .= $args->after;

        $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
    }

}
而不仅仅是打电话给定制导航步行器,在那里你可以得到如下导航菜单:

wp_nav_menu(
    array(
        ....,
        "walker"            => new Menu_Category_Count(),
    )
);
如果您从未接触过定制导航助行器,请查看this tutorial at tutsplus

SO网友:Amit Mishra

嘿,通过使用代码片段,您可以获得类别中的文章数量

$postsInCat = get_term_by(\'name\',\'NAMEOFCATEGORY\',\'category\');
$postsInCat = $postsInCat->count;
echo $postsInCat;
我想这对你有帮助

结束

相关推荐

List all blog categories

我已经创建了一个博客插件。我的博客中有博客类别。我想获得所有博客类别的列表,并将其列在我的www.domain中。com/blogs/page。我的博客类别名称为“blogcategory”。我不知道如何在list-category 作用我是wordpress开发的nooby。