如何使用自定义字段而不是wp_list_pages返回的页面标题?

问题描述 投票:0回答:2

当页面有子页面时,我正在尝试使用侧边菜单进行导航。我目前所拥有的几乎是完美的,但我没有使用菜单中子页面的标题,而是想使用自定义字段'sidebar_title'。

我正在运行我发现的这个功能:

function wpb_list_child_pages() { 

    global $post; 

    if ( is_page() && $post->post_parent )
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    else
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
    if ( $childpages ) {
        $string = '
        <nav class="sidenav">
               <ul>
                   <li><a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a></li>'
                   .$childpages.
               '</ul>
        </nav>';
    }
    return $string;
}

这给了我这个结果:

<nav class="sidenav">
  <ul>
    <li><a href="page URL">Parent Page</a></li>
    <li><a href="page URL">Child Page</a></li>
    <li><a href="page URL">Child Page</a></li>
  </ul>
</nav>

我只需要知道如何用自定义字段替换子页面的标题。

wordpress custom-fields
2个回答
0
投票

您需要使用get_pages函数,以便您可以控制布局。您现在使用的功能是wp_list_pages,它基于get_pages,因此您无需更改主要请求中的任何内容。所以你的完整代码将如下所示:

$childpages = get_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

if ( $childpages ) {
   $string = '<nav class="sidenav"><ul><li><a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a></li>'

   foreach( $childpages as $page ) {
      $string .= '<li><a href="' . get_permalink($page->ID) . '">' . get_post_meta($page->ID, 'sidebar_title', true) . '</a></li>';
   }

   $string .= '</ul></nav>';

   return $string;
}

0
投票

这似乎已经成功了。

function wpb_list_child_pages() { 

    global $post; 

    if ( is_page() && $post->post_parent )
        $childpages = get_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    else
        $childpages = get_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

    if ( $childpages ) {
        $string = '<nav class="sidenav"><ul><li><a href="'.get_permalink($post->post_parent).'">'.get_field(sidebar_title, ($post->post_parent)).'</a></li>';

        foreach( $childpages as $page ) {
        $string .= '<li><a href="' . get_permalink($page->ID) . '">' . get_post_meta($page->ID, 'sidebar_title', true) . '</a></li>';
    }

    $string .= '</ul></nav>';

    return $string;
}}
© www.soinside.com 2019 - 2024. All rights reserved.