我正在为我的一个帖子类型创建一个自定义提要,并允许用户设置该提要的名称。
http://www.mysite.com/feed/userdefinedname
现在,我还想让用户不仅可以显示该CPT中的所有帖子,还可以按类别过滤它们。目前情况是这样的:
http://www.mysite.com/feed/testcategory
然而,如果我能将其结构如下,我会非常喜欢它:
http://www.mysite.com/feed/userdefinedname/testcategory
这可能吗?
下面是生成这些基于类别的提要的代码:
/**
* Generate the feeds for categories
*
* @since 1.0
*/
function rss_c_add_category_feed( $in ) {
$category = $GLOBALS[\'wp_rewrite\']->feeds[ count( $GLOBALS[\'wp_rewrite\']->feeds ) - 1 ];
// Prepare the post query
// Get published rss_feed posts with a rss_category slug in the taxonomy
$rss_custom_feed_query = apply_filters(
\'rss_custom_feed_query\',
array(
\'post_type\' => \'rss_feed\',
\'post_status\' => \'publish\',
\'cache_results\' => false, // disable caching
\'tax_query\' => array(
array(
\'taxonomy\' => \'rss_category\',
\'field\' => \'slug\',
\'terms\' => array( $category ),
\'operator\' => \'IN\'
)
)
)
);
// Submit the query to get latest feed items
query_posts( $rss_custom_feed_query );
$sources = array();
while ( have_posts() ) {
the_post();
$sources[] = get_the_ID();
}
// Create the query array
$pre_query = array(
\'post_type\' => \'rss_feed_item\',
\'post_status\' => \'publish\',
\'cache_results\' => false, // disable caching
\'meta_query\' => array(
array(
\'key\' => \'rss_feed_id\',
\'value\' => $sources,
\'compare\' => \'IN\'
)
)
);
// Get options
$options = get_option( \'rss_settings_general\' );
if ( $options !== FALSE ) {
// If options exist, get the limit
$limit = $options[\'custom_feed_limit\'];
if ( $limit !== FALSE ) {
// if limit exists, set the query limit
$pre_query[\'posts_per_page\'] = $limit;
}
}
// query the posts
query_posts( $pre_query );
// Send content header and start ATOM output
header(\'Content-Type: application/atom+xml\');
// Disabling caching
header(\'Cache-Control: no-cache, no-store, must-revalidate\'); // HTTP 1.1.
header(\'Pragma: no-cache\'); // HTTP 1.0.
header(\'Expires: 0\'); // Proxies.
echo \'<?xml version="1.0" encoding="\' . get_option(\'blog_charset\') . \'"?\' . \'>\';
?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Latest imported feed items on <?php bloginfo_rss(\'name\'); ?></title>
<?php
// Start the Loop
while ( have_posts() ) : the_post();
?>
<entry>
<title><![CDATA[<?php the_title_rss(); ?>]]></title>
<link href="<?php the_permalink_rss(); ?>" />
<published><?php echo get_post_time( \'Y-m-d\\TH:i:s\\Z\' ); ?></published>
<content type="html"><![CDATA[<?php the_content(); ?>]]></content>
</entry>
<?php
// End of the Loop
endwhile;
?>
</feed>
<?php
}
最合适的回答,由SO网友:Mark Davidson 整理而成
正如Krzysiek所说,如果不知道你是如何进行feed的,很难给出一个好的答案。但是假设您已经扩展了默认的wordpress提要,以便可以通过索引访问提要。php?feed=userdefinedname,那么这个重写规则应该可以为您完成这项工作。
add_rewrite_rule(
\'^feed/(.+)/(.+)?/?$\',
\'index.php?feed=$matches[1]&category_name=$matches[2]\',
\'top\'
); // /feed/userdefinedname/testcategory