自定义tag.php模板

I'm very new to PHP and have some trouble wrapping my head around it sometimes so please bear with me.

I have a lot of categories and a lot of tags. I started making category-slug.php templates but it'd probably be best for me to just use category.php and tag.php templates. I just can't get them to work unless I add in something like 'category_name' => 'art'. I've also read that querying isn't ideal (I think that's what I'm doing?), but I have had custom development done and I'm not sure if that has or hasn't been left as my only option.

 $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 );

And then later on I have this with post title, date, excerpts, etc. to follow.

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

How do I make the category.php and tag.php pages specific to each unique slug without having to manually make each one?

1个回答



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


模板 层次结构指定WordPress将使用它在当前主题目录中找到的第一个模板文件,如下所示:</ p>


  1. category-slug.php </ li>
    < li> category-ID.php </ li>
  2. category.php </ li>
  3. archive.php </ li>
  4. index.php </ li>
    < / ol>
    </ blockquote>

    您不必执行任何自定义WP_Query来获取类别的帖子。</ p>

      if(have_posts)  ()):
    while(have_posts()):the_post();
    the_content(); //这是单个帖子的内容,属于这个类别
    the_title(); //单个标题 post
    the_excerpt(); //单个帖子的摘录
    //等等
    \ endwhile;

    endif;
    </ code> </ pre>

    这也是标签的类似情况。</ p>

    关于类别模板标签模板 。</ p>
    </ div>

展开原文

原文

Category Template Hierarchy, as described in 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

You don't have to do any custom WP_Query to get the posts of category.

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;

It's the similar scenario for tags as well.

More details on Category Templates and Tag Templates.

douxi0098
douxi0098 我已经阅读了一堆层次结构。 我看了一下我的最低层次模板在主题本身中是什么,它是archive.php,所以我现在已经在我的孩子主题中做了(我之前没有交叉引用,所以谢谢!)。 我拿出了查询并得到了正确的帖子。 谢谢!
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐