[英]WP Custom Tabs System- Multiple Loops for Multiple Custom Post Types
我现在正在建立一个Wordpress网站,并试图在一个页面上创建一个标签结构,在这里我可以创建一种子菜单并显示4个不同的自定义帖子类型循环,具体取决于哪个菜单项。已选择。 我已经能够获得与foreach循环一致的nav-items块,并且内容块可以正确切换,但是现在,每个内容div都显示我拥有的每个自定义帖子类型的所有帖子。
我已经试验了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>
我想我知道需要做什么 ,而是如何在这里我卡住了。 很感谢任何形式的帮助!
据我所知,您无需在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.