wordpress相关文章调用相同文章形式内容
摘要:今天大挖在升级wordpress主题时,需要在文章里调用相同文章形式的文章,搜索到以下代码可以完美实现我的功能需求,即通...
今天大挖在升级wordpress主题时,需要在文章里调用相同文章形式的文章,搜索到以下代码可以完美实现我的功能需求,即通过get_posts()函数。
调用指定文章形式的日志列表:
比如我需要在一组模块里调用相同文章形式的内容,如视频及图像,可以选择下面的代码,通过文章形式来归类所有相同文章形式的内容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
$posts = get_posts(array(
'numberposts' => '10',
'post_type' => 'post',
'tax_query'=>array(
array(
'taxonomy'=>'post_format',
'field' => 'slug',
'terms' => array('post-format-image')
)
),
)
);
if($posts):
foreach($posts as $post):
?>
<li><a href="<?php the_permalink(); ?>" target="_blank" title="<?php the_title();?>"><?php the_title();?></a></li>
<?php
wp_reset_postdata();
endforeach;
endif;
?>
|
注意:代码中的post-format-image是要调用的文章列表,格式是post-format-{name},name是文章形式的名称,如:aside、image、video、quote、link、gallery、status、audio、chat。
1
|
文章页面调用相关文章形式使用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
$posts = get_posts(array(
'numberposts' => '10',
'post_type' => 'post',
'exclude'=>get_the_ID(),
'tax_query'=>array(
array(
'taxonomy'=>'post_format',
'field' => 'slug',
'terms' => array('post-format-image'.get_post_format())
)
),
)
);
if($posts):
foreach($posts as $post):
?>
<li><a href="<?php the_permalink(); ?>" target="_blank" title="<?php the_title();?>"><?php the_title();?></a></li>
<?php
wp_reset_postdata();
endforeach;
endif;
?>
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-04-12 使用IP SLA配置静态路由跟踪(基本)
2021-04-12 wordpress升级需设置ftp的解决方法
2021-04-12 用命令创建MySQL数据库
2021-04-12 MySQL创建用户与授权