我在试图弄清楚如何做到这一点时也遇到了同样的问题,我遵循了烧酒的建议并想出了一个解决方案。可能有更好的解决方案,但这就是我的做法。我和我的朋友正在写一个动漫评论博客,我和他都会写一篇关于同一部动漫的评论。
我首先创建了两种帖子类型:
- 动漫:特定动漫的主页,如描述、图片等。
- 评论:作者对动漫的评论。我在这里启用的选项是编辑器、标题和作者。连同相关的动漫分类。这就是这里所需要的一切
然后我为动漫标题创建了一个分类法,因此当用户需要对尚未添加为评论的动漫撰写评论时,他们可以将标题添加到分类法中。
我将分类法与 post_types 和 wala 相关联!这就是你所需要的。
因此,现在当您想为新动漫撰写新评论时,您首先添加动漫帖子类型并写下动漫的内容并包含图片等。将标题添加到分类并检查它。之后,您然后创建一个帖子类型评论的新帖子并撰写您的评论,记得在您的分类中检查正确的标题以了解这将是什么动漫,然后您就可以开始了!
问题 1:如何将其包含到我的循环中?
好吧,您不想在循环中包含两种帖子类型,您只想在循环中包含帖子和其他帖子类型动画,因此您在 functions.php 文件中执行以下操作:
function include_custom_post_types( $query ) {
global $wp_query;
// Get all custom post types
$custom_post_type = get_query_var( 'post_type' );
// Get all post types
$post_types = get_post_types();
// If what you are getting is a category or a tag or that there are no custom
// post types you just want to set the post types to be the current post types
if ( (is_category() || is_tag()) && empty( $custom_post_type ))
$query->set( 'post_type' , $post_types );
// Set the custom post types you want to ignore
$ignore_types = array('reviews');
//Unset the post types that are gonna be ignored
foreach($post_types as $key=>$type)
{
if(in_array($type,$ignore_types))
{
unset($post_types[$key]);
}
}
// Set the post types for the query
if ( (is_home() && false == $query->query_vars['suppress_filters']) || is_feed())
$query->set( 'post_type', $post_types);
return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );
问题 2:如何显示评论?
我通过创建另一个 single.php 文件并将其重命名为 single-post_type_name.php 解决了这个问题。所以在这种情况下,我为我的帖子类型动漫创建了一个 single-anime.php 文件。然后代替内容,我想获得这个特定动漫的所有评论,所以我在主要内容区域的文件中添加了以下内容:
<?php
//You grab the taxonomy that you have selected for this post
$terms = wp_get_post_terms(get_the_ID(), 'animes_reviewed');
// This is the args array for the criteria that the posts need to be in
$args = array(
// This is the post type of where your reviews are at
post_type' => 'reviews',
// this is for searching the taxonomy usually it's
// taxonomy_name => checked_taxonomy
'anime' => $terms[0]->name,
'post_status' => 'publish'
);
// Grab the posts
$posts = get_posts($args);
//Here I echo out the information for debugging purpose, but
//Here is where you can do HTML to display your reviews
foreach($posts as $post)
{
echo($post->post_content);
the_author_meta( 'nickname', $post->post_author);
}
?>
通过添加更多分类法等,您可以做更多事情。实际上,我通过添加分类法并在帖子部分添加要查找的标准来实现剧集审查。希望这会对您有所帮助,但可能有点晚了:(感谢烧酒推荐自定义帖子类型!