【问题标题】:wp_list_pages with afc (advanced custom fields)wp_list_pages 和 afc(高级自定义字段)
【发布时间】:2013-06-29 20:17:34
【问题描述】:

除了 afc 之外,我还会在侧边栏中使用 wp_list_pages。

列表页面的常规输出是这样的

<ul>
 <li>page 1</<li>
 <li>page 2</<li>
<ul>
 <li>page 2.1</<li>
 <li>page 2.2</<li>
 <li>page 2.3</<li>
</ul>
 <li>page 3</<li>
 <li>page 4</<li>
 <li>page 5</<li>
 <li>page 6</<li>
</ul>

有些页面有自定义字段,我想要这样的东西

<ul>
 <li>page 1</<li>
 <li>page 2 <span> - **custom field info**</span></<li>
<ul>
 <li>page 2.1</<li>
 <li>page 2.2<span> - custom field info</span></<li>
 <li>page 2.3</<li>
</ul>
 <li>page 3</<li>
 <li>page 4<span> - custom field info</span></<li>
 <li>page 5</<li>
 <li>page 6</<li>
</ul>

我尝试了常规的 wordpress 自定义字段:

wp_list_pages("title_li=".$post->ID."&meta_key=key");

但这会过滤并仅向我显示带有键的页面,而不是其他页面。 我该如何解决这个小问题?任何(其他)想法?

谢谢

【问题讨论】:

    标签: wordpress custom-fields advanced-custom-fields


    【解决方案1】:

    你需要这样做:

    $args = array(
        'post_type' => 'page',
    );
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    
        echo '<ul>';
    
        while ( $query->have_posts() ) {
    
            $query->the_post();
    
            if ( get_field( "field_name" ) ) {
                $custom_field_value = ' <span>' . get_field( "field_name" ) . '</span>';
            } else {
                $custom_field_value = '';
            }
    
            echo '<li>' . get_the_title() . $custom_field_value . '</li>';
        }
    
        echo '</ul>';
    
    } else {
        // no posts found
    }
    
    /* Restore original Post Data */
    wp_reset_postdata();
    

    【讨论】:

      猜你喜欢
      • 2017-12-16
      • 1970-01-01
      • 2013-07-14
      • 2017-08-28
      • 2018-08-06
      • 2013-11-25
      • 2012-08-10
      • 2019-11-07
      • 2016-01-20
      相关资源
      最近更新 更多