我想显示我的faq帖子(自定义类型的帖子)main post (single.php)。我使用类别来匹配的职位。如果有任何类别来自faq帖子(自定义类型帖子)匹配main post (single.php)的类别然后在主帖子下面显示FAQ帖子内容。类别不需要匹配所有,但至少需要匹配一个。
<?php while (have_posts()):
the_post(); ?>
<h1 class="page-title"><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php get_template_part("widgets/cta"); ?>
<?php
$cat = the_category();
echo $cat[0]->cat_name;
?>
<?php
$args_faq = ["post_type" => "faq", "posts_per_page" => 2];
$faq_loop = new WP_Query($args_faq);
while ($faq_loop->have_posts()):
$faq_loop->the_post();
$category_faq = the_category();
$cat_slug_faq = $category_faq[0]->cat_name;
echo $cat_slug_faq[0]->cat_name;
if ($cat_slug_faq == $cat_slug) {
echo "<h4>" . get_the_title() . "</h4>";
echo the_content();
}
endwhile;
?>
<?php
endwhile; ?>
FAQ自定义帖子类型
add_action( 'init', 'create_faqs' );
function create_faqs() {
register_post_type( 'faqs',
array(
'labels' => array(
'name' => 'FAQs',
'singular_name' => 'FAQ',
'add_new' => 'Add New',
'add_new_item' => 'Add New Faq',
'edit' => 'Edit',
'edit_item' => 'Edit Faq',
'new_item' => 'New Faq',
'view' => 'View',
'view_item' => 'View Faq',
'search_items' => 'Search FAQs',
'not_found' => 'No FAQs found',
'not_found_in_trash' => 'No faqs found in Trash',
'parent' => 'Parent Faq'
),
'public' => true,
'menu_position' => 15,
// 'menu_icon' => 'dashicons-editor-help',
'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
'taxonomies' => array( '' ),
'menu_icon' => 'dashicons-editor-help',
'has_archive' => true
)
);
}
function reg_cat() {
register_taxonomy_for_object_type('category','faqs');
}
add_action('init', 'reg_cat');
你的代码中有很多错误。
the_category()
不返回任何东西,它响应术语链接。the_content()
也有回声,所以你不应该在这个函数之前添加echo
。the_content()
不应该被<p></p>
标签包裹。- 您没有在自定义查询中使用
wp_reset_postdata()
。 - 你的逻辑不符合你的要求。
- 你的代码中缺少必要的验证。
有一个简化的需求代码。
注意:我还没有测试代码,所以可能有拼写错误和错误,可以创建错误,所以确保你有FTP访问修改文件来修复错误,如果你使用WordPress文件编辑器然后测试代码在本地主机或其他地方,然后上传在现场网站
<?php
while ( have_posts() ) :
the_post();
?>
<h1 class="page-title"><?php the_title(); ?></h1>
<div class="post-content"><?php the_content(); ?></div>
<?php get_template_part( 'widgets/cta' ); ?>
<?php vh_display_post_faqs( get_the_ID() ); ?>
<?php endwhile; ?>
以上代码是您的single.php
模板文件,我添加了vh_display_post_faqs()
,它接受post id并在执行验证后输出faq内容。
下面的代码将进入functions.php
。
<?php
/**
* Display faq lists from matching post categories.
*
* @param int $post_id Post ID.
*/
function vh_display_post_faqs( $post_id = 0 ) {
// Return if post id is empty.
if ( empty( $post_id ) ) {
return;
}
// Get the categories' ids using the passed post id.
$categories_ids = wp_get_post_terms( $post_id, 'category', array( 'fields' => 'ids' ) );
// Validate for empty ids or wp errors.
if ( empty( $categories_ids ) || is_wp_error( $categories_ids ) ) {
return;
}
// Prepare query args, using tax query.
$query_args = array(
'post_type' => 'faqs',
'posts_per_page' => 2,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories_ids,
),
);
// Run query and fetch faq posts.
$faqs = new WP_Query( $query_args );
// Check if we have posts from the query.
if ( $faqs->have_posts() ) :
// Loop through the query posts.
while ( $faqs->have_posts() ) :
$faqs->the_post();
// Print the content.
?>
<div class="faq-post">
<h2 class="faq-title"><?php the_title(); ?></h2>
<div class="faq-content"><?php the_content(); ?>
</div>
<?php
endwhile;
// Reset the post data.
wp_reset_postdata();
endif;
}