Adding Breadcrumbs
-
GeneratePress:
Is there any way to add breadcrumbs in the theme?
:RedthruViolet
-
Breadcrumbs aren’t built into the theme, but you can easily integrate breadcrumbs into it.
For example, WordPress SEO has a breadcrumbs feature, which you can add to the theme in two ways:
1. The GP Hooks addon – simply paste the breadcrumbs PHP function into your desired hook (Before Content most likely), click execute PHP and you’re done.
2. With a PHP function in a child theme. Which would look something like this: https://gist.github.com/generatepress/a13fbd2bb88008da572f
To get the breadcrumbs code, go to “SEO > Internal Links”, and you’ll find it at the bottom.
This same method can be used with any third-party breadcrumb plugin that offers PHP code.
Let me know if this helps or not,
TomHi there,
Were you able to add the breadcrumbs to the theme?
Let me know 🙂
TomYes, breadcrumbs have been added.
You can see them here:
http://www.sustainablemontereycounty.com/likeweebly/
In the child theme header.php file, after “<?php do_action( ‘generate_after_header’ ); ?>”, I put this code:
<div class=”grid-container grid-parent”>
<?php the_breadcrumb(); ?>
</div>Here is the breadcrumb code, which, unfortunately I had to place in the parent theme’s functions.php file (this seems to be the only outstanding issue):
function the_breadcrumb()
{
global $post;/* Check for Front Page */
if (is_front_page())
{
echo ‘<p id=”breadcrumbs”> You are Here: <a href=”‘;
echo get_option(‘home’);
echo ‘”>‘;
echo ‘Home’;
echo ‘ </p>’;
}else
{
/* Show Breadcrumbs */
echo ‘<ul id=”breadcrumbs”>’;
echo ‘
- You are Here: <a href=”‘;
echo get_option(‘home’);
echo ‘”>’;
echo ‘Home’;
echo ‘ - ‘;
the_category(‘ - ‘);
- ‘;
the_title();
echo ‘ - ‘.get_the_title($ancestor).’
- <strong title=”‘.$title.'”> ‘.$title. ‘
- ‘ .get_the_title(). ‘
- Archive for “; the_time(‘F jS, Y’); echo’
- Archive for “; the_time(‘F, Y’); echo’
- Archive for “; the_time(‘Y’); echo’
- Author Archive”; echo’
- Blog Archives”; echo’
- Search Results”; echo’
<li class=”separator”> > ‘;
if( is_home() && get_option(‘page_for_posts’) )
{
/* echo ‘‘; */
echo ‘‘ . apply_filters(‘the_title’,get_page( get_option(‘page_for_posts’) )->post_title) . ‘‘;
/* echo ‘‘; */
}if (is_category() || is_single())
{
echo ‘<li class=”separator”> >
if (is_single())
{
echo ‘
<li class=”separator”> >‘;
}
}elseif (is_page())
{
if($post->post_parent)
{
$anc = get_post_ancestors( $post->ID );
$anc_count = count($anc);
// echo “
anc_count = ” . $anc_count;$title = get_the_title();
/* Go backwards through the array, to get the correct path display */
for ($k=$anc_count; $k>0; $k=$k-1)
{
$ancestor = $anc[$k-1];
$output = ‘<li class=”separator”> > ‘;
echo $output;}
echo ‘
‘;
}else
{
echo ‘‘;
}}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo”‘;}
elseif (is_month()) {echo”‘;}
elseif (is_year()) {echo”‘;}
elseif (is_author()) {echo”‘;}
elseif (isset($_GET[‘paged’]) && !empty($_GET[‘paged’])) {echo ”‘;}
elseif (is_search()) {echo”‘;}
echo ”;
}
}You can put that function in the child theme’s functions.php file instead of the parent theme’s functions.php file – it will work the same.
Then you can hook it into the header instead of editing that file, like this:
add_action('generate_after_header','custom_add_breadcrumbs'); function custom_add_breadcrumbs() { ?> <div class="grid-container grid-parent"> <?php the_breadcrumb(); ?> </div> <?php }That would also go in the child theme’s functions.php file.
Well, I tried some of that.
I copied functions.php to the child theme folder, and added the function there.
Then, when I launched the site, there were error messages saying you cannot redeclare a function.The functions file should be empty except for your new function.
The whole file looks like this:
<?php add_action('generate_after_header','custom_add_breadcrumbs'); function custom_add_breadcrumbs() { ?> <div class="grid-container grid-parent"> <?php the_breadcrumb(); ?> </div> <?php } function the_breadcrumb() { ... The rest of the_breadcrumb function }There’s no need to copy the functions over from the parent’s functions.php file – those functions are included anyways.
Well, thank you so much!
That worked.You’re very welcome.
- You are Here: <a href=”‘;
The topic ‘Adding Breadcrumbs’ is closed to new replies.
