我正在构建一个Wordpress网站,我正在尝试在单个页面上创建一个标签结构,我可以在其中创建一种子菜单并显示4种不同的自定义后期类型循环,具体取决于菜单项已被选中。我已经能够使nav-items块与foreach循环一致,并且内容块正确切换,但是现在,每个内容div都显示我拥有的每个自定义帖子类型的所有帖子。 / p>
我已经尝试过switch语句和if语句以及is_singular条件标记,但到目前为止没有任何工作正常
我在代码中添加了注释,以便为自己描绘每个部分正在做什么,认为它们可能会帮助您进入我的大脑空间。
<ul class="tab" role="tablist">
<?php
//All public posts that are not built into Wordpress
$argsnav = array(
'public' => true,
'_builtin' => false,
);
$output = 'objects'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
//Get the Post Types of each post as an object
$post_types = get_post_types( $argsnav, $output, $operator );
//Remove Product from Array
unset( $post_types ['product'] );
//Iterate through each post type
foreach ($post_types as $post_type) {
//And print out a list item with the corresponding ID and text
echo '<li>
<a href="#' . $post_type->name . '" role="tab" data-toggle="tab">
'. $post_type->label .'
</a>
</li>';
}
echo '</ul>';
?>
//New section for post content/ This is the buggy portion
<div class="tab-content">
<?php
//Iterate through each post type
foreach($post_types as &$post_type) { ?>
<!--Create div with corresponding id-->
<div class="tab-pane" id="<?php echo "$post_type->name" ?>">
<!--Create new query for all posts of each type-->
<?php $loop = new WP_Query(
array(
'post_type' => array('standup', 'novels', 'short_stories', 'music'),
));
//Loop through and display each post's title
if ( $loop->have_posts()) :
while ( $loop->have_posts() ) :
$loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<!--Stop the loop-->
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php } wp_reset_postdata(); ?>
</div>
我想我知道需要做什么,但我是如何陷入困境的。很感谢任何形式的帮助!
答案 0 :(得分:0)
我觉得,你不需要在wp_query中设置所有post_type,请试试这个
<!--Create new query for all posts of each type-->
<?php $loop = new WP_Query(
array(
'post_type' => $post_type->name,
));
//Loop through and display each post's title