首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么wp_list_pages不能在functions.php工作?

为什么wp_list_pages不能在functions.php工作?
EN

Stack Overflow用户
提问于 2013-09-20 15:34:57
回答 1查看 140关注 0票数 0

我正在使用下面的代码添加一个子菜单,该子菜单只显示当前的顶级父级和父级中的任何子页面。它在sidebar.php中直接使用时工作,但是当我将它包装在一个函数中并将它放在functions.php中时,它只显示我所有的顶级菜单项,而不是活动的父级。我怎么才能让它起作用?

在侧栏中工作:

代码语言:javascript
复制
<?php
$child_of_value = ( $post->post_parent ? $post->post_parent : $post->ID );
    $depth_value = ( $post->post_parent ? 2 : 1 );
        $wp_list_pages_args = array( 
            'child_of' => $child_of_value,
            'depth' => $depth_value,
            'title_li' => '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>'
        );
    wp_list_pages( $wp_list_pages_args );
?>

不适用于functions.php

代码语言:javascript
复制
function page_submenu() {   
    $child_of_value = ( $post->post_parent ? $post->post_parent : $post->ID );
    $depth_value = ( $post->post_parent ? 2 : 1 );
        $wp_list_pages_args = array( 
            'child_of' => $child_of_value,
            'depth' => $depth_value,
            'title_li' => '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>'
        );
    wp_list_pages( $wp_list_pages_args );
}
EN

回答 1

Stack Overflow用户

发布于 2013-09-20 16:22:27

我需要将global $post;添加到函数中。它起作用了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18920609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档