事实证明,我今天确实需要这样做,所以我继续写了一个页面解决方案。除了CPT之外,我不使用POST,但只要稍加修改,这也可以用于POST。我留下了我之前的答案,因为它确实提供了一种可能足以满足某些用途的解决方案。
add_filter(\'wpseo_breadcrumb_links\', \'wpse350538_customize_yoast_breadcrumbs\');
function wpse350538_customize_yoast_breadcrumbs($links)
{
//get current page slug and make it pretty
$current_page = sanitize_post($GLOBALS[\'wp_the_query\']->get_queried_object());
$slug = $current_page->post_name;
$slug_pretty = str_replace(\'-\', \' \', $slug);
if (is_page()) {
//this page has ancestors
if ($current_page->post_parent) {
//set the first breadcrumb to homepage
$links = array(
0 => array(
\'url\' => get_home_url(),
\'text\' => \'home\'
)
);
//get a list of parent pages, loop through them and add to link array
$parents = get_post_ancestors($current_page->ID);
foreach ($parents as $parent) {
$id = get_post_field(\'ID\', $parent);
//make sure we don\'t add the current page twice
if ($id !== $current_page->ID) {
//get parent slug and make it pretty
$parent_slug = get_post_field(\'post_name\', $current_page->post_parent);
$links[] = array(
\'url\' => get_permalink($id),
\'text\' => str_replace(\'-\', \' \', $parent_slug)
);
}
}
//lastly, add current page to link array
$links[] = array(
\'url\' => $slug,
\'text\' => $slug_pretty
);
} else {
// this page is top level so we just use the slug and make it pretty
$links = array(
0 => array(
\'url\' => get_home_url(),
\'text\' => \'home\'
),
1 => array(
\'url\' => $slug,
\'text\' => $slug_pretty
)
);
}
}
return $links;
}