PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

按第二个分类法的术语列出分类目录:Taxonomy Archives

WBOY
WBOY 原创
2023-09-01 11:06:01 351浏览

按第二个分类法的术语列出分类目录:Taxonomy Archives

如果您的网站使用多个分类法对信息进行分类,则根据第二个分类法的术语拆分分类法存档中的帖子会很有帮助。

在本教程中,我将向您展示如何创建分类存档来实现此目的。

如果您曾经使用过多关系数据库,您就会知道它们的一大功能是您可以根据多种分类法查看数据。例如,如果您有一个客户数据库,您可以轻松查看哪些客户雇用您从事不同类别的项目,然后通过查看哪些网页设计客户(例如位于给定位置)来进一步排序。

当我第一次开始使用 WordPress 时,我感到很沮丧,因为您无法轻松做到这一点 - 至少您无法通过运行默认主题的开箱即用 WordPress 安装来做到这一点。

但是,可以通过多种分类法对数据进行分类。在本教程中。我将向您展示如何创建一个分类页面来列出该分类中的帖子,并按也适用于它们的另一个分类的术语排序。

然后,我将为第二个分类创建第二个分类存档,其中按照第一个分类的术语顺序列出其帖子(相信我 - 当您看到它发生时,它会更有意义!)

您需要什么

要完成本教程,您需要:

  • WordPress 的开发安装
  • 代码编辑器

1。创建主题

在本教程中,我将使用两个新模板文件、一个样式表和一个函数文件创建一个 24 的子主题。如果您使用自己的主题,只需将代码从我的函数文件复制到您主题的函数文件中,然后添加经过调整的模板文件以反映您主题的标记。

为了创建我的主题,我在空主题文件夹中创建一个名为 style.css 的文件,并使用以下内容填充它:

/*
Theme Name:     WPTutsPlus Create a Taxonomy Archive to List Posts by a Second Taxonomy's Terms
Theme URI:      https://rachelmccollin.co.uk/wptutsplus-taxonomy-archive-list-by-second-taxonomy/
Description:    Theme to support WPTutsPlus tutorial on creating a custom taxonomy archive. Child theme for the Twenty Fourteen theme.
Author:         Rachel McCollin
Author URI:     http://rachelmccollin.co.uk/
Template:       twentyfourteen
Version:        1.0
*/

@import url("../twentyfourteen/style.css");

这将创建我的子主题。

2。注册帖子类型和分类法

在本教程中,我将使用我在创建教程中使用的相同 'animals' 帖子类型和 'animal_cat' 分类法自定义帖子类型模板。我还将添加第二个分类法,名为 'habitat'

为此,我创建一个名为 functions.php 的新文件。首先,我添加函数来注册我的帖子类型:

<?php
// register a custom post type called 'animals'
function wptp_create_post_type() {
    $labels = array( 
		'name' => __( 'Animals' ),
		'singular_name' => __( 'animal' ),
		'add_new' => __( 'New animal' ),
		'add_new_item' => __( 'Add New animal' ),
		'edit_item' => __( 'Edit animal' ),
		'new_item' => __( 'New animal' ),
		'view_item' => __( 'View animal' ),
		'search_items' => __( 'Search animals' ),
		'not_found' =>  __( 'No animals Found' ),
		'not_found_in_trash' => __( 'No animals found in Trash' ),
	);
	$args = array(
		'labels' => $labels,
		'has_archive' => true,
		'public' => true,
		'hierarchical' => false,
		'supports' => array(
			'title', 
			'editor', 
			'excerpt', 
			'custom-fields', 
			'thumbnail',
			'page-attributes'
		),
		'taxonomies' => array( 'post_tag', 'category'), 
	);
	register_post_type( 'animal', $args );
} 
add_action( 'init', 'wptp_create_post_type' );
?>

然后我在一个函数中注册我的两个分类法:

<?php
// register taxonomies
function wptp_register_taxonomies() {
    
	// register a taxonomy called 'Animal Family'
	register_taxonomy( 'animal_cat', 'animal',
		array(
			'labels' => array(
				'name'              => 'Animal Families',
				'singular_name'     => 'Animal Family',
				'search_items'      => 'Search Animal Families',
				'all_items'         => 'All Animal Families',
				'edit_item'         => 'Edit Animal Families',
				'update_item'       => 'Update Animal Family',
				'add_new_item'      => 'Add New Animal Family',
				'new_item_name'     => 'New Animal Family Name',
				'menu_name'         => 'Animal Family',
			),
			'hierarchical' => true,
			'sort' => true,
			'args' => array( 'orderby' => 'term_order' ),
			'rewrite' => array( 'slug' => 'animal-family' ),
			'show_admin_column' => true
		)
	);
	
	// register a taxonomy called 'Habitat'
	register_taxonomy( 'habitat', 'animal',
		array(
			'labels' => array(
				'name'              => 'Habitats',
				'singular_name'     => 'Habitat',
				'search_items'      => 'Search Habitats',
				'all_items'         => 'All Habitats',
				'edit_item'         => 'Edit Habitat',
				'update_item'       => 'Update Habitat',
				'add_new_item'      => 'Add New Habitat',
				'new_item_name'     => 'New Habitat Name',
				'menu_name'         => 'Habitat',
			),
			'hierarchical' => true,
			'sort' => true,
			'args' => array( 'orderby' => 'term_order' ),
			'show_admin_column' => true
		)
	);
}
add_action( 'init', 'wptp_register_taxonomies' );
?>

这将创建 'animal' 帖子类型以及适用于它的两个分类法。请注意,我使用了 'show_admin_column' 来让管理我的帖子时变得更轻松。

添加一些数据并根据我的分类法对动物进行分类后,我现在可以在 WordPress 仪表板中查看我的数据,如下所示。

注意:我对所使用的动物的分类不是很科学 - 请不要评论我对栖息地或科系的理解!

按第二个分类法的术语列出分类目录:Taxonomy Archives

3.创建第一个分类模板文件

下一步是为 'animal_cat' 分类存档创建模板文件。在主题文件夹中创建一个文件并将其命名为 taxonomy-animal_cat.php。现在从您的主题添加包装器代码(我已从我的父主题复制了此代码,如果您使用自己的主题,您的代码将会有所不同):

<?php
/*
WpTutsPlus tutorial for creating archive to display posts by mutiple taxonomy terms
Archive template for animal_cat taxonomy
*/
?>

<?php get_header(); ?>

    <div id="main-content" class="main-content">

	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">

			
		</div><!-- #content -->
	</div><!-- #primary -->
	<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();

现在需要向此模板文件添加一些数据。

识别当前查询的术语

存档模板将使用 WP_Query 为每个术语创建自定义查询。查询的对象之一将是当前显示的分类术语,因此您需要识别它并将其存储为变量。

get_header() 行下方,添加:

<?php
// get the currently queried taxonomy term, for use later in the template file
$animalcat = get_queried_object();
?>

稍后您可以使用该 $animalcat 变量。

输出页面标题

存档当前没有主标题,因此您需要使用刚刚定义的变量添加一个主标题。

在打开

标签后,添加以下内容:
<header class="archive-header">
    <h1 class="archive-title">
		<?php echo $animalcat->name; ?>
	</h1>
</header><!-- .archive-header -->

获取第二个分类的术语

接下来,您需要获取第二个分类的术语列表。在您刚刚添加的代码下方插入以下内容:

<?php //start by fetching the terms for the animal_cat taxonomy
$terms = get_terms( 'habitat', array(
    'hide_empty' => 0
) );
?>

这将获取所有术语的列表并将其存储在数组中。通过使用 'hide_empty' 您可以避免显示任何空术语 - 但正如您很快就会看到的,这只会阻止查询根本没有帖子的术语,而不是那些没有帖子的术语当前查询的分类术语。

创建循环

现在创建一个将为每个术语运行的循环:

<?php
// now run a query for each animal family
foreach ( $terms as $term ) {

    // Define the query
	$args = array(
		'post_type' => 'animal',
		'animal_cat' => $animalcat->slug,
		'habitat' => $term->slug
	);
	$query = new WP_Query( $args );
			
	// output the term name in a heading tag				
	echo'<h2>' . $term->name . ' habitat</h2>';
	
	// output the post titles in a list
	echo '<ul>';
	
		// Start the Loop
		while ( $query->have_posts() ) : $query->the_post(); ?>

		<li class="animal-listing" id="post-<?php the_ID(); ?>">
			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
		</li>
		
		<?php endwhile;
	
	echo '</ul>';
	
	// use reset postdata to restore orginal query
	wp_reset_postdata();

} ?>

对此的一些说明:

  • 为每个术语定义一个新查询。
  • 查询的参数包括第二个分类中的术语 ($term) 和当前正在查询的术语 ($animalcat)。
  • 如果您的分类法仅适用于一种帖子类型,您可以省略 'post_type' 参数,但我更愿意包含它以防万一。
  • $term 变量用于使用 $term->name 输出每个部分的标题。

现在保存您的模板文件并查看您的动物家族术语之一的分类存档:

按第二个分类法的术语列出分类目录:Taxonomy Archives

为每个查询添加帖子检查

目前,如您所见,模板正在输出空列表。通过检查每个查询是否有帖子可以轻松解决此问题。

在循环中包含以下内容:

if ( $query->have_posts() ) {

}

你的循环现在看起来像这样:

if ( $query->have_posts() ) {
    
	// output the term name in a heading tag				
	echo'<h2>' . $term->name . ' habitat</h2>';
	
	// output the post titles in a list
	echo '<ul>';
	
		// Start the Loop
		while ( $query->have_posts() ) : $query->the_post(); ?>

		<li class="animal-listing" id="post-<?php the_ID(); ?>">
			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
		</li>
		
		<?php endwhile;
	
	echo '</ul>';
}

如果查询没有任何帖子,这会阻止 WordPress 运行循环,从而删除那些空标题。现在我的存档页面如下所示:

按第二个分类法的术语列出分类目录:Taxonomy Archives

好多了!

为第二个分类创建模板文件

最后一步是为第二个分类的档案创建模板文件。

复制您的第一个模板文件并将其重命名为 taxonomy-habitat.php。编辑它以使术语正确。我需要对文件进行的编辑是:

  • 调整文件顶部的注释,使其准确
  • $animalcat 变量的名称更改为 $habitat (您可以通过为该变量指定一个更通用的名称来避免此问题 - 但不要将其称为 $term 因为您在其他地方使用它)
  • 编辑

    标题,以便它使用 $habitat 变量来输出当前查询术语的名称(我还在此处添加了一些解释性文本)这是可选的)
  • 更改 get_terms() 函数的第一个参数,使其使用 animal_cat 术语,而不是 habitat 术语。
  • 编辑查询参数,本质上是交换 'animal_cat''habitat' 的值。
  • 编辑循环中的

    内容以引用家庭而不是栖息地。

这意味着我的新模板文件如下所示:

<?php
/*
WpTutsPlus tutorial for creating archive to display posts by mutiple taxonomy terms
Archive template for habitat taxonomy
*/
?>

<?php get_header(); ?>

<?php
// get the currently queried taxonomy term, for use later in the template file
$habitat = get_queried_object();
?>

    <div id="main-content" class="main-content">

	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">

<header class="archive-header">
	<h1 class="archive-title">
		Habitat - <?php echo $habitat->name; ?>
	</h1>
</header><!-- .archive-header -->

<?php //start by fetching the terms for the animal_cat taxonomy
$terms = get_terms( 'animal_cat', array(
	'hide_empty' => 0
) );
?>

<?php
// now run a query for each animal family
foreach( $terms as $term ) {

	// Define the query
	$args = array(
		'post_type' => 'animal',
		'animal_cat' => $term->slug,
		'habitat' => $habitat->slug
	);
	$query = new WP_Query( $args );
	
	if( $query->have_posts() ) {
		
		// output the term name in a heading tag				
		echo'<h2>' . $term->name . ' family</h2>';
		
		// output the post titles in a list
		echo '<ul>';
		
			// Start the Loop
			while ( $query->have_posts() ) : $query->the_post(); ?>
	
			<li class="animal-listing" id="post-<?php the_ID(); ?>">
				<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
			</li>
			
			<?php endwhile;
		
		echo '</ul>';
	}
			
	
	
	// use reset postdata to restore orginal query
	wp_reset_postdata();

} ?>
			
		</div><!-- #content -->
	</div><!-- #primary -->
	<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();

进行这些更改后,保存新模板文件并检查您的存档之一:

按第二个分类法的术语列出分类目录:Taxonomy Archives

您现在拥有第二个分类的页面,其工作方式与第一个分类相同。

摘要

在本教程中,您学习了一种使用多种分类法显示数据的方法。您可以通过以下两种方式之一使用第三种分类法来进一步实现这一点:

  1. 重复获取术语、定义查询并为第二个术语之后的第三个术语运行循环的过程,以便您拥有两个单独的列表。
  2. 使用所有三个术语合并您的数据,方法是使用每个 $term 变量,其方式与 $habitat$animalcat 变量类似并在现有的 foreach() 语句中添加额外的 foreach() 语句。然后,您需要考虑如何使用列表或网格来布局结果数据。

为什么不尝试一下呢?

以上就是按第二个分类法的术语列出分类目录:Taxonomy Archives的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。