在导航下拉菜单中显示自定义内容

时间:2011-06-10 作者:puppybeard

我有一个自定义内容类型“balloons”,并且我已经为“balloon”项目“Water balloon”指定了父类别

我已将气球类别作为主菜单项。我想在下拉列表中显示一个指向“水气球”的链接,就像它是一个页面一样。

我不知道该怎么做,或者是否可能。有什么想法吗?提前谢谢。

2 个回复
SO网友:Bainternet

如果我理解的问题正确,那么您需要:

add_filter(\'walker_nav_menu_start_el\',\'auto_category_subMenu\',10,4);
function auto_category_subMenu($item_output, $item, $depth, $args){
    //first you check if the current item is a category
    if (!$item->object == \'category\'){
        return $item_output;
    }else{
    //if it is a category then check that it is the right one.
        if ($item->title == \'Water Balloon\'){
               //if we got this far the we are on the parent menu item and
               // we are going to get all of the balloon post type listed under it
            global $post;
            $tmp_post = $post;
            $args = array(
                \'post_type\' => \'balloons\',
                \'posts_per_page\' => -1,
                \'category_name\' => $item->title
            );
            $re = \'\';
            $submenu_items = get_posts( $args );
            foreach( $submenu_items as $post ){
                setup_postdata($post);
                $re .= \'<li ><a href="\'.get_permalink($post->ID).\'">\'.get_the_title($post->ID).\'</a></li>\';
            }
            $post = $tmp_post;
            return $item_output.\'<ul>\'.$re.\'</ul>\';
        }
    }
    return $item_output;
}
此代码假设您的类别名称为“Water Balloon”,并且您的帖子类型名称为“Ballogs”,如果不简单地更改它的话。

SO网友:Azizur Rahman

很可能你在什么地方有某种勾结。你有可能有这样的东西吗?

register_post_type( \'balloon\', $args ); 还有register_taxonomy( \'balloon\', array(\'balloon\'), $args );

看看下面的代码。这是一个完整的插件。将其与您的代码进行比较。

<?php
/*
  @Plugin Name: Balloons - Balloons Directory
  @Plugin URI: http://azizur-rahman.co.uk/
  @Description: Balloons Directory Manager
  @Author: Azizur Rahman
  @Version: 1.0
  @Author URI: http://azizur-rahman.co.uk/
  @package balloons
 */

add_action( \'init\', \'azizur_register_cpt_balloons\' );

function azizur_register_cpt_balloons() {

    $labels = array( 
        \'name\' => _x( \'balloons\', \'balloons\' ),
        \'singular_name\' => _x( \'balloon\', \'balloons\' ),
        \'add_new\' => _x( \'Add New\', \'balloons\' ),
        \'add_new_item\' => _x( \'Add New balloon\', \'balloons\' ),
        \'edit_item\' => _x( \'Edit balloon\', \'balloons\' ),
        \'new_item\' => _x( \'New balloon\', \'balloons\' ),
        \'view_item\' => _x( \'View balloon\', \'balloons\' ),
        \'search_items\' => _x( \'Search balloons\', \'balloons\' ),
        \'not_found\' => _x( \'No balloons found\', \'balloons\' ),
        \'not_found_in_trash\' => _x( \'No balloons found in Trash\', \'balloons\' ),
        \'parent_item_colon\' => _x( \'Parent balloon:\', \'balloons\' ),
        \'menu_name\' => _x( \'balloons\', \'balloons\' ),
    );

    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'description\' => \'balloons content type\',
        \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'post-formats\' ),
//        \'taxonomies\' => array( \'page-category\', \'balloon-category\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,


        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\'
    );

    register_post_type( \'balloons\', $args );
}


add_action( \'init\', \'azizur_register_taxonomy_balloon_types\' );

function azizur_register_taxonomy_balloon_types() {

    $labels = array( 
        \'name\' => _x( \'Balloon Types\', \'balloon types\' ),
        \'singular_name\' => _x( \'Balloon Type\', \'balloon types\' ),
        \'search_items\' => _x( \'Search Balloon Type\', \'balloon types\' ),
        \'popular_items\' => _x( \'Popular Balloon Type\', \'balloon types\' ),
        \'all_items\' => _x( \'All Balloon Type\', \'balloon types\' ),
        \'parent_item\' => _x( \'Parent Balloon Types\', \'balloon types\' ),
        \'parent_item_colon\' => _x( \'Parent Balloon Types:\', \'balloon types\' ),
        \'edit_item\' => _x( \'Edit Balloon Types\', \'balloon types\' ),
        \'update_item\' => _x( \'Update Balloon Types\', \'balloon types\' ),
        \'add_new_item\' => _x( \'Add New Balloon Types\', \'balloon types\' ),
        \'new_item_name\' => _x( \'New Balloon Types Name\', \'balloon types\' ),
        \'separate_items_with_commas\' => _x( \'Separate balloon type with commas\', \'balloon types\' ),
        \'add_or_remove_items\' => _x( \'Add or remove balloon type\', \'balloon types\' ),
        \'choose_from_most_used\' => _x( \'Choose from the most used balloon type\', \'balloon types\' ),
        \'menu_name\' => _x( \'Balloon Types\', \'balloon types\' ),
    );

    $args = array( 
        \'labels\' => $labels,
        \'public\' => true,
        \'show_in_nav_menus\' => true,
        \'show_ui\' => true,
        \'show_tagcloud\' => true,
        \'hierarchical\' => true,

        \'rewrite\' => true,
        \'query_var\' => true
    );

    register_taxonomy( \'balloon_types\', array(\'balloons\'), $args );
}

function azizur_balloons_cpt_rewrite_flush() {
  flush_rewrite_rules();
}
register_activation_hook(__FILE__, \'azizur_balloons_cpt_rewrite_flush\');
register_deactivation_hook(__FILE__, \'azizur_balloons_cpt_rewrite_flush\');

?>

结束