wordpress调用指定类型post_type的文章

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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查询
                } ?>

  可以使用的排序方法有

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

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

posted @   ytkah  阅读(1047)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2014-11-13 微信卡券功能相关策略调整 类目库存等进行修改
2014-11-13 微信电话本可以正常服务了 还我美女!
2013-11-13 解决升级WordPress及插件需输入FTP账号的问题
2013-11-13 移除wordpress留言中自动链接功能
2013-11-13 怎样更改wordpress登陆 URL防止恶意注册
网址导航 gg N / G Sitemap

部分内容来源于网络,如有版权问题请联系删除

  
点击右上角即可分享
微信分享提示