在RSS提要中显示排名前10的帖子

时间:2018-03-05 作者:Sdesign

我正在尝试获得一个rss提要,它将显示本周前10位的帖子。我正在使用top 10 popular posts插件将它们显示在网站的侧栏中,这很好,但需要在rss中显示它们,这样我就可以让它们发送出去的电子邮件。下面是10大API的一些代码。任何帮助都会很好。

<?php
/*
* This example fetches the popular posts tracked by Top 10. 
*
 */

if ( function_exists( \'get_tptn_pop_posts\' ) ) {
$settings = array(
    \'daily\' => TRUE,
    \'daily_range\' => 30,
    \'limit\' => 20,
    \'strict_limit\' => FALSE,
  );
  $topposts = get_tptn_pop_posts( $settings ); // Array of posts

  $topposts = wp_list_pluck( (array) $topposts, \'postnumber\' );

  $args = array(
    \'post__in\' => $topposts,
    \'orderby\' => \'post__in\',
    \'posts_per_page\' => 7,
    \'ignore_sticky_posts\' => 1 
  );

  $my_query = new WP_Query( $args );
  if ( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) {
        $my_query->the_post();
        echo \'<a href="\' . get_permalink( get_the_ID() ) . \'">\';
        the_title();
        echo \'</a>\';
        wp_reset_postdata();
     }
  } else {
  }
  wp_reset_query();

  }

1 个回复
SO网友:majick

如果在提要URL中检测到查询字符串,则可以通过重写主查询结果来实现。?top_ten

为前十名添加查询变量:

function my_query_vars( $query_vars ) {
    $query_vars[] = \'top_ten\';
    return $query_vars;
}
add_filter( \'query_vars\', \'my_query_vars\' );
为您的代码创建一个函数,以作为查询检索前十名

function my_top_ten_query($limit, $query) {

    if ( !function_exists( \'get_tptn_pop_posts\' ) ) {return $query;}

    $settings = array(
        \'daily\' => TRUE,
        \'daily_range\' => 30,
        \'limit\' => 20,
        \'strict_limit\' => FALSE,
    );
    $topposts = get_tptn_pop_posts( $settings ); // Array of posts

    $topposts = wp_list_pluck( (array) $topposts, \'postnumber\' );

    $args = array(
        \'post__in\' => $topposts,
        \'orderby\' => \'post__in\',
        \'posts_per_page\' => $limit,
        \'ignore_sticky_posts\' => 1 
    );

    $my_query = new WP_Query( $args );
    return $my_query;

}
然后过滤主查询以检查查询字符串,并返回前十个查询,而不是标准提要查询:

function my_feed_posts_filter( $query ) {

    // only for feeds
    if ( $query->is_feed && $query->is_main_query() )  {

        // check if the top_ten variable is set 
        if ( isset( $query->query_vars[\'top_ten\'] ) 
                && ! empty( $query->query_vars[\'top_ten\'] ) ) {
            // return your top ten query object instead
            return my_top_ten_query($query->query_vars[\'top_ten\'], $query);
        }
    } 
    return $query;
}

add_filter( \'the_posts\', \'my_feed_posts_filter\' )
解决方案改编自this answer on custom feed queries.

结束

相关推荐

在RSS提要中显示分类术语

The problem:我有一个自定义的帖子类型“announcements”和一个自定义的分类法“announcements\\u taxonomy”。我可以访问rss提要,方法是http://domain.com/announcements/feed - 问题是我看不到RSS提要中的类别/标记。Detail:我正在使用Simplepie获取提要项,并在WordPress安装之外显示它们。因为我的WP站点上的rss提要没有显示类别,所以我无法使用简单的pies get\\u categories()函数