Breadcrumb Modification for Blog

User
add_filter('wpseo_breadcrumb_links', 'add_parent_page_to_breadcrumb');
function add_parent_page_to_breadcrumb($links) {

// Get the current post
if (is_singular('page')) {
global $post;

// Check if the current page has a parent
if ($post->post_parent) {
unset($links[0]);
// Get the parent page details
$parent_id = $post->post_parent;
$parent = get_post($parent_id);

// Build the parent breadcrumb link
$parent_link = [
'url' => get_permalink($parent_id),
'text' => $parent->post_title,
'id' => $parent_id,
];

// Add the parent link before the current page link
array_splice($links, -1, 0, [$parent_link]);
}
}

return $links;
}
if blog detail page then don't home first link always show blog page as first link

Assistant
To modify your breadcrumb function so that it always shows the blog page as the first link on the blog detail pages, you can enhance the existing code by checking if the current page is a blog post and adjusting the breadcrumb links accordingly. Here's the updated code:

add_filter('wpseo_breadcrumb_links', 'add_parent_page_to_breadcrumb');
function add_parent_page_to_breadcrumb($links) {
    // Get the current post
    if (is_singular('page')) {
        global $post;

        // Check if the current page has a parent
        if ($post->post_parent) {
            unset($links[0]);
            // Get the parent page details
            $parent_id = $post->post_parent;
            $parent = get_post($parent_id);
             
            // Build the parent breadcrumb link
            $parent_link = [
                'url' => get_permalink($parent_id),
                'text' => $parent->post_title,
                'id'   => $parent_id,
            ];

            // Add the parent link before the current page link
            array_splice($links, -1, 0, [$parent_link]);
        }
    }

    // Check if the current post is a blog post
    if (is_single() && 'post' === get_post_type()) {
        // Get the blog page ID
        $blog_page_id = get_option('page_for_posts');
        
        // Remove the home link
        unset($links[0]);

        // Add the blog page link as the first link
        if ($blog_page_id) {
            $blog_link = [
                'url' => get_permalink($blog_page_id),
                'text' => get_the_title($blog_page_id),
                'id'   => $blog_page_id,
            ];
            array_unshift($links, $blog_link);
        }
    }

    return $links;
}

### Explanation:
1. **Parent Page Logic**: The existing logic for adding the parent page to the breadcrumb remains unchanged.
2. **Blog Page Logic**:
– It checks if the current post is a single blog post (`is_single()` and `get_post_type()`).
– It retrieves the ID of the blog page using `get_option('page_for_posts')`.
– It unsets the first link (the home link) and adds the blog page link at the beginning of the breadcrumb array.

This modification ensures that when viewing a blog detail page, the breadcrumb will start with the blog page instead of the home link.

Leave a Comment

Your email address will not be published. Required fields are marked *