如何在筛选器菜单中显示带有链接的自定义分类?

时间:2011-07-23 作者:Adrian

两天以来,我一直在尝试解决自定义分类法(categories)的问题。我有一个名为“Portfolio”的自定义帖子类型,它按预期工作,但我注册的层次分类法存在问题。我给它起了个名字:“画廊”。它不显示自定义分类法,只显示我博客中的默认类别。我在这里发现了一些类似的问题,但我真的不知道如何在我的案例中采用其他人的解决方案。不幸的是,我对wordpress还是个新手。有人能帮我吗?

这是我循环中的代码。php是我自定义帖子类型的模板(这是我的主页):

<div id="filtering-nav">
<a href="#" class="filter-btn"><span>Filter</span></a>
<ul>
    <li><a href="#all" class="all">All</a></li>
    <?php $args=array(\'orderby\' => \'name\');
    $galleries=get_categories($args);
    foreach($galleries as $gallery){ ?>
    <li><a href="#<?php echo $gallery->gallery_nicename; ?>" class="<?php echo $gallery->gallery_nicename; ?>"><?php echo $gallery->name; ?></a></li>
    <?php } ?>
</ul>
<div class="clearfix"></div>

这是我函数中的代码。php:

add_action(\'init\', \'portfolio_register\');
function portfolio_register() {
     $labels = array(
        \'name\' => _x(\'My Portfolio\', \'post type general name\'),
        \'singular_name\' => _x(\'Portfolio Item\', \'post type singular name\'),
        \'add_new\' => _x(\'Add New\', \'portfolio item\'),
        \'add_new_item\' => __(\'Add New Portfolio Item\'),
        \'edit_item\' => __(\'Edit Portfolio Item\'),
        \'new_item\' => __(\'New Portfolio Item\'),
        \'view_item\' => __(\'View Portfolio Item\'),
        \'search_items\' => __(\'Search Portfolio\'),
        \'not_found\' =>  __(\'Nothing found\'),
        \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
        \'parent_item_colon\' => \'\'
    );
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'menu_icon\' => get_stylesheet_directory_uri() . \'/images/portfolio-icon.png\',
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'menu_position\' => null,
        \'supports\' => array(\'title\',\'editor\',\'author\',\'thumbnail\',\'comments\')
      ); 
    register_post_type( \'portfolio\' , $args );
    flush_rewrite_rules();
}
// Custom taxonomy for Portfolio Categories (Galleries)
register_taxonomy(\'galleries\', array(\'portfolio\'), array(\'hierarchical\' => true, \'label\' => \'Galleries\', \'singular_label\' => \'Gallery\', \'rewrite\' => true, \'public\' => true ));
提前感谢!

1 个回复
SO网友:Rajeev Vyas

<div id="filtering-nav">
<a href="#" class="filter-btn"><span>Filter</span></a>
<ul>
    <li><a href="#all" class="all">All</a></li>
    <?php $args=array(\'orderby\' => \'name\',\'taxonomy\'=>\'galleries\');
    $galleries=get_categories($args);
    foreach($galleries as $gallery){ ?>
    <li><a href="<?php echo get_category_link( $gallery->term_id ) ; ?>" class="<?php echo $gallery->gallery_nicename; ?>"><?php echo $gallery->name; ?></a></li>
    <?php } ?>
</ul>
<div class="clearfix"></div>
当您将参数传递给get\\u categories函数时,需要将分类名称作为分类参数传递,以检索分类,而不是类别。。。

这是完整的参考资料。。

http://codex.wordpress.org/Function_Reference/get_categories

将您的代码与下面的代码进行比较,您在register post type的args中缺少分类法,而在custom taxonomy arg中重写slug。下面的代码工作正常。

$custom_slug = get_option(\'slug\') != \'\' ? get_option(\'slug\') : \'portfolio\';

    $args = array(
        \'labels\'            => array(
            \'name\'                  => __(\'Portfolio\'),
            \'singular_name\'         => __(\'Portfolio Project\'),
            \'add_new\'               => __(\'Add Project\'),
            \'add_new_item\'          => __(\'Add Project\'),
            \'new_item\'              => __(\'Add Project\'),
            \'view_item\'             => __(\'View Project\'),
            \'search_items\'          => __(\'Search Portfolio\'), 
            \'edit_item\'             => __(\'Edit Project\'),
            \'all_items\'             => __(\'Complete Portfolio\'),
            \'not_found\'             => __(\'No Projects found\'),
            \'not_found_in_trash\'    => __(\'No Projects found in Trash\')
        ),
        \'taxonomies\'        => array(\'portfolio-categories\', \'portfolio-clients\', \'portfolio-tags\'),
        \'public\'            => true,
        \'show_ui\'           => true,
        \'_builtin\'          => false,
        \'_edit_link\'        => \'post.php?post=%d\',
        \'capability_type\'   => \'post\',
        \'rewrite\'           => array(\'slug\' => __($custom_slug)),
        \'hierarchical\'      => false,
        \'menu_position\'     => 20,
        \'menu_icon\'         => WP_PLUGIN_URL . \'/portfolio/images/icon.jpg\',
        \'supports\'          => array(\'title\', \'editor\')
    );

    /** create portfolio categories (taxonomy) */
    register_taxonomy(\'portfolio-categories\', \'project\', array(
            \'hierarchical\'      => true,
            \'show_ui\'           => true,
            \'rewrite\'           => array(\'slug\' => __($custom_slug . \'/category\')),
            \'labels\'            => array(
                    \'name\'                          => __(\'Portfolio Categories\'),
                    \'singular_name\'                 => __(\'Portfolio Category\'),
                    \'search_items\'                  => __(\'Search Portfolio Categories\'),
                    \'popular_items\'                 => __(\'Popular Portfolio Categories\'),
                    \'all_items\'                     => __(\'All Portfolio Categories\' ),
                    \'parent_item\'                   => __(\'Parent Portfolio Category\'),
                    \'parent_item_colon\'             => __(\'Parent Portfolio Category\'),
                    \'edit_item\'                     => __(\'Edit Portfolio Category\'), 
                    \'update_item\'                   => __(\'Update Portfolio Category\'),
                    \'add_new_item\'                  => __(\'Add New Portfolio Category\'),
                    \'new_item_name\'                 => __(\'New Portfolio Category\'),
                    \'separate_items_with_commas\'    => __(\'Separate Portfolio Categories with commas\'),
                    \'add_or_remove_items\'           => __(\'Add or remove Portfolio Categories\'),
                    \'choose_from_most_used\'         => __(\'Choose from the most used Portfolio Categories\')
        )
    ));




    /** create new custom post type */
    register_post_type(\'portfolio\', $args);

结束