wordpress调用指定类型post_type的文章

  wordpress很强大,可以添加多种post_type文章类型,假如我们要调用product产品模型的文章要如何操作呢?随ytkah一起来看看吧。我们用'post_type' => 'product'进行指定,代码如下

<?php
                $args = array(
                    'post_type' => 'product',//自定义文章类型名称
                    'showposts' => 5,//输出的文章数量,这个可以是缺省值,不用设置
                    'orderby' => 'rand',//按随机调用,如果不要随机可以把这行删除
                );
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {
                    while ($my_query->have_posts()) : $my_query->the_post();?>
                        <div class="item col-xs-12 col-sm-4 col-md-3">
                            <div class="box">
                                <img src="<?php the_field('pimg01'); ?>" alt="<?php the_title(); ?>">
                                <div class="text">
                                    <b><?php the_title(); ?></b>
                                    <?php if (get_field('model')): ?>
                                        <p><?php the_field('model'); ?></p>
                                    <?php endif; ?>
                                    <?php if (get_field('be_applicable')): ?>
                                        <p><?php the_field('be_applicable'); ?></p>
                                    <?php endif; ?>
                                    <a href="<?php the_permalink(); ?>" class="common-btn">more</a>
                                </div>
                            </div>
                        </div>
                    <?php endwhile; wp_reset_query(); //重置query查询
                } ?>

  可以使用的排序方法有

'orderby' => 'date',                //按发布日期排序
'orderby' => 'modified',            //按修改时间排序
'orderby' => 'ID',              //按文章ID排序
'orderby' => 'comment_count',           //按评论最多排序
'orderby' => 'title',               //按标题排序
'orderby' => 'rand',                //随机排序
'order' => 'desc',           // 降序(递减,由大到小)

  有相同需求的朋友可以试一下

原文地址:https://www.cnblogs.com/ytkah/p/11846924.html