dongyang9813 2018-11-03 08:33
浏览 161
已采纳

自定义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条回答 默认 最新

  • doukuipai8544 2018-11-03 10:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办