为包含第一篇帖子的特色图片的类别创建自定义提要

时间:2012-12-31 作者:Jake Lisby

我需要一些帮助来解决这个问题。我有一个查询现在还不能正常工作,但我需要它来创建一个自定义提要,其中包含类别信息,然后包含该类别中第一篇帖子的特色图像。

有人想过如何让它运行吗?

/**
 * Custom Feed for Category Listing
 */
    function outputXMLFeed()
    {
        echo \'<?xml version="1.0"?>\';
        echo \'<items>\';
        $args=array(
          \'orderby\' => \'name\',
          \'order\' => \'ASC\'
        );
        $categories=get_categories($args);
        $posts = get_posts(array(\'category\' => $category->term_id));
        foreach($categories as $category) { 
            echo \'<item>\';
            echo \'<catID>\' . $category->term_id . \'</catID>\';
            echo \'<catname>\' . $category->name . \'</catname>\';
            echo \'<postcount>\' . $category->category_count . \'</postcount>\';
            echo \'<slug>\' . $category->slug . \'</slug>\';
            echo \'<featured>\' . [VARIABLE HERE] . \'</featured>\';
            echo \'</item>\';
        }
        echo \'</items>\';
    }
    add_action(\'init\', \'add_my_feed\');

    function add_my_feed(  ) {
      add_feed("myFeed", "outputXMLFeed");
    }

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

首先定义一个函数,通过给定的类别ID返回类别的第一篇帖子,例如:

function get_category_post($cat_id){
    $post_args = array(
        \'numberposts\' => 1,
        \'category\' => $cat_id,
        \'fields\' => \'ids\'
    );
    $posts = get_posts($post_args);
    return $posts[0];
}
然后,一旦拥有该功能,您就可以这样使用它:

function outputXMLFeed()
{
    echo \'<?xml version="1.0"?>\';
    echo \'<items>\';
    $args=array(
      \'orderby\' => \'name\',
      \'order\' => \'ASC\'
    );
    $categories=get_categories($args);
    foreach($categories as $category) { 
        echo \'<item>\';
        echo \'<catID>\' . $category->term_id . \'</catID>\';
        echo \'<catname>\' . $category->name . \'</catname>\';
        echo \'<postcount>\' . $category->category_count . \'</postcount>\';
        echo \'<slug>\' . $category->slug . \'</slug>\';
        echo \'<featured>\' . get_the_post_thumbnail(get_category_post($category->term_id) , $size = \'post-thumbnail\') . \'</featured>\';
        echo \'</item>\';
    }
    echo \'</items>\';
}
add_action(\'init\', \'add_my_feed\');

function add_my_feed(  ) {
  add_feed("myFeed", "outputXMLFeed");
}

结束