Wordpress 对多个类别的标题进行排序和显示

Wordpress 对多个类别的标题进行排序和显示,wordpress,Wordpress,我在我的页面模板中使用它来显示分类编号为8、17、23、423的最近20篇文章的标题 <div id="horizontalTab"> <ul class="resp-tabs-list"> <li>Football</li> <li>Ice Hockey</li> <li>Basketball</li> <li>Tennis</li&g

我在我的页面模板中使用它来显示分类编号为8、17、23、423的最近20篇文章的标题

<div id="horizontalTab">
   <ul class="resp-tabs-list">
     <li>Football</li>
     <li>Ice Hockey</li>
     <li>Basketball</li>
     <li>Tennis</li>
    </ul>
    <div class="resp-tabs-container">
      <?php 
      foreach (get_categories(array('include'=>'8, 17, 23, 423', 'orderby' => 'count', 'order' => DESC)) as $category) {
      $catid = $category->cat_ID;
      $args = array( 'category' =>$catid, 'numberposts' => 20 );
      $myposts = get_posts($args); ?>
      <div>
       <?php foreach($myposts as $post) { ?>
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
       <?php } ?>
     </div>
     <?php } ?>
   </div>
</div>

  • 足球
  • 冰球
  • 篮球
  • 网球

但这段代码添加了10多个数据库查询。如何优化此查询。 还有一个问题。如何更改排序类别的顺序:23、17、8423


既然您已经知道了类别,为什么还要使用get_categories。 您所要做的就是将类别ID存储在一个数组中并运行一个循环

$cat_array=array(23,17,8,423);
foreach ($cat_array as $catid)
{
$args = array( 'category' =>$catid, 'numberposts' => 20 );
$myposts = get_posts($args); 

  <div>
   <?php foreach($myposts as $post) { ?>
   <a href="<?php $post->guid; ?>"><?php $post->post_title; ?></a><br/>
   <?php } ?>
 </div>
<?php } ?>
$cat_array=array(23,17,8423);
foreach($catu数组作为$catid)
{
$args=数组('category'=>$catid,'numberposts'=>20);
$myposts=get_posts($args);


Parse error:syntax error,第15行(第15行:$cat_array={23,17,8423};)的/home/h54892/data/www/new.goalstube.ru/wp-content/themes/sandbox/index.php中意外出现的“{”,如果没有此代码页,将生成26个db查询,并使用它46。是否可以进行优化?