主页/WordPress笔记/博客文章/综合文章/如何在 WordPress 中显示当前分类标题、URL 及其他信息

如何在 WordPress 中显示当前分类标题、URL 及其他信息

Bug编译狮

Bug编译狮

要在 WordPress 网站上显示当前分类的标题、URL 以及其他相关信息,你可以使用一些内置的功能和插件来实现这一目标。以下是一种常见的方法:

步骤 1: 添加分类标签

首先,在 WordPress 的后台管理面板中添加一个自定义标签(Custom Post Type),用于存储与分类相关的数据。

使用示例代码:

// 插入到 functions.php 文件中
function add_custom_taxonomy() {
    register_taxonomy_for_object_type( 'category', 'custom-post-type-name' );
}
add_action('init', 'add_custom_taxonomy');

在这个例子中,我们注册了一个名为 custom-post-type-name 的自定义标签,其中 category 是分类。

步骤 2: 创建分类标签内容

接下来,创建一个模板文件来展示这个标签的内容。

示例模板文件 (single-custom-post-type.php):

<?php get_header(); ?>

<div class="container">
    <div class="row">
        <?php while ( have_posts() ) : the_post(); ?>
            <!-- 检查是否为分类页面 -->
            <?php if(is_category()): ?>
                <h1><?php the_title(); ?></h1>
                <p><a href="<?php echo get_category_link(get_queried_object_id()); ?>"><?php echo get_the_category_list(', '); ?></a></p>
                <!-- 显示更多分类信息 -->
                <ul>
                    <?php $categories = get_the_category();
                        foreach ($categories as $category) { ?>
                            <li><?php echo $category->name; ?></li>
                    <?php } ?>
                </ul>
            <?php endif; ?>
        <?php endwhile; ?>
    </div>
</div>

<?php get_footer(); ?>

在这个模板文件中,我们检查了当前页面是否是分类页面 (is_category() 函数),如果是,则显示分类的标题和链接以及所有子分类的信息。

步骤 3: 在首页或特定页面显示分类信息

如果你希望在首页或其他特定页面显示这些分类信息,可以在相应页面的模板文件中调用上述代码。

示例:在首页 (index.php)

<?php get_header(); ?>
<main role="main" class="container">
    <section class="post-list">
        <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
            <?php if(is_category()): ?>
                <article class="post">
                    <h2><?php the_title(); ?></h2>
                    <p><a href="<?php echo get_category_link(get_queried_object_id()); ?>"><?php echo get_the_category_list(', '); ?></a></p>
                    <ul>
                        <?php $categories = get_the_category();
                            foreach ($categories as $category) { ?>
                                <li><?php echo $category->name; ?></li>
                        <?php } ?>
                    </ul>
                </article>
            <?php endif; ?>
        <?php endwhile; endif; ?>
    </section>
</main>
<?php get_footer(); ?>

总结

通过以上步骤,你可以在 WordPress 中动态地显示当前分类的标题、URL 以及相关子分类信息。这不仅增强了用户体验,还帮助用户更好地了解网站的内容结构。记得根据实际需求调整代码以适应不同的页面布局和功能需求。

黑板前的Bug语法讲师

黑板前的Bug语法讲师

教案大纲

  1. 基础介绍
    • 简单介绍 WordPress 和其核心功能。
  2. 分类管理器
    • 如何创建和管理 WordPress 的分类。
  3. 使用分类标签
    • 分类标签的功能及其使用方法。
  4. 显示分类信息
    • 如何在 WordPress 页面上显示分类标题、URL 以及其它相关信息。

课程目标

  • 学习如何在 WordPress 中设置分类并显示它们的信息。
  • 掌握使用分类标签来美化页面设计的能力。
  • 能够根据需要自定义显示分类信息的方式。

讲解示例代码

1. 基础介绍

<?php
/*
Plugin Name: Category Management
Description: This plugin allows you to manage your WordPress categories.
Version: 1.0
Author: Your Name
*/

// 注册分类标签
function register_category() {
    $labels = array(
        'name' => _x('Categories', 'category name'),
        'singular_name' => _x('Category', 'category singular name')
    );

    $args = array(
        'label' => __('Categories', 'your-plugin-labels'),
        'description' => __('List of all your categories.', 'your-plugin-description'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_icon' => 'dashicons-category',
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'has_archive' => true,
        'query_var' => true,
        'supports' => array('title','editor','thumbnail')
    );

    register_post_type( 'category', $args );
}

add_action( 'init', 'register_category' );

2. 分类管理器

<?php
/**
 * Displays a list of the category tree
 */
function display_categories_tree() {

    global $wp_query;

    if ( ! empty( $wp_query->query_vars['cat'] ) ) { // Only show categories with a specified ID in the query
        $categories = get_categories( array( 'hide_empty' => 0, 'orderby' => 'name', 'order' => 'ASC' ) );

        foreach ( $categories as $category ) {
            echo '<li class="list-group-item"><a href="' . esc_url( get_term_link( $category ) ) . '">' . esc_html( $category->name ) . '</a></li>';
        }
    } else {
        echo '<div class="alert alert-info">' . __( 'Please select a category from the drop-down menu or use the search bar to find it.', 'your-plugin-name' ) . '</div>';
    }

}

3. 使用分类标签

<div id="categories-tree">
    <?php display_categories_tree(); ?>
</div>

4. 显示分类信息

<li class="list-group-item"><a href="<?php echo esc_url(get_term_link($term)); ?>"><?php echo esc_html($term->name); ?></a></li>

实战应用

  1. 登录到您的 WordPress 主题网站
  2. 创建一个新的主题文件夹,命名为 “mytheme”
  3. 将上述代码复制粘贴到 “mytheme” 文件夹下的 functions.php 文件中
  4. 保存更改并激活主题

通过以上步骤,您可以在 WordPress 中轻松地添加、管理和展示分类信息了。