WordPress自定义分类PHP开发

类别提供了一种将相关博客文章组合在一起的有用方法。您可能想为博客中涵盖的不同主题创建类别。您可以使用类别将您的博客文章分类和分组到不同的部分。例如,如果您有涵盖特定音乐类型的文章,您可能希望将这些文章归为一个类别。

类别不仅有助于使帖子井井有条,而且您还可以使用类别在您网站的多个位置显示帖子,帮助访问者快速了解您网站的主题,并允许他们更快地浏览您的网站。类似于 WordPress 中标签的使用,人们通过类别搜索来查找相似的主题和主题。

您可以有父类别和子类别,使帖子的层次结构成为可能。此外,一篇文章可以放在多个类别中。这为您提供了很大的灵活性,可以准确显示您想要的帖子,完全按照您希望的方式在小部件、菜单中或直接在您的 WordPress 主题中显示。

在帖子中显示类别

可以通过某些方式显示类别。其中之一在帖子中。当您要阅读 WordPress 帖子时,您会看到该帖子属于某些类别(一个或多个)。要在 WordPress 的帖子循环中显示这些类别,您可以使用以下代码。

while ( have_posts() ) : the_post(); . . $categories = get_the_category(); foreach($categories as $category){ echo $category->name . wp_get_list_item_separator(); } . .endwhile;

输出

Category 1,Category 2,Category 3,

get_the_category  () 内置的 WordPress 函数返回 WP_Term,它是类别的 term_id、名称、slug、描述等详细信息列表。

  • term_id
  • 名称
  • 鼻涕虫
  • 术语组
  • term_taxonomy_id
  • 分类
  • 类别
  • 描述
  • 父母
  • 数数
  • 筛选
  • 猫ID
  • 类别计数
  • 类别_描述
  • 猫名
  • category_nicename
  • 类别_父母

如果你想在循环外使用这个函数,你必须将帖子 ID 作为参数传递。

get_the_category($post_id );

在此示例中,您必须自己控制分隔符。另一件事是类别名称没有链接且不可单击,最好改用 get_the_category_list() 函数。

在 WordPress 的循环中还有另一种简洁方便的方式来显示帖子类别。get_the_category_list() 函数显示带有额外选项的类别。

while ( have_posts() ) : the_post(); . . $categories_list = get_the_category_list( wp_get_list_item_separator() ); if ( $categories_list ) { printf( /* translators: %s: List of categories. */ '<span>' . esc_html__( 'Categorized as %s', 'textdomain' ) . ' </span>', $categories_list // phpcs:ignore WordPress.Security.EscapeOutput ); } . .endwhile;

输出

Categorized as Category 1,Category 2,Category 3

在此示例中,您可以显示 WordPress 类别列表名称,并且名称链接到类别列表显示部分。此外,您可以使用此方法显示 WordPress 自定义帖子类型类别。

如何在 WordPress 中创建类别模板存档页面

对于 WordPress 网站,通常为类别、标签、自定义帖子类型和分类法使用不同的模板存档页面。通过为类别创建模板,您可以将特定功能添加到类别存档页面。

例如,您可以允许用户订阅类别、添加类别图像、显示类别描述以及为每个类别选择不同的布局。

要创建和设计 WordPress 类别存档模板,您需要为该特定类别创建一个页面模板。转到主题的文件夹。从主题文件列表中,打开 category.php 文件,如果那里没有 category.php 文件,则创建一个。

对于类别,层次结构相当简单。例如,假设类别的 slug 是 新闻 ,类别 ID 是 6模板层次结构指定 WordPress 将使用它在当前主题目录中从以下列表中找到的第一个模板文件:

  1. 类别 slug.php
  2. 类别ID.php
  3. 类别.php
  4. 归档.php
  5. 索引.php

也就是说,如果您没有 category-slug.php(假设是 category-news.php),WordPress 将检查 category-ID.php(如 category-6.php),依此类推。

现在打开 category-news.php 文件并向其中添加以下代码。

<?phpget_header();$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );?><div> <div> <h1><?php echo apply_filters( 'the_title', $term->name ); ?> News</h1> <?php if ( !empty( $term->description ) ): ?> <div> <?php echo esc_html($term->description); ?> </div> <?php endif; ?> <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?> <div <?php post_class('post'); ?>> <h2><a href="<?php%20the_permalink();%20?>"><?php the_title(); ?></a></h2> <div> <p><?php the_time(get_option('date_format')); ?> by <?php the_author_posts_link(); ?></p> </div> <div> <?php the_content( __('Full story…') ); ?> </div> </div> <?php endwhile; ?> <?php else: ?> <h2>No Post Type in <?php echo apply_filters( 'the_title', $term->name ); ?></h2> <div> <div> <p>It seems there isn't anything happening in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, something is bound to happen soon.</p> </div> </div> <?php endif; ?> </div><?php get_footer(); ?>

这是用于显示类别帖子列表和存档的 WordPress 类别模板示例。

如何以编程方式创建 WordPress 类别

如果你想通过代码在 WordPress 中创建一个类别,你可以简单地使用 wp_insert_term() 函数,如下所示。

wp_insert_term( // the name of the category 'Category A', // the taxonomy, which in this case if category (don't change) 'category', array( 'slug' => 'category-a', ));

如果您想在 WordPress 中为您的自定义帖子类型创建一个类别,只需将“类别”更改为您的帖子类型类别 slug。在这种情况下是“hscategory”。

wp_insert_term( // the name of the category 'Category A', // the taxonomy, which in this case if category (don't change) 'hscategory', array( 'slug' => 'category-a', ));

要为父类别创建子类别,您需要稍微更改代码。

wp_insert_term( // the name of the sub-category 'Sub-category 1', // the taxonomy 'category' (don't change) 'category', array( 'slug' => 'sub-cat-1', // link with main category. In the case, become a child of the "Category A" parent 'parent'=> term_exists( 'Category A', 'category' )['term_id'] ));

对于自定义帖子类型类别

wp_insert_term( // the name of the sub-category 'Sub-category 1', // the taxonomy 'category' (don't change) 'hscategory', array( 'slug' => 'sub-cat-1', // link with main category. In the case, become a child of the "Category A" parent 'parent'=> term_exists( 'Category A', 'hscategory' )['term_id'] ));

如何在 WordPress 中以编程方式删除类别

要按代码删除类别,请使用以下代码。

$categ_ID = 204;if ( wp_delete_category( $categ_ID ) ) { echo "Category #$categ_ID was successfully deleted";} else { echo "Category #$categ_ID deletion failed";}

wp_delete_category() WordPress 内置函数用于删除类别。

通过is_category()函数判断页面是否为分类页面

is_category();// When any Category archive page is being displayed. is_category( '204' );// When the archive page for Category 204 is being displayed.is_category( 'Category One' );// When the archive page for the Category with Name "Category One" is being displayed.is_category( 'category-slug' );// When the archive page for the Category with Category Slug "category-slug" is being displayed.is_category( array( 204, 'category-slug', 'Category One' ) );// Returns true when the category of posts being displayed is either term_ID 204,// or slug "category-slug", or name "Category One".// Note: the array ability was added in version 2.5.

相关文章