php - 如何覆盖自定义提要的 WP Admin 联合提要限制?

标签 php wordpress feed syndication

我需要覆盖 WP 管理中的设置,以便在常规 -> 管理下的联合设置中最近发布的帖子数量。

我正在使用以下代码获取所有帖子,但我不需要那么多。有人可以给我一个如何检索 50 个帖子的例子吗?这应该只会影响这个 Feed,而不会影响所有其他 Feed。

function no_limits_for_feed( $limits ) {
    return '';
}

add_filter( 'post_limits', 'no_limits_for_feed' );

更新:这是我正在使用的代码示例,它来自 Feed JSON WordPress 插件:

class feed_json {
    function feed_json() {
        global $wp_rewrite;

        add_action('init', array(&$this, 'add_feed_json'));
        add_action('do_feed_json', array(&$this, 'do_feed_json'), 10, 1);
        add_filter('template_include', array(&$this, 'template_json'));
        add_filter('query_vars', array(&$this, 'add_query_vars'));

        $plugin_basename = plugin_basename(__FILE__);
        add_action('activate_' . $plugin_basename, array(&$this, 'add_feed_json_once'));
        add_action('deactivate_' . $plugin_basename, array(&$this, 'remove_feed_json'));
    }

    function add_feed_json_once() {
        global $wp_rewrite;
        $this->add_feed_json();
        $wp_rewrite->flush_rules();
    }

    function remove_feed_json() {
        global $wp_rewrite;
        $feeds = array();
        foreach ( $wp_rewrite->feeds as $feed ) {
            if ( $feed !== 'json' ) {
                $feeds[] = $feed;
            }
        }
        $wp_rewrite->feeds = $feeds;
        $wp_rewrite->flush_rules();
    }

    function add_query_vars($qvars) {
      $qvars[] = 'callback';
      $qvars[] = 'limit';
      return $qvars;
    }

    function add_feed_json() {
        add_feed('json', array(&$this, 'do_feed_json'));
    }

    function do_feed_json() {
        load_template($this->template_json(dirname(__FILE__) . '/feed-json-template.php'));
    }

    function template_json( $template ) {
        $template_file = false;
        if (get_query_var('feed') === 'json') {
            $template_file = '/feed-json.php';
            if (function_exists('get_stylesheet_directory') && file_exists(get_stylesheet_directory() . $template_file)) {
                $template_file = get_stylesheet_directory() . $template_file;
            } elseif (function_exists('get_template_directory') && file_exists(get_template_directory() . $template_file)) {
                $template_file = get_template_directory() . $template_file;
            } elseif (file_exists(dirname(__FILE__) . '/feed-json-template.php')) {
                $template_file = dirname(__FILE__) . '/feed-json-template.php';
            } else {
                $template_file = false;
            }
        }

        return (
            $template_file !== false
            ? $template_file
            : $template
            );
    }
}
new feed_json();

最佳答案

我以为你可以连接到 pre_get_posts ,但如果它是提要,则 posts_per_page 值稍后会被覆盖。因此,只需连接到 post_limits,检查您的提要,并在需要时返回不同的 LIMIT 部分。

add_filter( 'post_limits', 'so6230475_post_limits', 10, 2 );
function so6230475_post_limits( $limits, &$wp_query )
{
    if ( $wp_query->is_feed( 'json' ) ) {
        $limits = 'LIMIT 50';
    }
    return $limits;
}

关于php - 如何覆盖自定义提要的 WP Admin 联合提要限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6230475/

相关文章:

javascript - 如何使用数据库连接制作动态 amchart?

wordpress - Git Status - 仅列出有更改的直接子文件夹,而不是内部文件

php - 以编程方式批量创建 WooCommerce 产品

Javascript 页面加载延迟 1-2 秒,样式怪异

javascript - FB 提要对话框不会在弹出窗口中显示图片缩略图

javascript - Google Feed API 导致页面空白

javascript - 如何使用 javascript 根据单元格的值更改单元格的背景颜色?

php - 如何测试我的应用程序 phpunit - zend framework 2 中的每个模块

php 为多个项目输出 javascript

python - 使用 Google App Engine 的 RSS Feed 聚合器 - Python