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.

I have this function:

function the_breadcrumb() {
    if (!is_home()) {
        echo "Start » ";
        echo '<a href="';
        echo get_option('home');
        echo '">';
        bloginfo('name');
        echo "</a> » ";
        if (is_category() || is_single() ) {
        the_category('title_li=');
        if (is_single()) {
            echo " » ";
            the_title();
        }
    } elseif (is_page()) {
        echo the_title();
    } elseif (is_single() && $post_type == 'partners') {
        echo "IS THIS WORKING?";
            }
    } elseif (is_home()) {
    echo "Start » ";    bloginfo('name');
    }
}

I also have created a custo post type called 'partners'.

However, my breadcumb does not display my IS THIS WORKING TEXT

Help appreciated. Alex

share|improve this question

2 Answers 2

This is a classic failure to indent code leading to confusion issue.

If we reformat your code correctly we see:

function the_breadcrumb() {
    if (!is_home()) {
        echo "Start » ";
        echo '<a href="';
        echo get_option('home');
        echo '">';
        bloginfo('name');
        echo "</a> » ";
        if (is_category() || is_single() ) {
            the_category('title_li=');
            if (is_single()) {
                echo " » ";
                the_title();
            }
        } elseif (is_page()) {
            echo the_title();
        } elseif (is_single() && $post_type == 'partners') {
            echo "IS THIS WORKING?";
        }
    } elseif (is_home()) {
        echo "Start » ";    bloginfo('name');
    }
}

Now we can follow it appropriately. What's happening is it's entering the is_category || is_single() if statement, and executing that part first. Because the if statement succeeded, it never reaches your check later on.

But, even if it did, there's a second problem, you're checking $post_type, but where is this coming from? Nowhere is it declared as a global or assigned a value, it's been plucked out of thin air.

So here is my alternative:

function the_breadcrumb() {
    if (is_home()) {
        echo "Start » ";
        bloginfo('name');
        return;
    }
    echo "Start » ";
    echo '<a href="'.home_url().'">';
    bloginfo('name');
    echo "</a> » ";

    // Check each case starting with the most specific down to the most generic
    global $post;
    $post_type = '';
    if ( !empty( $post) {
        $post_type = $post->post_type;
    }
    if ( is_single() && $post_type == 'partners' ) {
        echo "IS THIS WORKING?";
    } elseif ( is_page() ) {
        echo the_title();
    } else if (is_category() || is_single() ) {
        the_category('title_li=');
        if (is_single()) {
            echo " » ";
            the_title();
        }
    }
}

Here I've used the global post object to get around the lack of a $post_type variable to compare against. I've also re-ordered the if statements from most specific to most generic. Finally I took the big if else on the is_home call and put it at the top and did a return, that way we don't nest the code as much and it becomes more readable, and a comment for good measure. I also used home_url instead of get_option('home')

So in summary:

  • Always indent correctly. You have no excuses for not doing it, most editors will do this for you. If yours doesn't, go grab sublimetext or another IDE like Komodo or PHPStorm.
  • Read your code from top to bottom and follow it in your head. It's not hard and bugs like this become quite obvious very quickly. It also gets far far easier as you do it, eventually you won't need to think about it at all, it'll just happen automatically
  • Never put » in html, always use the entitycode else your html won't validate
share|improve this answer
1  
+1 for the "indenting" lecture –  s_ha_dum Feb 12 '13 at 15:33
    
@Tom, thank you for the lecture :) Believe or not, I am trying to keep things organized but I can never learn how to paste the code in here to make it look like in my editor :( –  Alex Feb 12 '13 at 20:13
    
Right now I'm getting a Fatal error: Call to undefined function is_post_type() in ... - I registered the post type like this: register_post_type( 'partners', array( 'labels' => $labels, 'public' => true, 'supports' => $supports ) - but, maybe I'm doing it wrong? –  Alex Feb 12 '13 at 20:15
    
Maybe because you're mixing tabs and spaces? I've changed the is_post_type to a call to the global post object, is_post_type didnt do what I thought it did –  Tom J Nowell Feb 13 '13 at 10:09

In addition to Tom J Nowell's answer...

You haven't set $post_type anywhere. That condition will always fail, and if you enabled debugging you'd probably see some notices about it.

I don't know how to fix that as the code is out of context and I don't if, where, or how you've set $post_type. If you defined it as global somewhere then global $post_type; at the head of the function should do it. Maybe you need...

global $post;
$post_type = $post->post_type;

... but that could be problematic for some pages, like archive pages.n

Sorry, best I can do given the information.

share|improve this answer
    
Thank you @s_ha_dum - I registered post_type previously like this: 'register_post_type( 'partners', array( 'labels' => $labels, 'public' => true, 'supports' => $supports )' makes sense? –  Alex Feb 12 '13 at 20:10

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.