我正在尝试创建一个函数,在循环外创建一条包含页面片段的面包屑轨迹。这些面包屑显示从主页到当前(子)页面的路径。
例如,在“example.com/attractions/parcs/parc name”页面上,会显示以下面包屑:Home > Attractions > Parcs > Parc Name
我做了很多研究,发现了可以执行部分函数的各种代码片段,但我的PHP技能还不足以自己创建整个函数。
这就是我所拥有的:
在循环中获取后段塞:$slug = basename(get_permalink());
(src) global $post; echo
$post->post_name; (src:见上文)
<?php
global $post;
if($post->post_parent) { $post_data = get_post($post->post_parent);
echo $post_data->post_name; }
?>
(src)if ( is_page() )
{
$post = $wp_query->get_queried_object();
if ( $post->post_parent == 0 )
{
echo "<li> » ".the_title(\'\',\'\', FALSE)."</li>";
}
else
{
$title = the_title(\'\',\'\', FALSE);
$ancestors = array_reverse( get_post_ancestors( $post->ID ) );
array_push($ancestors, $post->ID);
foreach ( $ancestors as $ancestor )
{
if( $ancestor != end($ancestors) )
{
echo \'<li> » <a href="\'. get_permalink($ancestor) .\'">\'. strip_tags( apply_filters( \'single_post_title\', get_the_title( $ancestor ) ) ) .\'</a></li>\';
}
else
{
echo \'<li> » \'. strip_tags( apply_filters( \'single_post_title\', get_the_title( $ancestor ) ) ) .\'</li>\';
}
}
}
}
非常感谢所有想法、建议和解决方案:)