我创建了一个高级自定义字段,允许编辑器添加要在Bootstrap 3旋转木马中显示的图像和标题。我对图像字段的条件逻辑有问题。我们使用的脚本旨在使用在高级自定义字段中添加的图像,如果高级自定义字段中没有图像,则使用帖子的特色图像。标题也是如此。如果在高级自定义字段中输入了标题,则会使用该标题。如果高级自定义字段中没有添加标题,则使用帖子标题。
标题部分按预期工作,但图像部分不工作;即使将图像添加到高级自定义字段,它也始终使用特征图像。我怀疑旋转木马代码中使用的条件逻辑有问题,但我无法找出问题所在。我已经在下面发布了脚本,如果您能提供任何指导,我将不胜感激。
提前感谢您的帮助。
// Based on code originally developed by Ben Blakley. See http://www.charityandbiscuits.com/blog/create-bootstrap-wordpress-carousel-tagscategories-featured-images/
// Custom loop for featured items in Bootstrap Carousel.
// Slider will show posts in the \'Featured\' category
// Exported PHP from Advanced Custom Fields plugin. Creates custom fields which are added to post, page, and event editors.
if(function_exists("register_field_group"))
{
register_field_group(array (
\'id\' => \'acf_featured-posts-slides\',
\'title\' => \'Featured posts slides\',
\'fields\' => array (
array (
\'key\' => \'field_5270201a54e4a\',
\'label\' => \'Image\',
\'name\' => \'carousel-image\',
\'type\' => \'image\',
\'instructions\' => \'Upload an image here if you don\\\'t want to use the Featured Image\',
\'save_format\' => \'id\',
\'preview_size\' => \'home-slider\',
\'library\' => \'all\',
),
array (
\'key\' => \'field_527020aa54e4b\',
\'label\' => \'Title\',
\'name\' => \'carousel-title\',
\'type\' => \'text\',
\'instructions\' => \'Put a short title here if you don\\\'t want the image slider to use the Post/Page Title\',
\'default_value\' => \'\',
\'placeholder\' => \'\',
\'prepend\' => \'\',
\'append\' => \'\',
\'formatting\' => \'none\',
\'maxlength\' => 83,
),
),
\'location\' => array (
array (
array (
\'param\' => \'post_type\',
\'operator\' => \'==\',
\'value\' => \'post\',
\'order_no\' => 0,
\'group_no\' => 0,
),
),
array (
array (
\'param\' => \'post_type\',
\'operator\' => \'==\',
\'value\' => \'page\',
\'order_no\' => 0,
\'group_no\' => 1,
),
),
array (
array (
\'param\' => \'post_type\',
\'operator\' => \'==\',
\'value\' => \'event\',
\'order_no\' => 0,
\'group_no\' => 2,
),
),
),
\'options\' => array (
\'position\' => \'side\',
\'layout\' => \'default\',
\'hide_on_screen\' => array (
),
),
\'menu_order\' => 0,
));
}
//Add a WordPress image size if the slider image size isn\'t already set in WordPress.
add_image_size( \'home-slider\', \'750\', \'410\', true );
//Creates a shortcode so you can just type [carousel] wherever you want the slideshow displayed.
function carousel_shortcode(){
$number = 0;
$q = query_posts( array ( \'category_name\' => \'featured\', \'posts_per_page\' => 6, \'post_type\' => array ( \'post\', \'page\',\'event\') ) );
if(have_posts()):
//If no thumbnail or carousel-image exists leave it out of the loop to avoid blank slides.
//If carousel-image set, use it, otherwise use featured image thumbnail(\'home-slider).
//If carousel-title set, use it as slide title, otherwise use the_title()
?>
<div id="myCarousel" class="carousel slide carousel-fade">
<ol class="carousel-indicators">
<?php while(have_posts()): the_post(); ?>
<?php
$postIdl = get_the_ID();
$has_featured_imagel = has_post_thumbnail( $postIdl );
$carousel_imagel = get_field(\'carousel-image\');
$carousel_image_urll = $carousel_imagel[\'url\'];
if($carousel_image_urll != \'\' || $has_featured_imagel)
{
?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php } endwhile; ?>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<?php while(have_posts()): the_post(); ?>
<?php
$slider_image = \'\';
$slider_title = \'\';
$postId = get_the_ID();
$has_featured_image = has_post_thumbnail( $postId );
$carousel_title = get_post_meta($postId, \'carousel-title\', $single = true);
$carousel_image = get_field(\'carousel-image\');
$carousel_image_url = $carousel_image[\'url\'];
if($carousel_title != \'\')
{
$slider_title = $carousel_title;
}
else
{
$slider_title = get_the_title();
}
if($carousel_image_url != \'\')
{
$attachment_id = get_field(\'carousel-image\');
$size = \'home-slider\';
$thumb = wp_get_attachment_image_src( get_field(\'carousel-image\'), \'home-slider\' );
$url = $thumb[\'0\'];
$slider_image = \'<img src="\'.$url.\'">\';
}
else
{
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'home-slider\' );
$url = $thumb[\'0\'];
$slider_image = \'<img src="\'.$url.\'">\';
}
if($carousel_image_url != \'\' || $has_featured_image)
{
?>
<div class="item">
<img src="<?php echo $url; ?>">
<a href="<?php the_permalink(); ?>"><div class="carousel-caption">
<h3 style="text-transform: uppercase"><?php echo $slider_title; ?></h3>
</div></a>
</div>
<?php } ?>
<?php endwhile; ?>
</div>
<!-- Carousel nav -->
<a id="left" class="carousel-control left" href="#myCarousel" data-slide="prev"><i class="fa fa-chevron-left icon-2x"></i></a>
<a id="right" class="carousel-control right" href="#myCarousel" data-slide="next"><i class="fa fa-chevron-right icon-2x"></i></a>
</div>
<?php endif; wp_reset_query();
}
add_shortcode(\'carousel\', \'carousel_shortcode\');
//Add js to footer. Change the interval to alter how long the slides display - 4000 equals 4 seconds.
function featured_carousel() { ?>
<script>
jQuery(document).ready(function($){
$("#myCarousel .carousel-indicators li:first").addClass("active");
$("#myCarousel .carousel-inner .item:first").addClass("active");
$("#myCarousel").carousel({
interval: 4000
})
});
</script>
<?php
}
add_action(\'wp_footer\', \'featured_carousel\');```