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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧