【问题标题】:Filter wordpress-menu by category name按类别名称过滤 wordpress 菜单
【发布时间】:2015-04-11 14:37:00
【问题描述】:

我需要按类别名称过滤 wordpress 菜单。

到目前为止,我发现的是以下循环,它为我提供了所有菜单项:

function exclude_menu_items( $items, $menu, $args ) {
    // Iterate over the items to search and destroy
    foreach ( $items as $key => $item ) {
        //if ( $item->object_id == 168 ) unset( $items[$key] );
        print_r($item);
        echo '<br>';
        //var_dump(get_the_category($item->ID));
        //echo get_query_var('cat');
    }

    return $items;
}

add_filter( 'wp_get_nav_menu_items', 'exclude_menu_items', null, 3 );

我为页面添加了具有如下类别的功能:

function add_categories_for_pages() {
    register_taxonomy_for_object_type('category', 'page');
}
add_action( 'init', 'add_categories_for_pages' );

但是现在如何从菜单项中获取页面类别?

【问题讨论】:

    标签: php wordpress add-filter


    【解决方案1】:

    似乎我找到了解决方案,我不知道它是否是最好的,但到目前为止它似乎有效:

    function exclude_menu_items( $items, $menu, $args ) {
        // Iterate over the items to search and destroy
        foreach ( $items as $key => $item ) {
            $categories = get_the_category( $item->object_id );
            foreach($categories as $category){
                if($category->cat_name != 'mycatname'){
                    unset( $items[$key] );
                }
            }
        }
        return $items;
    }
    

    你怎么看?

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 2021-10-12
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      相关资源
      最近更新 更多