自定义帖子类型和面包屑冲突

时间:2014-02-25 作者:AndrettiMilas

An example of the extra space

我有一个自定义的帖子类型。看起来面包屑为插入的父页面(不存在)提供了额外的空间。你可以在我所附的图片中看到这一点。

导致这个额外的、不存在的父级的自定义帖子类型发生了什么?

以下是我的自定义页面类型:

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

function register_cpt_result() {

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

    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'description\' => \'My firm\\\'s case results\',
        \'supports\' => array( \'title\', \'editor\', \'excerpt\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,


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

    register_post_type( \'result\', $args );
}
以下是我的面包屑功能:

function crumbs() {
    if ((is_page() && ! is_category()) || is_archive() || is_single() || is_single()) {
        $crumbs = \'\';
        $crumbs .= \'<div class="crumbs"><span xmlns:v="http://rdf.data-vocabulary.org/#">\';
        $crumbs .= \'<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="\'.get_bloginfo(\'url\').\'">Home</a></span>\';
        $post_ancestors = get_post_ancestors($post);

        if ($post_ancestors) {
            $post_ancestors = array_reverse($post_ancestors);
            foreach ($post_ancestors as $crumb)
                $crumbs .= \'<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="\'.get_permalink($crumb).\'">\'.get_the_title($crumb).\'</a></span>\';
        }
        if (is_category() || is_single()) {
            $category = get_the_category();
            $crumbs .= \'<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="\'.get_category_link($category[0]->cat_ID).\'">\'.$category[0]->cat_name.\'</a></span>\';
        }
        if (!is_category())
            $crumbs .= \'<span typeof="v:Breadcrumb"><span class="crumbs-last" property="v:title">\'.get_the_title().\'</span></span>\';
        $crumbs .= \'</span></div>\';
        $close_crumb = \'</span>\';
        $open_crumb = \'<span typeof="v:Breadcrumb">\';
        $separator = \'<span class="separator">→</span>\';
        echo str_replace($close_crumb.$open_crumb, $close_crumb.$separator.$open_crumb, $crumbs);
    }
}

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

您的自定义帖子类型与条件匹配is_single(), 因此,这一部分试图输出该帖子分配到的类别,我假设该类别不存在:

if (is_category() || is_single()) {
    $category = get_the_category();
    $crumbs .= \'<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="\'.get_category_link($category[0]->cat_ID).\'">\'.$category[0]->cat_name.\'</a></span>\';
}
检查是否$category 包含类别或更改is_single 只需检查post 岗位类型:

if (is_category() || is_singular(\'post\')) {

结束

相关推荐

Displaying oEmbed errors?

有时,通过oEmbed嵌入项目是不可能的,例如,当YouTube视频已禁用嵌入时。The oEmbed service will return a 401 Unauthorized, 并且不会转换代码。有没有办法通知用户这一点?当前的工作流是非直观的(至少对我来说),我更喜欢在WordPress页面上,或者更好的是,在编辑器中显示一条消息,说明对象无法嵌入。