我想使用它,如果它们是滑块,那么显示它。如果它们没有滑块,那么它将显示一个图像。我的代码是这样的。但他们只是显示图像,虽然他们是一个滑块图像。如果我删除带有条件的图像部分,则显示滑块。我的代码中有什么问题。有人请回答吗?
<div class="slideshow">
<?php if (has_post_thumbnail('slider-image')) : ?>
<div data-ride="carousel" class="carousel slide" id="carousel-example-generic">
<ol class="carousel-indicators">
<?php
global $post;
$args = array( 'posts_per_page' => 4, 'post_type'=> 'slider-items');
$myposts = get_posts( $args );
$count = 0;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li data-target="#carousel-example-generic" data-slide-to="<?php echo $count++;?>"></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ol>
<div class="carousel-inner">
<?php
global $post;
$args = array( 'posts_per_page' => 4, 'post_type'=> 'slider-items');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<div class="item">
<?php the_post_thumbnail('slider-image'); ?>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
</div>
</div>
<?php else :?>
<div class="fixed_slider">
<img src="<?php echo get_template_directory_uri(); ?>/images/slider-fixed.jpg" alt="" />
</div>
<?php endif;?>
</div>
答案 0 :(得分:0)
您不必一遍又一遍地收到相同的帖子。
要检查滑块项目帖子类型中是否有帖子,您首先会获得if语句之外的帖子。然后,检查是否有任何帖子。如果有,请执行滑块,修复图像。
尝试这样的事情。
<div class="slideshow">
<?php
$args = array( 'posts_per_page' => 4, 'post_type'=> 'slider-items');
$sliderposts = get_posts( $args );
if ( count($sliderposts) > 0) : ?>
<div data-ride="carousel" class="carousel slide" id="carousel-example-generic">
<ol class="carousel-indicators">
<?php
$count = 0;
foreach( $sliderposts as $post ) : setup_postdata($post); ?>
<li data-target="#carousel-example-generic" data-slide-to="<?php echo $count++;?>"></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ol>
<div class="carousel-inner">
<?php
foreach( $sliderposts as $post ) : setup_postdata($post); ?>
<div class="item">
<?php the_post_thumbnail('slider-image'); ?>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
</div>
</div>
<?php else :?>
<div class="fixed_slider">
<img src="<?php echo get_template_directory_uri(); ?>/images/slider-fixed.jpg" alt="" />
</div>
<?php endif;?>
</div>