【发布时间】:2018-02-05 02:35:17
【问题描述】:
我现在正在构建一个 Wordpress 网站,我正在尝试在单个页面上创建一个选项卡结构,我可以在其中创建一种子菜单并显示 4 个不同的自定义帖子类型循环,具体取决于什么菜单项被选中。我已经能够获得与 foreach 循环一致的 nav-items 块,并且内容块正在正确切换,但是现在,每个内容 div 都显示了我拥有的每个自定义帖子类型的所有帖子。
我已经尝试过 switch 语句和 if 语句以及 is_singular 条件标记,但到目前为止没有任何工作正常
我在代码中加入了 cmets 来为自己描述每个部分在做什么,我认为它们可能会帮助您进入我的大脑空间。
<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>
我想我知道什么需要做,但是我被困在了哪里。很感谢任何形式的帮助!
【问题讨论】:
标签: php wordpress loops custom-post-type