wordpress调用相同文章形式的日志列表

WordPress教程 1498

前面博客吧分享了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
23
<?php 
//by 博客吧
	$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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php 
//by 博客吧
	$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; 
?>

精品推荐: