Halloween Sale 15% off Ends in See the plans
PHP

Add a Parent Page to Yoast Breadcrumbs for a Post Type

Puts a regular page in front of a custom post type in the Yoast SEO breadcrumbs, for example Home > Services > Web Design when services have no archive.

Don’t have WPIDE yet? Install it on your site

Code

// Post type => ID of the page to show before it.
$wpide_crumb_parents = array(
    'service' => 42,
);

add_filter( 'wpseo_breadcrumb_links', function ( $links ) use ( $wpide_crumb_parents ) {
    if ( ! is_array( $links ) || ! is_singular( array_keys( $wpide_crumb_parents ) ) ) {
        return $links;
    }

    $page_id = (int) $wpide_crumb_parents[ get_post_type() ];

    if ( ! $page_id || 'publish' !== get_post_status( $page_id ) ) {
        return $links;
    }

    array_splice( $links, 1, 0, array( array( 'url' => get_permalink( $page_id ), 'text' => get_the_title( $page_id ), 'id' => $page_id ) ) );

    return $links;
} );

Notes

Map each post type to the ID of the page that should appear as its parent.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.