Php Wordpress菜单项

Php Wordpress菜单项,php,wordpress,Php,Wordpress,我有一个主页,其中有一个子项。现在我的问题是,主页是否可以直接链接到其中的子项?例如 -主页(链接到第2页) ---第2页 这是我的密码: <div id="MainNav"> <ul> <?php wp_list_pages('exclude=3&sort_column=menu_order&title_li=&depth=1'); ?> </ul> </div> <div id="leftCol">

我有一个主页,其中有一个子项。现在我的问题是,主页是否可以直接链接到其中的子项?例如

-主页(链接到第2页)

---第2页

这是我的密码:

<div id="MainNav">
<ul>
<?php wp_list_pages('exclude=3&sort_column=menu_order&title_li=&depth=1'); ?>
</ul>
</div>
<div id="leftCol">
<?php if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } else { ?>
<?php } ?>
</div>


乍一看,您的方法应该有效。如果没有关于你问题的更多信息,就不能说更多

作为替代,我在我维护的博客上做了类似的事情,但需要对输出进行更多的控制。所以我最终实现了自己的功能。我是这样做的:

// Generate the subnav for a page
// Returns a list of links to child pages
// if there are no children, returns a blank list
function get_subnav($page_id)
{
     $current_page = get_page($page_id);
     if ($current_page->ancestors) {
       $child = get_page($page_id);
       $ancestor_id = $child->ancestors[0];
       $page = get_page($ancestor_id);
     } else {
       $page = get_page($page_id);
     }

     $children = get_children('post_parent=' . 
            $page->ID . '&post_type=page&post_status=publish&order=ASC&orderby=menu_order');

     if ($children) {
       $html = '';
       foreach ($children as $child) {
       $html .= "<li><a href='" . get_page_link($page->ID) . "'>" . get_post_meta($page->ID, 'nav_name', true) . "</a></li>\n";
       }
     } else {
       return false;
}


return $html;
//为页面生成子AV
//返回指向子页面的链接列表
//如果没有子级,则返回一个空白列表
函数get\u subnav($page\u id)
{
$current\u page=get\u page($page\u id);
如果($当前页面->祖先){
$child=获取页面($page\u id);
$consensor_id=$child->consensors[0];
$page=get\u page($祖先\u id);
}否则{
$page=获取页面($page\u id);
}
$children=get_children('post_parent=')。
$page->ID'&post_type=page&post_status=publish&order=ASC&orderby=menu_order');
if(儿童){
$html='';
foreach($childrenas$child){
$html.=“
  • \n”; } }否则{ 返回false; } 返回$html;
    }

    我这样称呼它:
    getsubnav($post->ID)

    请注意,它不会爬过整个子页面树。如果您从子页面调用它,它将使用其第一个祖先作为根节点来构造导航。

    我不明白

    您提供的代码不会抛出错误(即使在调试模式下也不会)。你是在问这是否有效吗?如果是,是的。如果它不适合你,请告诉我们你的规格和你在你的主题中添加这个


    此外,您可能应该使用
    $wp\u query->post->post\u parent
    ,而不是
    $post->post\u parent
    。如果有人一直在使用自定义查询并在循环中调用它们,
    $post
    ,这会更安全。

    我以前通过创建自动重定向到第一个子级的页面模板来完成这项工作。也许你可以用这个。在模板目录中添加一个名为tofirstchild.php的文件,并将其放入其中:

    <?php
    
    /*
    Template Name: Redirect To First Child
    */
    
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            $pagekids = get_pages( "post_parent=" . $post->ID . "&sort_column=menu_order" );
            $firstchild = $pagekids[0];
            wp_redirect( get_permalink( $firstchild->ID ) );
        }
    }
    
    ?>
    
    
    

    然后在管理区域,选择“关于我们”页面的“重定向到第一个孩子”模板。

    谢谢。没有问题,我只是想知道是否有可能将主页链接到第一个孩子。假设你有一个主导航“关于我们”,里面有一些关于我们的子导航,例如“我们是谁,团队,新闻”。我希望“关于我们”链接到“我们是谁”,而不是链接到一个关于我们的页面。如果你在我发布后看到这一点,代码中就有错误。现在更正了。