全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1210|回复: 7

[Windows VPS] 请教:wordpress怎么调用子分类标题和子分类的文章

[复制链接]
发表于 2019-4-10 07:48:41 | 显示全部楼层 |阅读模式

用了以下方式调用,输出的是文章所有分类的文章。怎么调用指定分类的文章?
另外如果这个子分类a包括子分类b、c的话,子分类a会把bc的文章都调用出来,就重复了。
谢谢大佬解答。


  1. <?php
  2. global $cat;
  3. $cats = get_categories(array(
  4. 'child_of' => $cat,
  5. 'parent' => $cat,
  6. 'hide_empty' => 0
  7. ));
  8. $c = get_category($cat);
  9. if(empty($cats)){
  10. ?>
  11. <div class="item">
  12. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  13. <div class="post">
  14. <h2><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  15. <p><?php the_excerpt(); ?></p>
  16. <p><a href="<?php the_permalink(); ?>">more >></a></p>
  17. <div class="meta"><?php the_time('Y-m-d'); ?> | 标签: <?php the_tags('', ' , ', ''); ?></div>
  18. </div>
  19. <?php endwhile; ?>
  20. <?php else: ?>
  21. <div class="post"><p>暂无文章</p></div>
  22. <?php endif; ?>
  23. </div>
  24. <div class="navigation">
  25. <span class="alignleft"><?php next_posts_link('&laquo; Older posts') ?></span>
  26. <span class="alignright"><?php previous_posts_link('Newer posts &raquo;') ?></span>
  27. </div>
  28. <?php
  29. }else{
  30. foreach($cats as $the_cat){
  31. $posts = get_posts(array(
  32. 'category' => $the_cat->cat_ID,
  33. 'numberposts' => 10,
  34. ));
  35. if(!empty($posts)){
  36. echo '
  37. <div class="item cat_item">
  38. <div class="item_title"><h2><a title="'.$the_cat->name.'" href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2></div>
  39. <ul class="box_list">';
  40. foreach($posts as $post){
  41. echo '<li><span class="alignright">'.mysql2date('Y-m-d', $post->post_date).'</span>
  42. <a title="'.$post->post_title.'" href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
  43. }
  44. echo '</ul>
  45. </div>';
  46. }
  47. }
  48. }
  49. ?>
复制代码
 楼主| 发表于 2019-4-10 07:51:51 | 显示全部楼层
  1. <ul>
  2. <?php
  3.     $args=array(
  4.         'cat' => 1,   // 分类ID
  5.         'posts_per_page' => 10, // 显示篇数
  6.     );
  7.     query_posts($args);
  8.     if(have_posts()) : while (have_posts()) : the_post();
  9. ?>
  10.     <li>
  11.         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> //标题
  12.         <span>
  13.         <?php if (has_excerpt()) {
  14.                 echo $description = get_the_excerpt(); //文章编辑中的摘要
  15.             }else {
  16.                 echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要
  17.             } ?>
  18.         </span>
  19.     </li>
  20. <?php  endwhile; endif; wp_reset_query(); ?>
  21. </ul>
复制代码


或者用这个方式调用,怎么能把子分类的名称调用出来?现在调用出来的只有文章的名称。
发表于 2019-4-10 07:59:23 | 显示全部楼层
晕。。。官网一查就有的答案

$category_detail=get_the_category('4');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
 楼主| 发表于 2019-4-10 08:05:03 | 显示全部楼层
XiaoFaye 发表于 2019-4-10 07:59
晕。。。官网一查就有的答案

$category_detail=get_the_category('4');//$post->ID

我试一下哈
 楼主| 发表于 2019-4-10 08:54:41 | 显示全部楼层
XiaoFaye 发表于 2019-4-10 07:59
晕。。。官网一查就有的答案

$category_detail=get_the_category('4');//$post->ID

这段话放在什么位置呢?
发表于 2019-4-10 09:14:26 | 显示全部楼层
mylawer 发表于 2019-4-10 08:54
这段话放在什么位置呢?

在the_post(); 后面

$id = get_the_ID();
$category_detail = get_the_category($id);
foreach($category_detail as $cd){
echo $cd->cat_name;
}
 楼主| 发表于 2019-4-10 09:20:42 | 显示全部楼层
XiaoFaye 发表于 2019-4-10 09:14
在the_post(); 后面

$id = get_the_ID();

这个是每个标题前面都会有分类。
我想的样式是这样:

大分类

子分类1
文章1
文章2

子分类2
文章1
文章2

这样~
 楼主| 发表于 2019-4-10 09:37:47 来自手机 | 显示全部楼层
XiaoFaye 发表于 2019-4-10 09:14
在the_post(); 后面

$id = get_the_ID();

发现一个笨方法,就是把这段代码根据子分类多少就复制多少个代码,这样调用也能实现,哈。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-25 10:11 , Processed in 0.066804 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表