情况我有两个cpt:
这两个CPT I中的项目和作品具有相同的分类“类别”,术语也相同:
建筑、景观、室内设计我有如下网址
projects/architecture
works/architecture
so on...
问题现在的问题是,上面的两个URL返回相同的内容,即术语体系结构的所有存档,无论cpt如何。
有没有办法projects/architecture
将只返回以下所有邮件projects 用于期限的architecture?
编辑这里是我的category template (category.php)
<?php get_header(); ?>
<!-- info about the category -->
<?php
/* Queue the first post, that way we know
* what date we\'re dealing with (if that is the case).
*
* We reset this later so we can run the loop
* properly with a call to rewind_posts().
*/
if ( have_posts() )
the_post();
?>
<h1 class="page-title"><?php
_e( ucwords($post->post_type), \'boilerplate\' );
?></h1>
<?php
/* Since we called the_post() above, we need to
* rewind the loop back to the beginning that way
* we can run the loop properly, in full.
*/
rewind_posts();
/* Run the loop for the archives page to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-archives.php and that will be used instead.
*/
?>
<ul class=\'large-block-grid-4 small-block-grid-3 listWrap\'>
<?php
get_template_part( \'loop\', \'category\' );
?>
</ul>
<?php
?>
<?php get_footer(); ?>
这是我的
category loop (循环类别.php)
<?php while ( have_posts() ) : the_post(); ?>
<?php
$attachments = get_posts( array(
\'post_type\' => \'attachment\',
\'post_parent\' => $post->ID,
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'numberposts\' => 1
) );
?>
<?php $img = wp_get_attachment_image_src($attachments[0]->ID, \'full\');?>
<li class=\'listwp equalize\'>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class=\'th\'>
<img src=\'<?php echo $img[0]; ?>\'>
<?php $categories = get_the_terms($post->ID, \'category\'); ?>
<div>
<span>
<?php foreach($categories as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</span>
</div>
</a>
<h2><?php the_title(); ?></h2>
</li>
<?php endwhile; ?>
SO网友:Jonathan de M.
好的,我按照s_ha_dum 使用pre\\u gets\\u posts。
以下所有代码都指向函数。php
1创建新的重写规则
function rewrite_clean()
{
// my CPTs
$types = array(\'projects\', \'works\');
foreach($types as $type)
{
add_rewrite_rule(\'^\'.$type.\'/([^/]*)/?$\', \'index.php?post_type=\'.$type.\'&term=$matches[1]\',\'top\');
}
}
add_action(\'init\', \'rewrite_clean\');
转换以下格式
?post\\u type=项目(&U);术语=架构
改为以下格式
项目/架构
2覆盖post查询
// check the vars in url and if there is term then only display from this term
function add_cat_cpt_filter($query)
{
if($query->is_archive() && get_query_var( \'term\' ) && get_query_var( \'post_type\' ))
{
// get the id of the category by using the slug
$id = get_category_by_slug(get_query_var( \'term\' ))->term_id;
if($id){
// add a filter to the query
$query->set(\'cat\', $id);
}
else{
// redirect if the term doesn\'t exist
wp_redirect( \'/\'.get_query_var( \'post_type\' ));
exit;
}
return;
}
}
add_action( \'pre_get_posts\', \'add_cat_cpt_filter\' );
上面的代码将使用URL中使用的术语过滤当前存档页面。
明白了,然后我遇到了一个问题,原因是Custom Post Type Permalinks
插件,它一直显示一个类别模板,而我想保留我的CPT存档模板,只需在其上添加一个过滤器。
The solution 就是简单地禁用并重新启用插件,它为我做到了这一点。