【问题标题】:Wordpress Child Pages in Alphabetical Order按字母顺序排列的 Wordpress 子页面
【发布时间】:2014-01-14 19:54:28
【问题描述】:

我正在使用一段特定的代码在父页面上显示子页面名称、特色图片和摘录。此代码工作正常,但我一直在尝试修改它以按字母顺序显示子帖子但没有成功。

我尝试将 ORDER BY menu_order 更改为 ORDER BY 标题或 ORDER BY 名称,但它破坏了页面。

如果有人有一些提示或提示,您的建议将不胜感激!

<?php

    $pageChildren = $wpdb->get_results("
        SELECT *
        FROM $wpdb->posts
        WHERE post_parent = ".$post->ID."
        AND post_type = 'page'
        ORDER BY menu_order
    ", 'OBJECT');

    if ( $pageChildren ) :
        foreach ( $pageChildren as $pageChild ) :
            setup_postdata( $pageChild );
            ?>
            <?php $imagethumb = get_post_meta($pageChild->ID, 'image-thumb', $single = true); ?>

<div id="solutionbutton">
<div id="centerbutton">
        <img src="<?php echo $imagethumb; ?>" />
        <h4><?php print '<a href="' . get_permalink( $pageChild->ID ) . '">' . get_the_title( $pageChild->ID ) . '</a>'; ?></h4>
        </div>
        <div id="entry">
            <?php echo excerpt(13); ?>
</br></br>
            <a href="<?php echo get_permalink($pageChild->ID); ?>">Read More...</a>
        </div>
</div>
        <?php
        endforeach;
    endif;
    ?>

            </div>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试这个:

    $pageChildren = $wpdb->get_results("
        SELECT *
        FROM $wpdb->posts
        WHERE post_parent = ".$post->ID."
        AND post_type = 'page'
        ORDER BY post_title
    ", 'OBJECT');
    

    它用于页面。但是是否有指定 post 父级的功能?如果是这样,请将post_type = 'page' 更改为post_type = 'post'

    顺便说一句,您的代码可以使用 WordPress 内置函数重构,如下所示:

    $pageChildren = get_posts(array(
        'post_type'=>'page', 
        'nopaging'=>true, 
        'orderby'=>'menu_order'
    ));
    

    【讨论】:

    • 我使用了你的第一个解决方案,效果很好!
    【解决方案2】:
    **Try the code.....**
    
    $args = array(
        'orderby' => 'title',
        'order'=> 'DESC',
        'post_parent' => $post->ID,
        'post_type' => 'page'
        );
    
    $get_childrens = get_children($args);
    print_r($get_childrens);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2012-04-13
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多