我从来没有使用WP类型插件到目前为止,但这里是你可以做的:为档案链接添加一个章节参数(有一个重写规则),你会在你的blogposts档案模板中获得和有条件地显示帖子到这个参数。
首先我们更改存档链接发送章毛坯:
<?php echo get_post_type_archive_link('blogpost') . '/' . $post->post_name; ?>
然后我们定义在的functions.php这个新rewrite tag:
function custom_rewrite_tag() {
add_rewrite_tag('%chapter%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
为了使URL看起来不错,我们将添加一条重写规则(因此您没有像/archive/?chapter=chapter-1
这样的网址,但是/archive/chapter-1
) - 这仍然会转到functions.php:
function custom_rewrite_rule($rules) {
add_rewrite_rule('^archive/([^/]+)/?$', 'index.php?post_type=blogposts&chapter=$matches[1]', 'top');
}
add_filter('init', 'custom_rewrite_rule', 10, 0);
您可能必须根据您的配置更改URL /帖子类型名称。
而在去年,你可以在你的相关博客文章这个查询ARG档案模板,$wp_query->query_vars
阵列:
$wp_query->query_vars['chapter']
因为我不知道很多关于WP类型,我真的不知道什么如下,但似乎可以查询本章的孩子的职位与此:
if(isset($wp_query->query_vars['chapter']) && $chapter = get_page_by_path($wp_query->query_vars['chapter'])) {
$childargs = array(
'post_type' => 'blogposts',
'numberposts' => -1,
'meta_query' => array(array('
key' => '_wpcf_belongs_property_id', 'value' => $chapter->ID
))
);
$child_posts = get_posts($childargs);
} else {
// default template : display all posts
}
如何在章节和博客帖子之间建立联系?这是一个类别吗? – vard
感谢您关注此事。这不是一个类别。一章现在是一个自定义文章类型,因为它可以有一个“章节副标题”和一个“章节图片”。 CPT“章节”有一个职位关系,可以有孩子“blogposts”。你认为最好使用类别? –
不,我只是想了解你如何建立你的CPT关系。如果你粘贴你的问题你的CPT定义可能会有所帮助。 – vard