跳到主要内容

您要在WordPress中创建唯一的类别页面布局吗?

对于WordPress网站,通常使用不同的模板来分类,标记,自定义帖子类型和分类法。

在本文中,我们将向您展示如何在WordPress中创建类别模板。

如何在wordpress中创建类别模板
如何在wordpress中创建类别模板

通过创建类别模板,可以在类别页面上添加特定功能。

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

为什么要在WordPress中创建类别模板

WordPress为您的所有类别生成单独的页面。您可以通过访问如下网址来查看它们:

https://example.com/category/news/

最流行的WordPress主题都带有内置模板来展示精美的类别页面。这些模板突出显示类别标题,并在其下方显示类别说明。

但是,某些主题可能无法很好地处理此问题,或者您可能要自定义类别页面。这是您需要在WordPress中创建类别模板的地方。

让我们看一下如何在WordPress中创建类别模板。

类别页面的WordPress模板层次结构

WordPress具有功能强大的模板系统,可让您为网站的不同部分创建不同的模板。

当显示任何页面时,WordPress会按预定义的层次结构顺序查找模板。

要显示类别页面,它将按以下顺序查找模板:category-slug.php→category-id.php→category.php→archive.php→index.php

首先,WordPress将使用类别标签来查找特定于该特定类别的模板。例如,category-design.php模板将用于显示“设计”类别。

如果找不到类别标签模板,则WordPress将查找具有类别ID的模板,例如category-6.php。之后,它将查找通用类别模板,通常为category.php。

如果不存在通用类别模板,则WordPress将寻找通用存档模板,例如archive.php。最后,它将使用index.php模板显示类别。

这是我们的WordPress模板层次结构指南。

在WordPress中为您的主题创建类别模板

首先,让我们看一个典型的category.php模板。

<?php
/**
* A Simple Category Template
*/

get_header(); ?> 

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

<?php 
// Check if there are any posts to display
if ( have_posts() ) : ?>

<header class="archive-header">
<h1 class="archive-title">Category: <?php single_cat_title( '', false ); ?></h1>


<?php
// Display optional category description
 if ( category_description() ) : ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
</header>

<?php

// The Loop
while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

<div class="entry">
<?php the_content(); ?>

 <p class="postmetadata"><?php
  comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments closed');
?></p>
</div>

<?php endwhile; 

else: ?>
<p>Sorry, no posts matched your criteria.</p>


<?php endif; ?>
</div>
</section>


<?php get_sidebar(); ?>
<?php get_footer(); ?>

现在,假设您有一个名为“ Design”的类别,其中类别标签为“ design”,并且您希望以与其他类别不同的​​方式显示该类别。

为此,您需要为该特定类别创建模板。转到外观»主题编辑器。在右侧的主题文件列表中,单击category.php,如果那里没有category.php文件,则查找archive.php

如果找不到这些模板中的任何一个,则很有可能您正在使用WordPress主题框架,本教程可能对您没有用。我们建议您参考所使用的​​特定框架。

如果找到上面的文件,请复制的所有内容category.php并将其粘贴到文本编辑器(如记事本)中。将此文件另存为category-design.php

使用FTP客户端连接到WordPress托管,然后转到/ wp-content / themes / your-current-theme /并将category-design.php文件上传到主题目录。

现在,您对此模板所做的任何更改将仅显示在该特定类别的存档页面中。

使用此技术,您可以根据需要创建多个类别的模板。只需使用category- {category-slug} .php作为文件名。您可以通过访问WordPress管理区域中的类别部分找到类别标签。

这是category-slug.php模板的示例。注意,我们使用了与category.php相同的模板,仅做了很少的更改。

由于我们已经知道将使用的类别,因此我们可以手动添加标题,描述或任何其他详细信息。另请注意,我们已使用<?php the_excerpt(); ?> 而不是<?php the_content(); ?>

找出为什么我们认为使用帖子摘要或摘录而不是完整帖子是一个好主意。

<?php
/**
* A Simple Category Template
*/

get_header(); ?> 

<section id="primary" class="site-content">
<div id="content" role="main">
<?php 
// Check if there are any posts to display
if ( have_posts() ) : ?>

<header class="archive-header">
<?php
// Since this template will only be used for Design category
// we can add category title and description manually.
// or even add images or change the layout
?>

<h1 class="archive-title">Design Articles</h1>
<div class="archive-meta">
Articles and tutorials about design and the web.
</div>
</header>

<?php

// The Loop
while ( have_posts() ) : the_post();
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

<div class="entry">
<?php the_excerpt(); ?>

 <p class="postmetadata"><?php
  comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments closed');
?></p>
</div>

<?php endwhile; // End Loop

else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
</section>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

如果您不想使用类别标签模板,则可以使用类别ID模板为特定类别ID创建模板。这是在WordPress中查找类别ID的方法。

对类别使用条件标签

在为主题创建模板时,您需要确定是否确实需要单独的模板来完成您想做的事情。

在某些情况下,您要进行的更改不太复杂,可以使用通用模板(例如category.php甚至archive.php)中的条件标签来实现。

WordPress支持许多主题作者可以在其模板中使用的条件标签。

一种这样的条件标签是is_category()。如果符合条件,则可以使用此条件标记更改模板以显示不同的输出。

例如,假设您有一个精选文章类别,称为“精选”。

现在,您想在类别存档页面上显示此特定类别的一些其他信息。为此,请在后面的category.php文件中添加此代码<?php if ( have_posts() ) : ?>

<header class="archive-header">

<?php if(is_category( 'Featured' )) : ?>
	<h1 class="archive-title">Featured Articles:</h1>
<?php  else: ?>
	<h1 class="archive-title">Category Archive: <?php single_cat_title(); ?> </h1>
<?php endif; ?>

</header>

使用Beaver Themer创建类别模板

Beaver Themer允许您为主题创建布局。您可以选择要使用模板的各个类别,然后使用拖放工具进行编辑。

首先,转到Beaver Builder »Themer Layouts»Add New页面。

您需要为其命名,然后在“位置”选项下选择类别。

从那里,您将能够使用Beaver Builder的拖放编辑器自定义类别布局页面。

完成后,单击“完成”按钮,然后选择“发布”以应用类别模板。

现在,您可以访问您的网站以查看类别模板的使用情况。

我们希望本文能帮助您学习如何在WordPress中创建类别模板。

回到顶部