1

I know this may be off-topic as it has to do with Genesis (a premium framework), but with general WordPress, this has to do with running a remove_action and an add_action depending on if a custom field exists. I'm not seeing something simple that I'm doing wrong here.

What I'm trying to do is show the default breadcrumbs unless a custom fields exists, and if so, display the contents of a custom field.

Why doesn't

remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' ); add_action('genesis_before_loop', 'customfield_breadcrumbs');

fire when a custom field exists, remove the default breadcrumbs, add customfield_breadcrumbs and then echo my custom field?

The full function:

function customfield_breadcrumbs() {

global $post;

$crumb = get_post_meta($post->ID, 'custom_breadcrumb', true); 

    if ($crumb) {

remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
add_action('genesis_before_loop', 'customfield_breadcrumbs');

echo '<div class="breadcrumb">';
echo get_post_meta($post->ID, 'custom_breadcrumb', $single = true);
echo '</div>';

    }
}

This correctly outputs the html (from $post and $crumb) of the custom field, but I need to display the default breadcrumbs unless there is a custom field:

 function customfield_breadcrumbs() {

    global $post;

    $crumb = get_post_meta($post->ID, 'custom_breadcrumb', true); 

        if ($crumb) {

    echo '<div class="breadcrumb">';
    echo get_post_meta($post->ID, 'custom_breadcrumb', $single = true);
    echo '</div>';

        }
    }

And this is the core breadcrumb function from Genesis:

function genesis_breadcrumb( $args = array() ) {

    global $_genesis_breadcrumb;

    if ( ! $_genesis_breadcrumb )
        $_genesis_breadcrumb = new Genesis_Breadcrumb;

    $_genesis_breadcrumb->output( $args );

}

add_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );

And they are configured by settings in Genesis >> Theme Settings:

enter image description here

| improve this question | | | | |
  • When are you calling the function? Does $post contain expected value and $crumb is in turn as expected? – Rarst Dec 16 '14 at 16:44
  • Rarst, thanks, see edited quesiton. I get the corect values for $post and $crumb But the if statement doesn't remove_action and add_action. – markratledge Dec 16 '14 at 16:51
  • Are the actions added with priorities in framework code? Those are needed on removal to uniquely identify what is being removed. – Rarst Dec 16 '14 at 16:56
  • Interesting, I don't know. I just added to my question the core breadcrumb function from Genesis for reference; there are no priority calls in it. So I need a priority? – markratledge Dec 16 '14 at 17:04
  • when do you call customfield_breadcrumbs? it seems odd that you're adding an action to call customfield_breadcrumbs inside customfield_breadcrumbs, not sure how you're expecting that to work. – Milo Dec 16 '14 at 17:07
1

I'm not clear on how your code works as-is, as I mentioned in my comment. It looks like you're adding an action to call a function inside the function that you want to call with that action. If nothing outside the function invokes it, it never runs.

add_action( 'template_redirect', 'check_breadcrumb_condition' );

function check_breadcrumb_condition(){
    global $post;
    $crumb = get_post_meta($post->ID, 'custom_breadcrumb', true); 
    if ($crumb) {
        remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
        add_action( 'genesis_before_loop', 'customfield_breadcrumbs' );
    }
}

function customfield_breadcrumbs() {
    global $post;
    echo '<div class="breadcrumb">';
    echo get_post_meta($post->ID, 'custom_breadcrumb', true);
    echo '</div>';
}
| improve this answer | | | | |
  • Ah, that works! I didn't know enough to be able to describe what I was trying to do. I now see that the remove and add actions need to be in a separate function called first, before they are modified by customfield_breadcrumbs. Thanks for your patience. – markratledge Dec 17 '14 at 18:30
  • you might also want to check is_single() along with the meta value, otherwise if you visit an archive page where the first post has a custom breadcrumb, you'll get that post's breadcrumb instead of the archive breadcrumb. – Milo Dec 17 '14 at 20:00
  • Thanks, good point, and I did that. Genesis also gives options where the breadcrumbs display (I added a screenshot to my question) as to archives, etc. – markratledge Dec 17 '14 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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