循环遍历 wordpress 类别 [英] Looping through wordpress categories

查看:29
本文介绍了循环遍历 wordpress 类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Wordpress 的新手,一直在努力创建类别循环.循环应该:

  1. 遍历所有类别
  2. 回显类别名称(带有指向
  3. 的链接)
  4. 回显该类别中的最后 5 个帖子(带有要发布的永久链接)

每个的 html 将是

<div class="cat_name"><a href="<?php get_category_link( $category_id ); ?>">猫名</a>

<ul class="cat_items"><li class="cat_item"><a href="permalink">cat 项目 1</a><li class="cat_item"><a href="permalink">cat 项目 2</a><li class="cat_item"><a href="permalink">cat 项目 3</a><li class="cat_item"><a href="permalink">cat 项目 4</a><li class="cat_item"><a href="permalink">cat 项目 5</a>

请帮忙

解决方案

糟糕,错过了你想要的 5 个帖子

'姓名','订单' =>'ASC');$categories=get_categories($cat_args);foreach($categories 作为 $category) {$args=数组('showposts' =>5、'category__in' =>数组($category->term_id),'caller_get_posts'=>1);$posts=get_posts($args);如果($帖子){echo '<p>Category: <a href="' .get_category_link( $category->term_id ) .'" title="' .sprintf( __("查看 %s 中的所有帖子"), $category->姓名) . '" ' .'>'.$category->name.'</p>';foreach($posts as $post) {setup_postdata($post);?><p><a href="<?php the_permalink() ?>"rel="bookmark" title="永久链接到<?php the_title_attribute(); ?>"><?php the_title();?></a></p><?php}//foreach($posts}//如果 ($posts}//foreach($categories?>

I am new to Wordpress and been pulling my hair out trying to create a category loop. The loop is supposed to:

  1. loop through all categories
  2. echo out the category name (with link to
  3. echo out the last 5 posts in that category (with permalink to post)

The html for each would be

<div class="cat_wrap">
   <div class="cat_name">
       <a href="<?php get_category_link( $category_id ); ?>">Cat Name</a>
   </div>
   <ul class="cat_items">
      <li class="cat_item">
         <a href="permalink">cat item 1</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 2</a>
      </li>
      <li class="cat_item">
          <a href="permalink">cat item 3</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 4</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 5</a>
      </li>
   </ul>
</div>

Please help

解决方案

Oops, missed that you wanted 5 posts

<?php
//for each category, show 5 posts
$cat_args=array(
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) { 
    $args=array(
      'showposts' => 5,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

这篇关于循环遍历 wordpress 类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆