简体   繁体   中英

Custom tag.php template

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM