2015-11-02 80 views
1

我无法设置我的WordPress模板。我不知道如何实现以下目标。我的网站设置如下:如何设置我的Wordpress模板结构来处理我的自定义帖子类型

我有一个自定义帖子类型“章”。 “章”是其他CPT的父母。我有几个帖子类型,如:评论,访谈,博客帖子......

在章节页面上,我为本章中的每种帖子类型都做了一个不同的WP_Query。

page template single-chapter.php

现在我希望能够到当前章节中点击“博客帖子(2)”,并打开所有博客帖子的档案页。我怎样才能做到这一点?我假设我应该创建一个模板页面“archive-blogpost.php”。我已经发现,我可以链接到这个网页使用:

<?php echo get_post_type_archive_link('blogpost'); ?> 

但是我不能看到这个页面如何能知道目前我是什么章?

+0

如何在章节和博客帖子之间建立联系?这是一个类别吗? – vard

+0

感谢您关注此事。这不是一个类别。一章现在是一个自定义文章类型,因为它可以有一个“章节副标题”和一个“章节图片”。 CPT“章节”有一个职位关系,可以有孩子“blogposts”。你认为最好使用类别? –

+0

不,我只是想了解你如何建立你的CPT关系。如果你粘贴你的问题你的CPT定义可能会有所帮助。 – vard

回答

0

我使用WP类型的函数来实现我想要的东西:

$ child_posts = types_child_posts(“blogpost”,array('post_id'=> $ _GET ['wpv-pr-child-of']));

有了这个,我可以查询页面上自定义帖子类型的所有子帖子。

我还使用会话变量来跟踪当前章节的进度。

0

我从来没有使用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 
} 
+0

感谢您的详细回复!我会试试这个并回复你。 –

相关问题