$post->post_parent 在页面上的行为与在functions.php 中的行为不同
我创建/修改了一个在 WordPress 页面上显示面包屑的函数。修改后的版本使用 #post->post_parent 来获取页面的父页面,以便获得完整的面包屑路径(主页 > 第 1 页 > 第 2 页 > 第 3 页与主页 > 第 3 页
)代码在页面 上完美执行(即主页 > 第 1 页 > 第 2 页 > 第 3 页)
。但是当我将它放入一个函数并从functions.php页面调用它时,它无法使用 $post->post_parent (即第3页与主页>第3页)< 检测该页面是否有父级/代码>。
这可能是因为页面代码是在 the_loop 中执行的,但该函数不知何故位于其外部?
页面代码:
if (!is_home()) {
echo "<ul id='breadcrumb'>";
echo '<li><a href="';
echo get_option('home');
echo '">HOME';
echo "</a></li>";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
the_title('<li>', '</li>');
echo "</ul>";
}
} elseif (is_page()) {
if(!$post->post_parent){
//echo "No Parent";
}
else{
echo '<li>'. wp_list_pages('include='.$post->post_parent.'&title_li=' ).'</li>';
}
the_title('<li>', '</li>');
echo "</ul>";
}
}
函数代码:
function the_breadcrumb() {
if (!is_home()) {
echo "<ul id='breadcrumb'>";
echo '<li><a href="';
echo get_option('home');
echo '">HOME';
echo "</a></li>";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
the_title('<li>', '</li>');
echo "</ul>";
}
} elseif (is_page()) {
if(!$post->post_parent){
//echo "No Parent";
}
else{
echo '<li>'. wp_list_pages('include='.$post->post_parent.'&title_li=' ).'</li>';
}
the_title('<li>', '</li>');
echo "</ul>";
}
}
}
此代码本质上没有什么不同,只是它现在包装在函数中。它不显示父页面的事实令人沮丧。我不想在我创建的每个页面模板中都包含此代码。
帮助&建议将不胜感激!
I created/modified a function to display breadcrumbs on pages on WordPress. The modified version makes use of #post->post_parent to get the parent of a page in order to have a full breadcrumb trail (home > page 1 > page 2 > page 3 vs. home > page 3)
The code executes perfectly on page (ie. home > page 1 > page 2 > page 3)
. But when I place it into a function and call it form the functions.php page it cannot detect if the page has a parent using $post->post_parent (ie. page 3 vs. home > page 3)
.
Could this be because the on page code is executed in the_loop but the function is somehow outside of it?
On page code:
if (!is_home()) {
echo "<ul id='breadcrumb'>";
echo '<li><a href="';
echo get_option('home');
echo '">HOME';
echo "</a></li>";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
the_title('<li>', '</li>');
echo "</ul>";
}
} elseif (is_page()) {
if(!$post->post_parent){
//echo "No Parent";
}
else{
echo '<li>'. wp_list_pages('include='.$post->post_parent.'&title_li=' ).'</li>';
}
the_title('<li>', '</li>');
echo "</ul>";
}
}
Function code:
function the_breadcrumb() {
if (!is_home()) {
echo "<ul id='breadcrumb'>";
echo '<li><a href="';
echo get_option('home');
echo '">HOME';
echo "</a></li>";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
the_title('<li>', '</li>');
echo "</ul>";
}
} elseif (is_page()) {
if(!$post->post_parent){
//echo "No Parent";
}
else{
echo '<li>'. wp_list_pages('include='.$post->post_parent.'&title_li=' ).'</li>';
}
the_title('<li>', '</li>');
echo "</ul>";
}
}
}
There is nothing inherently different about this code except that it is now wrapped in a function. The fact that it doesn't display the parent pages is frustrating. I don't want to have to include this code on every page template I create.
Help & Suggestions will be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$post
未在您的函数中定义。尝试将 $post 作为函数的参数:$post
ist not defined in your function. Try to give $post as parameter to the function:或者,
在函数顶部定义。
Or, define
at the top of the function.