Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

An example of the extra space

I have a custom post type. It seems the breadcrumbs have an extra space for a parent page (which does not exist) inserted. You can see this in the image I attached.

What is happening with theis custom post type that is causing this extra, non-existent parent?

Here is my custom page type:

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 );
}

Here is my breadcrumbs function:

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);
    }
}
share|improve this question
    
Maybe you have to add condition: if(get_post_type() == 'result'){...manual add your cpt title...or use get_post_type_object('result')->labels->name...} –  NoSense Feb 25 at 20:59

1 Answer 1

up vote 1 down vote accepted

Your custom post type matches the conditional is_single(), so this part is trying to output the category the post is assigned to, which I'm assuming doesn't exist:

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>';
}

either check if $category contains a category, or change is_single to just check the post post type:

if (is_category() || is_singular('post')) {
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.