【问题标题】:wordpress get_categories() issuewordpress get_categories() 问题
【发布时间】:2010-11-01 00:54:37
【问题描述】:

我创建了下面的函数,该函数旨在列出 id 为 3 的父级的子类别。

该函数还应返回每个子类别中第一个帖子的元数据。

这可行(有点),但它不是只获取一组数据,而是返回 3 组具有不同结果的数据。

任何想法为什么?

global $cat;
  global $post;
  $categories = get_categories('child_of=3');
  foreach ($categories as $cat) :
    $postslist = get_posts($cat->cat_ID, 'numberposts=1&order=DESC');
    foreach ($postslist as $post) :
        $option = '<li id="'.get_post_meta($post->ID, 'id', true).'">';
        $option .='<a class="preview" rel="'.get_post_meta($post->ID, 'thumbnail', true).'" ';
        $option .='href="'.get_bloginfo('url').'/'.$post->post_name.'">';
        $option .=$cat->cat_name;
        $option .='</a>';
        $option .='</li>';
    echo $option;
    endforeach;
  endforeach;

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    如果您在 wordpress.org 上查看 get_posts() 的文档,您会发现该函数只有一个参数。

    get_posts('numberposts=1&category='. $cat->cat_ID .'&order=DESC');
    

    【讨论】:

      【解决方案2】:

      我已经弄清楚我哪里出错了!这个:

      $postslist = get_posts($cat->cat_ID, 'numberposts=1&order=DESC');
      

      已替换为:

      $postslist = get_posts('category='.$cat->cat_ID.'numberposts=1&order=DESC');
      

      这给出了实际的类别 ID。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-25
        相关资源
        最近更新 更多