【问题标题】:WordPress Side Navigation with Parent Heading and Child Sub Pages带有父标题和子子页面的 WordPress 侧面导航
【发布时间】:2016-05-07 00:55:36
【问题描述】:

我在 WordPress 中有以下导航结构:

  Page
     Sub Page
        Child Page
        Child Page
        Child Page
  Page
     Sub Page
        Child Page
        Child Page
        Child Page
  Page
     Sub Page
        Child Page
        Child Page
        Child Page

在每个子页面和子页面上,我想在侧边栏中显示以下内容:

  Sub Page
        Child Page
        Child Page
        Child Page

我最接近的是来自here的以下代码:

<ul>
<?php
global $post;
$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );      
wp_list_pages( array(
       'title_li' => '',
       'child_of' => $current_page_parent,
       'depth' => '1' )
);
?>
</ul>

但是,在子页面上,它只显示所有子页面的列表(没有子页面),在子页面上,它显示该部分的子页面列表,但缺少父子页面标题。

如何修改以实现我想要的?

谢谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    是的,你是对的,这没有在子页面或子页面上显示父页面,因为你可以使用以下代码。

    <?php if ( $post->post_parent ) {
    $children = wp_list_pages( array(
        'title_li' => '',
        'child_of' => $post->post_parent,
        'echo'     => 0
    ) );
    $title = get_the_title( $post->post_parent ); } else {
    $children = wp_list_pages( array(
        'title_li' => '',
        'child_of' => $post->ID,
        'echo'     => 0
    ) );
    $title = get_the_title( $post->ID );} if ( $children ) : ?>
    <h2><?php echo $title; ?></h2>
    <ul>
        <?php echo $children; ?>
    </ul>
    

    【讨论】:

    • 看起来不错,但出现错误:解析错误:语法错误,第 17 行的 eval() 代码中出现意外 $end
    • 朋友,这不应该是我的代码错误,我已经测试过工作正常,如果您阅读了他们所说的关于我的代码中未使用的 $end 的错误,请查看您的代码。如果直到出现错误而不是更深入地告诉我。
    • 对不起,我已经测试了几次,仍然得到同样的错误。另外,如果我将代码粘贴到phptester.net,我会收到以下错误:FATAL ERROR syntax error, unexpected '&lt;', expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) on line number 14
    猜你喜欢
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多