$post->post_parent 在页面上的行为与在functions.php 中的行为不同

发布于 2024-12-12 20:17:53 字数 2362 浏览 2 评论 0原文

我创建/修改了一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海不蓝 2024-12-19 20:17:53

$post 未在您的函数中定义。尝试将 $post 作为函数的参数:

function the_breadcrumb($post) {

$post ist not defined in your function. Try to give $post as parameter to the function:

function the_breadcrumb($post) {
随梦而飞# 2024-12-19 20:17:53

或者,

global $post;

在函数顶部定义。

function fname()
{
   global $post;
   code...
}

Or, define

global $post;

at the top of the function.

function fname()
{
   global $post;
   code...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文