主页/WordPress笔记/博客文章/综合文章/如何在 WordPress 中创建自定义文章类型存档页面

如何在 WordPress 中创建自定义文章类型存档页面

Bug编译狮

Bug编译狮

要在WordPress中创建自定义文章类型的存档页面,您首先需要理解几个基本概念和操作步骤。

1. 确定文章类型

在WordPress中,每个文章类型(如“博客”、“产品”或“服务”)都有其特定的结构和功能。要为新文章类型创建存档页面,您首先需要确定这个新的文章类型名称。例如,如果您想创建一个名为“事件”的文章类型,那么在这个例子中,“事件”就是您要创建的文章类型名称。

2. 创建文章类型

接下来,在WordPress后台,找到并点击“文章类型”选项卡,然后点击“添加新文章类型”。在这里,您可以输入文章类型的名称、描述以及任何其他属性。对于我们的例子,“事件”,我们只需要输入它的名称即可:

add_post_type_support( 'event', 'post-formats' );

这行代码会添加格式支持到文章类型“事件”。

3. 创建存档页面

一旦文章类型已经创建好,下一步就是在WordPress后台创建一个存档页面。返回到“文章类型”选项卡,选择您的新文章类型,然后点击“新建存档页面”。在此处,您可以在页面标题框中输入一个描述性名称,比如“所有事件”。

4. 编辑存档页面

编辑完成后,您可能会注意到存档页面上没有显示任何内容。这是因为默认情况下,WordPress不会自动加载文章类型的内容。为了使存档页面能够显示文章类型中的数据,我们需要编写一些代码来处理这一点。

示例代码:

以下是一段简单的PHP代码,用于从数据库中获取并显示所有文章类型的数据:

function get_event_posts() {
    $args = array(
        'post_type' => 'event',
        'posts_per_page' => -1,
    );

    $query = new WP_Query($args);

    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
        the_title('<h2>', '</h2>');
        the_content();
    endwhile; endif;

    wp_reset_postdata();
}
?>

<div class="archive">
    <?php get_event_posts(); ?>
</div>

这段代码的作用是:

  • 定义了一个函数get_event_posts(),该函数用于查询文章类型“事件”的所有帖子。
  • 使用了WP_Query类来执行查询。
  • 在循环中,它显示了每篇文章的标题和内容。
  • 最后,调用了wp_reset_postdata()以恢复原始的全局 $post 变量。

5. 测试存档页面

保存上述代码后,刷新浏览器,您应该能看到一个新的带有“所有事件”标签的存档页面。如果一切正常,您应该看到所有相关的文章类型的内容。

总结

通过以上步骤,您现在可以成功地在WordPress中创建并设置一个自定义文章类型的存档页面。这个过程不仅包括创建文章类型和存档页面,还包括了如何通过代码动态地展示这些数据。希望这些信息对您有所帮助!

黑板前的Bug语法讲师

黑板前的Bug语法讲师

课程目标

本课程旨在教授您如何使用 WordPress 创建自定义文章类型存档页面(也称为“文章类别”)。这个过程将帮助您更好地组织和管理您的网站内容。

教学大纲

  1. 理解文章类型:首先,我们需要了解什么是文章类型,它们是如何工作的以及为什么需要它们。
  2. 创建新文章类型:通过设置特定的文章格式来创建新的文章类型。
  3. 配置文章类型存档页:为每个文章类型设置一个存档页面以展示其内容。
  4. 优化文章类型存档页:学习如何根据实际需求调整存档页的设计、布局和显示方式。
  5. 发布并测试文章类型存档:最后,我们将演示如何发布并验证自定义文章类型的存档页面是否按预期工作。

演示示例代码

1. 理解文章类型

  • 定义文章类型:使用 add_action 函数来定义文章类型。
  • 分类名称:可以是任何字符串或数字组合,用于识别文章类型。
  • 文章格式:可以是标准的标题和正文格式,也可以是其他定制的格式。
function create_custom_post_type() {
    $labels = array(
        'name' => _x('Custom Post Type', 'Post Type General Name', 'theme'),
        'singular_name' => _x('Custom Post Type', 'Post Type Singular Name', 'theme'),
        'menu_name' => _x('Custom Post Types', 'Menu Name', 'theme'),
    );
    $args = array(
        'label' => __('Custom Post Type', 'theme'),
        'description' => __('Custom post type for storing custom content.', 'theme'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'has_archive' => false,
        'hierarchical' => false,
        'rewrite' => false,
        'supports' => array('title', 'editor', 'thumbnail'),
        'taxonomies' => array(),
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'menu_position' => null,
        'menu_icon' => 'dashicons-admin-post',
    );

    register_post_type( 'custom_post_type', $args );
}

add_action( 'init', 'create_custom_post_type' );

2. 创建新文章类型

  • 使用 register_post_type 函数来注册一个新的文章类型。
  • 在此示例中,我们创建了一个名为 “my-custom-type” 的文章类型。
function create_my_custom_type() {
    $labels = array(
        'name' => _x('My Custom Type', 'Post Type General Name', 'theme'),
        'singular_name' => _x('My Custom Type', 'Post Type Singular Name', 'theme'),
        'search_items' => 'Search My Custom Type',
        'all_items' => 'All My Custom Type',
        'parent_item' => 'Parent My Custom Type',
        'parent_item_colon' => 'Parent My Custom Type:',
        'edit_item' => 'Edit My Custom Type',
        'update_item' => 'Update My Custom Type',
        'add_new_item' => 'Add New My Custom Type',
        'new_item_name' => 'New My Custom Type Name',
    );

    $args = array(
        'label' => __('My Custom Type', 'theme'),
        'description' => __('Custom post type for storing custom content.', 'theme'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'has_archive' => false,
        'hierarchical' => false,
        'rewrite' => false,
        'supports' => array('title', 'editor', 'thumbnail'),
        'taxonomies' => array(),
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'menu_position' => null,
        'menu_icon' => 'dashicons-admin-post',
    );

    register_post_type( 'my-custom-type', $args );
}

add_action( 'init', 'create_my_custom_type' );

3. 配置文章类型存档页

  • 设置文章类型存档页的标题和描述。
  • 使用 query_posts 函数获取相关文章数据。
  • 根据文章类型选择合适的模板或样式。
function my_custom_type_archive_page() {
    $args = array(
        'posts_per_page' => -1, // 获取全部文章
        'category_name' => 'my-custom-type', // 根据文章类型筛选文章
    );

    $archive_query = new WP_Query($args);

    if ($archive_query->have_posts()) : // 当有文章时
        while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
            <div class="post">
                <h2><?php the_title(); ?></h2>
                <p><?php the_content(); ?></p>
            </div>
        <?php endwhile;
    endif;

    wp_reset_postdata();
}
add_action( 'template_redirect', 'my_custom_type_archive_page' );

4. 优化文章类型存档页

  • 根据实际需求调整文章类型存档页的布局和设计。
  • 可以添加CSS类或插件来增强存档页的功能性。
<style>
    .my-custom-type-archive-page .post {
        margin-bottom: 20px;
    }
    .my-custom-type-archive-page .post h2 {
        font-size: 20px;
    }
    .my-custom-type-archive-page .post p {
        font-size: 16px;
    }
</style>

5. 发布并测试文章类型存档

  • 在主题目录下找到自定义文章类型文件夹。
  • 进入相应文件夹并运行 wp fresh install 命令。
  • 尝试访问自定义文章类型的存档页面。

结语

本文介绍了如何在 WordPress 中创建自定义文章类型存档页面的基本步骤。通过上述示例代码,您可以轻松地创建自己的文章类型,并根据实际需求对其进行修改和扩展。希望这些建议对您有所帮助!