外贸建站资讯分享

WordPress怎么调用自定义文章循环类型下的所属自定义分类

阅读数:2453 时间:14/10/2022 来源:解决方案技术分享 标签:

您所在的位置:

WordPress给默认的文章功能赋予了分类目录(Categories)的功能:

也就是说文章Posts拥有Cagegories,而自定义文章类型默认是没有这个功能的。但WordPress给自定义文章开放了term(分类)这个功能,在后台的操作方式和Cagegory是一样的。每一种term都有一个名称,叫做taxonomy(翻译为分类法)。也可以把cagegory理解为一种名称叫做Cagegory的term。

下面是程序:

add_action( 'init', 'create_hotel_taxonomies', 0 );//Hotel自定义分类
function create_hotel_taxonomies() {
	register_taxonomy(
		'hotel_category', //后续要用到的分类法名称
		'hotels',
		array(
			'labels' => array(
				'name' => 'Hotel Location',
				'add_new_item' => 'Add New Hotel Location',
				'new_item_name' => "New Hotel Location"
			),
			'show_ui' => true,
			'show_tagcloud' => false,
			'hierarchical' => true
		)
	);
}

 

这段代码添加到functions.php中,刷新后台,就能看到在我的自定义文章类型Hotels下,有了一个叫做‘Hotel Location’的分类法

我的分类法在程序里面的name叫’hotel_category’,在后台显示出来的label name是‘Hotel Location’,在后续的开发中要用到的name只有’hotel_category’这一个。

为了管理方便,在后台的Hotels列表里面,我希望也能把这个分类法体现出来:

add_filter( 'manage_edit-hotels_columns', 'hotel_columns' );//在后台的Hotels列表中添加分类的列
function hotel_columns( $columns ) {
	$columns['hotel_category'] = 'Category'; //添加‘hotel_category’的列
	$columns['hotel_id'] = 'Hotel Id'; //把hotel的ID也作为一列添加
	return $columns;
}
add_action( 'manage_posts_custom_column', 'hotel_populate_columns' );//在后台的Hotels列表中添加分类列和ID列的数据
function hotel_populate_columns( $column ) {
	if ( 'hotel_category' == $column ) {
		$hotel_category =  get_the_term_list(get_the_ID(), 'hotel_category', '','<br/>') ;//显示所属自定义分类的list,用回车分隔
		echo $hotel_category;
	}
	if ( 'hotel_id' == $column ) {
		$hotel_id =  get_the_ID() ;
		echo $hotel_id;
	}
}

在后台添加几条hotel的数据,列表里显示出分类和ID这两列:

接下来需要在主题中调用这些分类了,主要有这几种方法:

1. 用get_posts()函数调用指定的自定义分类文章

<ul>
<?php
$myposts = get_posts( array( 
	'numberposts'=>50, 
	'post_type' => 'hotels', 
	'orderby' => 'post_title', 
	'order' => 'ASC',
	'hotel_category' => 'China' //在此直接设置分类法名称和值,get_posts()函数就会调用对应的文章内容
) );
foreach( $myposts as $post ) {
	$content = $post->post_content;
	if ( ! $content ) // Check for empty page
	continue;
	$content = apply_filters( 'the_content', $content );
?>
	<li><a href="<?php the_permalink(); ?>">
		<?php the_title(); ?><br/>
		<?php the_terms( $post->ID, 'hotel_category' ); ?>
	</a></li>
<?php }; ?>
</ul>

2. 用the_terms()函数调用当前文章所属的分类链接列表

the_terms()函数在上面已经用过,详细列举它的具体用法

the_terms( get_the_ID(), 'hotel_category' ); //调用指定ID文章的自定义分类列表
the_terms( $post->ID, 'hotel_category' ); //调用当前文章的自定义分类列表
the_terms( $post->ID, 'Hotel Location:', 'hotel_category: ', ' / ' ); //添加前缀,分隔符等,输出结果变成:Hotel Location: China / Thailand / Japan

the_terms()函数的官方文档:http://codex.wordpress.org/Function_Reference/the_terms

3. 用get_the_terms()函数调出当前文章的所有term信息

这个函数非常有用,有时候我们需要用到当前文章的自定义分类名称,ID,别名,描述,甚至于这个分类下有多少其他文章等

$terms=get_the_terms( $post->ID, 'hotel_category');
$termNames = array();
$termIDs = array();
$termSlugs = array();
$termDescs = array();
$termCounts = array();
$termStrings = array();
foreach ( $terms as $term ) {
	$termNames[] = $term->name; //把分类值的名称加入到数组
	$termIDs[] = $term->term_id; //把分类的ID加入到数组
	$termSlugs[] = $term->slug; //把分类的别名加入到数组
	$termDescs[] = $term->description; //把分类的描述加入到数组
	$termCounts[] = $term->count; //把分类的文章数量统计加入到数组
	$termStrings[] = $term->name."(".$term->count.")";
}				
$termString = join( ", ", $termStrings ); //把名称用逗号连起来组成这样的字符串:China(3), Thailand(1), Japan(2)

get_the_terms()函数的官方文档:http://codex.wordpress.org/Function_Reference/get_the_terms

4. 便捷的get_the_term_list(),直接输出带链接的分类列表

echo '<ul>';
echo get_the_term_list( $post->ID, 'hotel_category', '<li>', '', '</li>' );
echo '</ul>';

get_the_term_list()函数的官方文档:http://codex.wordpress.org/Function_Reference/get_the_term_list

5. 用get_terms()函数,在任何地方输出某个自定义分类的列表

$terms = get_terms( 'hotel_category' );
$termsCount = 0;
if ( $terms && !is_wp_error( $terms ) ) {
	echo '<ul class="archive-list">';
	foreach ( $terms as $term ) {
		echo '<li><a href="'.get_term_link( $term, $term->taxonomy ) . '">' . $term->name . '</a><span>('.$term->count.')</span></li>';
		$termsCount += $term->count;
	}
	echo '</ul>';
}

get_terms()函数的官方文档:http://codex.wordpress.org/Function_Reference/get_terms


关注我们
TOP
关闭

网站需求

    您的公司*

    您的姓名*

    您的手机*

    您的需求