wp_nav_menu计算自定义帖子类型的帖子 [英] wp_nav_menu count posts from custom post-type

查看:54
本文介绍了wp_nav_menu计算自定义帖子类型的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立wordpress投资组合网站.

I am making a wordpress portfolio website.

我想从菜单中的自定义帖子类型统计帖子.

I want to count my posts from a custom post-type in my menu.

这是我想要实现的:

Designs  Films 
 15        20

这是我菜单的代码:

<?php
            if (get_sub_option(GN_SLUG, 'use_bootstrap', false)) {
                wp_nav_menu(array(
                    'menu' => 'top',
                    'theme_location' => 'top',
                    'depth' => 2,
                    'container' => 'div',
                    'container_class' => 'collapse navbar-collapse',
                    'container_id' => 'navbar',
                    'menu_class' => 'nav navbar-nav navbarTop',
                    'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
                    'walker' => new wp_bootstrap_navwalker())


                );
            }
            else {
                wp_nav_menu(array('theme_location' => 'top'));
            }
        ?>

我知道如何计算自定义帖子类型

I know how to count my custom post-type

$posts = get_posts('post_type= work_content');
$count = count($posts);
echo $count;

但是我不知道如何将计数器结合到菜单上...这可能吗?我该如何处理?

But I don't know how to combine the counter to my menu... Is this possible? How can I handle this?

谢谢!

推荐答案

1)首先,您应该在 wordpress.stackexchange.com

1) At first, you should ask WP question at wordpress.stackexchange.com

2)关于解决方案:您可以执行此操作:

2) about solution: you can do this :

add_filter( 'wp_nav_menu_items', function($menu) { 
    $GLOBALS['my_counter_fro_design']= substr_count($menu, 'menu-item-object-Designs');
    $GLOBALS['my_counter_fro_film']= substr_count($menu, 'menu-item-object-Films');
});

然后,在执行 wp_nav_menu 函数之后,您可以:

then , after executing the wp_nav_menu function, you can i.e.:

var_dump($GLOBALS['my_counter_fro_design']); exit;

这篇关于wp_nav_menu计算自定义帖子类型的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆