What i mean is i am trying to add breadcrumbs to my blog and i thought of adding it before content or after title but there’s an issue with that – on archive pages or category pages the breadcrumbs appear for every posts.
so i figured i have to attach the breadcrumbs to something above the post – like the head.
how can i do that ?
this is what I’ve done so far for the content:
add_filter( 'the_content', array($this, 'adding_bread_crumbs') );
function adding_bread_crumbs( $content ) {
/* Getting the current page permalink and site url */
$pageURl = preg_replace('//$/','',get_permalink());
$homeURL = preg_replace('//$/','',site_url());
if ( function_exists('yoast_breadcrumb')) {
/* Checking if the current page is home page */
if($pageURl != $homeURL && !is_page('Home')){
$breadcrumbs = '<div class="breadcrumbHolder">';
$breadcrumbs .= yoast_breadcrumb('<p id="breadcrumbs">','</p>',false);
$breadcrumbs .= '</div>';
$content = $breadcrumbs.$content;
}
}
return $content;
}
Is there anything that would replace ‘the_content’ another hook maybe? or is there an action i have to use ?
Thanks
So far as I am aware, there is no hook in the location you need, if I understand you. The closest I can think of is
loop_start
, which as you might expect runs when the loop begins. The problem with that hook is that you don’t know what content the theme may have between the<head>
of the document and the place where the Loop runs.By far the best solution is to edit the functionality, or your own hook, into your theme if you’ve written the theme, or create a child theme and edit that if you are using someone else’s theme.
Inside your theme, wherever you want your output to show up, add the following:
Then you’ll be able to use add_action( ‘my_sweet_hook’, ‘my_sweet_breadcrumbs’ ); (along with the appropriate function code) to display your content there.
In WordPress 5.2 there’s wp_body_open which is triggered after the opening tag.