我需要能够显示来自4种不同自定义帖子类型的最新帖子,然后在旋转木马中使用这些帖子。一切都很好,但我对代码的外观不满意,因为我在重复所有内容。
首先,我对不同的自定义帖子类型进行了4种不同的查询:
<!--recipes -->
<?php
$recipesQuery = new WP_Query( array(
\'post_type\' => \'recipes\',
\'posts_per_page\' => 1
) );
?>
<!--value -->
<?php
$valueQuery = new WP_Query( array(
\'post_type\' => \'value_chain\',
\'posts_per_page\' => 1
) );
?>
<!--press-->
<?php
$pressQuery = new WP_Query( array(
\'post_type\' => \'press\',
\'posts_per_page\' => 1
) );
?>
<!--multimedia -->
<?php
$mediaQuery = new WP_Query( array(
\'post_type\' => \'multimedia\',
\'posts_per_page\' => 1
) );
?>
然后我重复这个代码块4次,每种帖子类型一次:
<?php if ( $valueQuery->have_posts() ) :
while ( $valueQuery->have_posts() ) : $valueQuery->the_post(); ?>
<div class="item">
<div class="row">
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\' );?>
<?php if( $thumb ):?>
<div class="col-md-6 col-sm-6 col-xs-12 back">
<div class="back"
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\' );?>
style="background-image: url(\'<?php echo $thumb[\'0\'];?>\');">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="info">
<h3>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h3>
<hr>
<div class="excerpt"><p></p></div>
<div class="row">
<div class="col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="read-more">
<?php echo _e(\'Leer más\', \'myCustomTemplate\'); ?>
</a>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="col-md-12 col-sm-12 col-xs-12 no-bg">
<div class="info">
<h3>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h3>
<hr>
<div class="excerpt"><p><?php echo wp_trim_words( get_the_content(), 100 ); ?></p></div>
<div class="row">
<div class="col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="read-more">
<?php echo _e(\'Leer más\', \'myCustomTemplate\'); ?>
</a>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile;
endif; wp_reset_postdata(); ?>
有没有更好的方法?我知道我可以创建带有自定义帖子类型的数组,但不确定如何使用它创建旋转木马项目。
最合适的回答,由SO网友:Bram 整理而成
下面的示例是循环中的代码。这个post_types
数组包括您要查询的所有帖子类型。然后,下一位(foreach
, 更多信息here) 运行用大括号括起来的代码({
到}
) 对于其中的每个项目post_types
-大堆
post_types = array(\'recipes\', \'value_chain\', \'press\', \'multimedia\');
foreach($post_types as $post_type){
$query = new WP_Query ( array(
\'post_type\' => $post_type,
\'posts_per_page\' => 1
) );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="item">
<div class="row">
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\' );?>
<?php if( $thumb ):?>
<div class="col-md-6 col-sm-6 col-xs-12 back">
<div class="back"
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\' );?>
style="background-image: url(\'<?php echo $thumb[\'0\'];?>\');">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="info">
<h3>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h3>
<hr>
<div class="excerpt"><p></p></div>
<div class="row">
<div class="col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="read-more">
<?php echo _e(\'Leer más\', \'myCustomTemplate\'); ?>
</a>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="col-md-12 col-sm-12 col-xs-12 no-bg">
<div class="info">
<h3>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h3>
<hr>
<div class="excerpt"><p><?php echo wp_trim_words( get_the_content(), 100 ); ?></p></div>
<div class="row">
<div class="col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="read-more">
<?php echo _e(\'Leer más\', \'myCustomTemplate\'); ?>
</a>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile;
endif; wp_reset_postdata();
}
?>