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

wordpress模板

Have you visited a site where each category has different layout? In WordPress theme development, it is a common practice to use different templates for categories, tags, custom post types, and taxonomies. By creating templates for categories you can add specific features on category pages. For example, you can allow users to subscribe to categories, add category images, show category description and choose a different layout for each category. In this guide, we will show you how to create category templates in WordPress.

您是否访问过每个类别都有不同布局的网站? 在WordPress主题开发中,通常的做法是对类别,标签, 自定义帖子类型分类法使用不同的模板。 通过为类别创建模板,您可以在类别页面上添加特定功能。 例如,您可以允许用户订阅类别添加类别图像 ,显示类别描述并为每个类别选择不同的布局。 在本指南中,我们将向您展示如何在WordPress中创建类别模板。

类别页面的WordPress模板层次结构 (WordPress Template Hierarchy for Category Pages)

WordPress has a powerful templating system. You can create a WordPress theme by using different templates for different sections of your website. WordPress looks for a template in a pre-defined hierarchical order when displaying any page. To display a category page, it looks for templates in this order.

WordPress具有强大的模板系统。 您可以通过对网站的不同部分使用不同的模板来创建WordPress主题。 当显示任何页面时,WordPress会按预定义的层次结构顺序查找模板。 要显示类别页面,它将按此顺序查找模板。

category-slug.php → category-id.php → category.php → archive.php → index.php

category-slug.php→category-id.php→category.php→archive.php→index.php

First WordPress will look for a template specific for that particular category using the category slug, for example, category-design.php template will be used to display ‘Design’ category. If it does not find a category-slug template, then WordPress will look for a template with category id, for example category-6.php. After that it will look for the generic category template which is usually category.php. If there is no generic category template present, then WordPress will look for generic archive template, i.e. archive.php. Lastly it will use index.php template to display the category.

首先,WordPress将使用类别标签来查找特定于该特定类别的模板,例如, category-design.php模板将用于显示“设计”类别。 如果找不到类别标签模板,则WordPress将寻找具有类别ID的模板,例如category-6.php 。 之后,它将查找通常为category.php的通用类别模板。 如果不存在通用类别模板,则WordPress将寻找通用存档模板,即archive.php 。 最后,它将使用index.php模板显示类别。

在WordPress中创建类别模板 (Creating a Category Template in WordPress)

Lets first take a look at a typical category.php template.

首先让我们看一个典型的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(); ?>

Now lets assume that you have a category called “Design” with the category-slug “design” and you want to display this category differently than others. To do that, you need to create a template for that particular category. Go to Appearance » Editor. From the list of theme files on your right, click on category.php, if you do not have a category.php file there, then look for archive.php. If you can not find either of these templates then there is a good chance that you are using a WordPress Theme Framework and this tutorial may not useful for you. We suggest that you refer to the specific framework you are using.

现在,假设您有一个名为“ Design”的类别,其中类别标签为“ design”,并且您希望以不同于其他的方式显示该类别。 为此,您需要为该特定类别创建模板。 转到外观»编辑器 。 在右侧的主题文件列表中,单击category.php ,如果那里没有category.php文件,则查找archive.php 。 如果找不到这些模板中的任何一个,则很有可能您正在使用WordPress主题框架 ,本教程可能对您没有用。 我们建议您参考所使用的​​特定框架。

If you find the files above, then copy all the contents of category.php and paste them in a text editor like Notepad. Save this file as category-design.php.

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

Connect to your website using FTP client. Go to /wp-content/themes/your-current-theme/ and upload category-design.php file to your theme directory. Now any changes you make to this template will only appear in this particular category’s archive page. Using this technique you can create templates for as many categories as you want. Just use category-{category-slug}.php as the file name. You can find category slugs by visiting the categories section in WordPress admin area.

使用FTP客户端连接到您的网站。 转到/ wp-content / themes / your-current-theme /并将category-design.php文件上传到主题目录。 现在,您对此模板所做的任何更改将仅显示在该特定类别的存档页面中。 使用此技术,您可以根据需要创建多个类别的模板。 只需使用category- {category-slug} .php作为文件名。 您可以通过访问WordPress管理区域中的类别部分找到类别标签。

Here is an example of a category-slug.php template, notice that we have used the same template as category.php with little changes. Since we already know the category it will be used for we can add title, description, or any other details manually. Also notice that we have used <?php the_excerpt(); ?> instead of <?php the_content(); ?>. Check out why we think using post summary or excerpt instead of full post is a good idea.

这是category-slug.php模板的示例,请注意,我们使用的模板与category.php相同,几乎没有变化。 由于我们已经知道该类别,因此可以将其用于手动添加标题,描述或任何其他详细信息。 还要注意,我们使用了<?php the_excerpt(); ?> <?php the_excerpt(); ?>代替<?php the_content(); ?> <?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(); ?>

If you do not want to use category-slug template, then you can use category-id template to create a template for specific category ID (How to find a category ID in WordPress).

如果您不想使用category-slug模板,则可以使用category-id模板为特定类别ID( 如何在WordPress中查找类别ID )创建模板。

对类别使用条件标签 (Using Conditional Tags for a Category)

When creating templates for your theme, you need to ask yourself do you really need a separate template to do what you want to do? In some cases, the changes you want to make are not too complicated and can be achieved using conditional tags inside a generic template, like category.php or even archive.php.

为主题创建模板时,您需要问自己是否真的需要一个单独的模板来完成您想做的事情? 在某些情况下,您要进行的更改不太复杂,可以使用通用模板(例如category.php甚至archive.php)中的条件标签来实现。

WordPress comes with support for many conditional tags that theme authors can use in their templates. One such conditional tag is is_category(). Using this conditional tag, you can change your templates to display different output if the condition is matched. For example, lets suppose you have a category for featured posts called “Featured”. Now you want to show some extra information on the category archive page for this particular category. To do that add this code in category.php file right after <?php if ( have_posts() ) : ?>.

WordPress支持主题作者可以在其模板中使用的许多条件标签 。 一种这样的条件标记是is_category() 。 如果符合条件,则可以使用此条件标记更改模板以显示不同的输出。 例如,假设您有一个精选文章类别,称为“精选”。 现在,您想在类别存档页面上显示该特定类别的一些其他信息。 为此,请在<?php if ( have_posts() ) : ?>之后的category.php文件中添加此代码。



<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>

Learning WordPress theme development is not something that can be achieved overnight. But you can start learning by tweaking your templates and making smaller changes. It is a risk, and you will break things more often than you would like, but the joy of finally getting it right will keep you motivated.

学习WordPress主题开发并非一overnight而就。 但是,您可以通过调整模板并进行较小的更改来开始学习。 这是一种风险,您会比预期更多地破坏事物,但最终正确解决问题的喜悦将使您充满动力。

We hope this article helped you create category templates in WordPress. If you have any questions about modifying category templates in WordPress, then please leave a comment below.

我们希望本文能帮助您在WordPress中创建类别模板。 如果您对在WordPress中修改类别模板有任何疑问,请在下面发表评论。

翻译自: https://www.wpbeginner.com/wp-themes/how-to-create-category-templates-in-wordpress/

wordpress模板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值