自定义 tag.php 模板

Custom tag.php template

我是 PHP 的新手,有时会遇到一些麻烦所以请多多包涵。

我有很多分类和很多标签。我开始制作 category-slug.php 模板,但我最好只使用 category.php 和 tag.php 模板。除非我添加 'category_name' => 'art' 之类的内容,否则我无法让它们工作。我还读到查询并不理想(我认为这就是我正在做的?),但我已经完成了定制开发,但我不确定这是否已经或没有作为我唯一的选择。

 $page_content = "";
 if (have_posts()) : 
     while (have_posts()) : the_post();
         $page_content .= get_the_content(); 
     endwhile; 
 endif;     

 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $args = array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged' 
 => $paged );

然后我有这个 post 标题、日期、摘录等。

 <?php
      $wp_query = new WP_Query($args);
      while ( $wp_query->have_posts() ) : $wp_query->the_post();
 ?>

如何使 category.php 和 tag.php 页面针对每个独特的 slug 而不必手动制作每个页面?

类别模板层次结构,如 WP codex 中所述:

The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:

  1. category-slug.php
  2. category-ID.php
  3. category.php
  4. archive.php
  5. index.php

您无需进行任何自定义 WP_Query 即可获取类别的帖子。

if (have_posts()) : 
    while (have_posts()) : the_post();
        the_content();//this is the content of the single post, belonging to this category
        the_title();//title of the single post
        the_excerpt();//excerpt of the single post
        //and so on
    endwhile; 
endif;

标签也是类似的情况。

有关 Category Templates and Tag Templates 的更多详细信息。