繁体   English   中英

WordPress排序自定义帖子类型

[英]Wordpress sort custom post type

我有一个自定义帖子类型,它的全部内容都上下颠倒显示,这意味着第一个条目在我的实际首页上最后显示-有没有办法改变排序顺序?

我的代码如下:

        <?php global $post; ?>

        <ul class="menu-items">
            <?php
            $menuloop = new WP_Query(array(
                'posts_per_page' => -1,
                'post_type'      => 'menu',
                'tax_query'      => array(
                    // Note: tax_query expects an array of arrays!
                    array(
                        'taxonomy' => 'menu_type', // my guess
                        'field'    => 'slug',
                        'terms'    => $menuname_category
                    )
                ),
            ));

            ?>
            <?php if ( have_posts() ) : while ( $menuloop->have_posts() ) : $menuloop->the_post(); ?>
            <li>
                    <div class="grid2column"><?php the_title(); ?></div>
                    <div class="grid2column lastcolumn"><?php if(get_post_meta($post->ID, 'menuoption_menu_pricing', true)): ?><?php echo get_post_meta($post->ID, 'menuoption_menu_pricing', true) ?><?php endif; ?></div>
                    <div class="clearfix"></div>
                    <div class="item-description-menu"><?php echo get_the_excerpt(); ?></div>
            </li>
            <?php endwhile; ?>
            <?php endif; ?>
        </ul>

我想您在问题中给出的代码中有错误。 posts_per_page => -1

为什么会为-1 要显示的内容,它必须是一个数字> 0

为了进行排序,您需要将这两个参数添加到数组中

array ( 'orderby' => 'date', 'order' => 'DESC'

基本上,您可以将DESC更改为ASC以颠倒顺序。

最终数组

$menuloop = new WP_Query(array(
                'posts_per_page' => 10,
                'post_type'      => 'menu',
                'orderby'        => 'date', // new arg
                'order'          => 'DESC', // new arg
                'tax_query'      => array(
                    // Note: tax_query expects an array of arrays!
                    array(
                        'taxonomy' => 'menu_type', // my guess
                        'field'    => 'slug',
                        'terms'    => $menuname_category
                    )
                ),
            ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM