WordPress获取自定义文章类型所有分类法文章循环

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

在之前的文章里,悟空搜详细讲解过WordPress自定义文章类型(PostType),并且有过使用自定义文章类型实现网址导航案例,有心的朋友可以再温故下前面这两篇文章:

[xx_insert_post station_article=”1165″] [xx_insert_post station_article=”1210″]

接下来这篇WordPress教程悟空搜将会详细讲解如何获取自定义文章类型下所有分类法和文章循环。

话不多说,直接贴上功能代码:

<?php
	$args=array(
		'taxonomy' => 'sitecat',
		'hide_empty'=>'0',
		'hierarchical'=>1,
		'parent'=>'0',
	);
	$categories=get_categories($args);
	foreach($categories as $category){
		$cat_id = $category->term_id;
?>
//第一个循环为获取所有自定义文章类型的分类法。$cat_id

<?php
	$salong_posts = new WP_Query(
			array(
			'post_type' => 'site',//自定义文章类型,这里为site
			'ignore_sticky_posts' => 1,//忽略置顶文章
			'posts_per_page' => 8,//显示的文章数量
			'tax_query' => array(
				array(
					'taxonomy' => 'sitecat',//自定义文章类型的分类法名称
					'field'    => 'id',
					'terms'    => $cat_id,//分类法ID
				)
			),
		)
	);
	if ($salong_posts->have_posts()): while ($salong_posts->have_posts()): $salong_posts->the_post(); 
?>
//循环内容
<?php the_title(); ?>
	
<?php endwhile; endif; ?>
//第二个循环为获取所有分类法的文章循环

<?php }?>



使用方法

上面代码能够获取到你已经创建的自定义文章类型、分类法和分类法的所有文章,只需要新建一个自定义文章类型的分类模板即可使用!

新建一个自定义文章类型的分类模板可以参考下面这篇文章:

[xx_insert_post station_article="1262"]

扫码关注wp悟空搜

精选优质免费WordPress主题模板,分享最新WordPress实用建站教程!

记住我们的网址:ztJun.com

Leave a Comment