可以从一个提要请求几种帖子类型吗?

时间:2012-10-03 作者:Emre

如果是,应如何格式化查询URL?我尝试过:

  • post_type=cat1&post_type=cat2
  • post_type=cat1,cat2
  • post_type=cat1+cat2
This bug报告说您可以使用post_type[]=cat1&post_type[]=cat2 如果不是因为“所有请求参数都转换为字符串”。情况仍然如此吗?这意味着什么?

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

你可以使用add_feed() 并创建新的feed…为每个帖子类型组合创建一个单独的feed。这不是我所说的灵活,所以我建议创建一个端点。

以下代码为WordPress安装的根目录创建端点。刷新永久链接后,您将在/multifeed/ (仅限页数)或/multifeed/post+page+custom/ 对于几乎任何给定的组合。

我不知道为什么,但它似乎不适用于附件。

<?php
/*
 * Plugin Name: T5 Multi Post Type Feed
 */

add_action(
    \'init\',
    array ( T5_Multi_Post_Type_Feed::get_instance(), \'plugin_setup\' )
);
class T5_Multi_Post_Type_Feed
{
    /**
     * Plugin instance.
     *
     * @see get_instance()
     * @type object
     */
    protected static $instance = NULL;

    /**
     * Internal handler for WordPress
     *
     * @type string
     */
    protected static $query_var = \'multifeed\';

    /**
     * Access this plugin’s working instance
     *
     * @wp-hook init
     * @since   2012.10.03
     * @return  object of this class
     */
    public static function get_instance()
    {
        NULL === self::$instance and self::$instance = new self;

        return self::$instance;
    }

    /**
     * Used for regular plugin work.
     *
     * @wp-hook init
     * @since   2012.10.03
     * @return  void
     */
    public function plugin_setup()
    {
        add_rewrite_endpoint( self::$query_var, EP_ROOT );
        add_filter( \'request\', array ( $this, \'set_query_var\' ) );
        // Hook in late to allow other plugins to operate earlier.
        add_action( \'template_redirect\', array ( $this, \'render\' ), 100 );
    }

    /**
     * Print the feed items.
     *
     * @wp-hook template_redirect
     * @since   2012.10.03
     * @return void
     */
    public function render()
    {
        if ( ! is_front_page() or ! get_query_var( self::$query_var ) )
        {
            return;
        }

        $args       = get_query_var( self::$query_var );
        $post_types = array_filter( explode( \'+\', $args ) );
        query_posts( array ( \'post_type\' => $post_types ) );
        // make the post object accessible in this scope. Hat tip to @Rarst. :)
        global $post;
        require WPINC . \'/feed-rss2.php\';
        exit;
    }

    /**
     * Set the endpoint variable to a default value.
     *
     * If the endpoint was called without further parameters it does not
     * evaluate to TRUE otherwise.
     *
     * @wp-hook request
     * @since   2012.10.03
     * @param   array $vars
     * @return  array
     */
    public function set_query_var( $vars )
    {
        // make the following code shorter
        $q       = self::$query_var;
        $default = apply_filters( \'t5_multipost_default_type\', \'page\' );

        if ( isset ( $vars[ $q ] ) and \'\' === $vars[ $q ] )
        {
            $vars[ $q ] = $default;
        }

        // Sometimes WP misinterprets the request as a page request
        // and returns a 404.
        if ( ( isset ( $vars[\'pagename\'] ) and $q === $vars[\'pagename\'] )
            or ( isset ( $vars[\'page\'] )
                and isset ( $vars[\'name\'] )
                and $q === $vars[\'name\']
            )
        )
        {
            $vars[\'page\'] = $vars[\'pagename\'] = $vars[\'name\'] = FALSE;
            $vars[ $q ] = $default;
        }
        return $vars;
    }

    /**
     * Get the URL for our endpoint.
     *
     * Usage:
     * $url = T5_Multi_Post_Type_Feed::get_instance()->get_endpoint_url(
     *      array( \'post\', \'attachment\' )
     * );
     *
     * @return  string
     * @wp-hook plugin_row_meta
     * @since   2012.09.21
     * @return  string
     */
    public function get_endpoint_url( $types = array( \'page\' ) )
    {
        $post_types = implode( \'+\', $types );
        // If that is false, there are no pretty permalinks.
        $has_permalinks = get_option( \'permalink_structure\' );
        $home = home_url( \'/\' );

        if ( ! $has_permalinks )
        {
            return add_query_arg(
                array ( self::$query_var => $post_types ),
                $home
            );
        }

        return user_trailingslashit( $home . self::$query_var . "/$post_types" );
    }
}
作为插件安装,只需访问该页面即可刷新永久链接设置,仅此而已。

结束

相关推荐

Grouping post-types in loop

当用户从下拉列表中选择特定类别时,将显示多个帖子类型的列表。我想用标题中的帖子类型标题分组显示数据。我不确定如何划分为岗位类型组。我花了一点时间寻找,但没有找到我真正需要的东西。我还试图添加<?php $post_type = get_post_type( $post->ID ); echo $post_type; ?> 但这只是重复(显然)。调用循环<div id=\"content\" role=\"main\" style=\"float:right; width:765px