Why is this query not working when another is present? - php

This is driving me nuts, I'm sure it's something stupid but I'm too burnt and can't find it. I'm building a custom WP theme which has 2 sliders, one is the default Bootstrap slider and the other is an Owl Slider. Before you tell me "why don't you use 2 instances of Owl slider?", I already thought of that, tested and works. However, I'm very curious as to why is this not working. Here's my code:
BOOTSTRAP Carousel:
<section id="panel4">
<div class="container">
<h2>Testimonials</h2>
</div>
<!-- container -->
<div class="container-fluid">
<?php $queryObject=new WP_Query( 'post_type=testimonial' ); // The Loop! if ($queryObject->have_posts()) { ?>
<div id="carousel-testimonial" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php while ($queryObject->have_posts()) {$queryObject->the_post(); ?>
<div class="item ">
<div class="container">
<div class="row">
<div class="col-sm-3 col-md-2 thumb-box">
<img class="thumb" src="<?php echo types_render_field( " testimonial-avatar ", array( url => "true ", "width " => "400 ", "height " => "300 ", "proportional " => "true " ) );?>">
</div>
<div class="col-sm-9 col-md-10">
<h3>"<?php echo types_render_field("testimonial-excerpt", array("argument1"=>"value1","argument2"=>"value2","argument2"=>"value2")); ?>"</h3>
<blockquote>
<?php echo types_render_field( "testimonial-text", array( "argument1"=>"value1","argument2"=>"value2","argument2"=>"value2")); ?></blockquote>
<p class="sig">
<?php echo types_render_field( "testimonial-name", array( "argument1"=>"value1","argument2"=>"value2","argument2"=>"value2")); ?></p>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-testimonial" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-testimonial" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!-- .carousel -->
</div>
<!-- .container -->
</section>
and then OWL SLIDER
<?php if( have_rows( 'slider_photos') ): ?>
<div class="container-fluid">
<div id="happy" class="owl-carousel">
<?php while( have_rows( 'slider_photos') ): the_row(); // vars $image=g et_sub_field( 'slider_image'); ?>
<div class="item-owl">
<img class="img-responsive" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</div>
<?php endwhile; ?>
</div>
<!-- .carousel -->
</div>
<?php endif; ?>
Now, the problem seems to be in Bootstrap. If I take it off, Owl slider works. If I add 2 instances of Owl Slider, it works. If I add Bootstrap Carousel, only Bootstrap works and Owl displays nothing (not even in code, it's as if it didn't even exist)
Can anyone tell me what is going on and how to fix this?

Most likely you need to call wp_reset_postdata() before have_rows( 'slider_photos') in the Owl slider, to restore the global $post object, that you override in your Bootstrap carousel loop.
The have_rows('slider_photos') depends on the global $post object.
Another option would be to use have_rows( 'slider_photos', $post_id ) to specify what $post_id you want to use.

Related

Dynamic HTML Classes PHP and WordPress

Can you please advise which is the most appropriate way to append classes dynamically to the 's below based on the ID of the post?
The goal is to add a class to some of the div's depending on the ID of the post being even or odd.
<div class="service__preview">
<div class="row mb-5">
<div class="col-4">
<div class="row">
<div class="col-5 // here I want to dynamically add order-2 if get_the_ID() is even">
<div class="service__preview service__preview-id">
<?php echo get_the_ID() + 1;?>
</div>
</div>
<div class="col-7 // here I want to dynamically add order-1 if get_the_ID() is even">
<div class="service__preview service__preview-icon">
<div class="icon__container icon__container-3x">
<div class="icon__container icon__container-2x">
<div class="icon__container icon__container-1x">
<i class="<?php echo get_post_meta($post->ID, 'service_icon', true); ?> fa-2x"></i>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-1">
</div>
<div class="col-6">
<a class="service__preview service__preview-title" href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<p class="service__preview service__preview-description"><?php the_content(); ?></p>
<a class="service__preview service__preview-link" href=" <?php the_permalink(); ?>"><p>Learn more</p></a>
</div>
</div>
Looking forward to your suggestions.
FYI: the project is a WordPress template.

Why my wordpress loop modifies my html code?

for the first time I'm facing a problem that I can't explain. I'm creating a simple wordpress loop for a gallery but, I don't know why, the loop change my html code inside the loop.
At first, this is what I wrote :
<?php query_posts('posts_per_page=9');
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php
$post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'large', false);
if ($thumbnail)
(string)$thumbnail = $thumbnail[0];
} else {
$thumbnail = './assets/no-picture.jpg';
}
?>
<article class="col-lg-4 col-sm-6">
<a href="#Modal1" data-toggle="modal" class="gallery-box">
<img src="<?php echo $thumbnail; ?>" class="img-responsive" alt="Image">
<div class="gallery-box-caption">
<div class="gallery-box-content">
<div>
<h2><?php the_title(); ?></h2>
<h6 class='wide-space'><?php the_category(); ?></h6>
<i class="icon-lg ion-ios-play-outline"></i>
</div>
</div>
</div>
</a>
</article>
<?php endwhile; ?>
<?php endif; ?>
But when it's launched online, the code inside the loop become this :
<article class="col-lg-4 col-sm-6">
<a href="#Modal1" data-toggle="modal" class="gallery-box">
<img src="https://labelleequipeproductions.com/wp-content/uploads/2020/08/lessines-terre-de-developpement.jpg" class="img-responsive" alt="Image">
</a>
<div class="gallery-box-caption">
<div class="gallery-box-content">
<a href="#Modal1" data-toggle="modal" class="gallery-box">
</a>
<div>
<a href="#Modal1" data-toggle="modal" class="gallery-box">
<h2>Lessines, Terre de développement</h2>
</a>
<h6 class="wide-space">
<ul class="post-categories">
<li>
Corporate
</li>
</ul>
</h6>
<i class="icon-lg ion-ios-play-outline"></i>
</div>
</div>
</div>
</article>
Are there someone who could help me on this ? I never seen this before and I often use this kind of loop.

how to show blog featured image on hover css?

I've almost finished my first custom theme for a website. I've created some custom post type to manage a portfolio and a staff member section. No problem with the code, but the client asked if is it possible to change the staff images when an user hover on it. Is there any way to achieve this in wordpress?
Here is a sample of my code
<div class="team-boxed">
<?php
$args = array(
'post_type' => 'team'
);
$team = new WP_Query($args);
?>
<div class="intro">
<h2 class="text-center text-uppercase" id="">Team</h2>
</div>
<div class="row people">
<?php if( $team->have_posts() ): while( $team->have_posts() ): $team->the_post(); ?>
<div class="col-md-6 col-lg-4 item">
<div class="box">
<div class="rounded-circle" style="background-image:url('<?php the_post_thumbnail_url('full'); ?>')" id="staff-pic"></div>
<h3 class="team-name"><?php the_title(); ?></h3>
<p class="description text-center"><?php echo get_the_content(); ?></p>
<div class="social"><i class="fab fa-facebook"></i><i class="fab fa-twitter"></i><i class="fab fa-instagram"></i></div>
</div>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
Simplest way would be to output that like this. If you have ALT image stored in custom meta for example you can use that as SECOND IMAGE.
<img src="FIRST IMAGE" onmouseover="this.src='SECOND IMAGE'" onmouseout="this.src='FIRST IMAGE'" />
Other options include outputting some additional CSS or JS. But you can also use general JS with data attributes maybe to handle that with more options, animations etc.

WordPress advanced custom fields, loop though repeater

What I have
I have a repeater setup in Advanced custom fields and bootstrap slider in my code.
What I trying to achieve
I would like to add a image and text in my WordPress repeater and have it populate my slider. so that each new repeater row has its own image and text.
I have the image working, but not the text so at the moment every slide has its own image, but for some reason I have all the text displaying for each slide.
bellow is the layout of my ACF repeater
bellow is my current slider
so I have 3 images that show on the correct slides but as you can see my content is showing all not just the content related for the slide
My code
<?php
$sliderImages = get_field('section_8_slider');
$count = 0;
$section = "section_8_";
$order = array();
?>
<div class="col-lg-8 mb-3">
<div class="carousel-inner">
<?php foreach ($sliderImages as $imgNumber => $image): ?>
<div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
<img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">
<?php if (have_rows($section . 'slider')):
while (have_rows($section . 'slider')):
the_row(); ?>
<?php
foreach ($sliderImages as $i => $row)
if ($sliderImages ): ?>
<div class="carousel-text-block">
<h4 class="mb-1"> <?php echo $row['content_title']; ?></h4>
<p class="small m-0"> <?php echo $row['content']; ?></p>
</div>
<?php endif; ?>
<?php endwhile;
endif; ?>
</div>
<?php endforeach ?>
</div>
<a class="carousel-control-prev" href="#carousel_03" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
<a class="carousel-control-next" href="#carousel_03" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
</div>
You are looping each field 2 times. You could use get_sub_field() in order to simplify your code, try something like this:
<div class="carousel-inner">
<?php if (have_rows('section_8_slider')):
while (have_rows('section_8_slider')): the_row();
$image = get_sub_field('image');
$content_title = get_sub_field('content_title');
$content = get_sub_field('content');?>
<div class="carousel-item">
<img src="<?= $image['url'] ?>" alt="<?= $image['alt'] ?>">
<div class="carousel-text-block">
<h4 class="mb-1"> <?= $content_title;?></h4>
<p class="small m-0"> <?= $content;?></p>
</div>
</div>
<?php endwhile;
endif; ?>
Hope it helps
I have managed to solve it myself, i was close, please see answer bellow
<div class="carousel-inner">
<?php foreach ($sliderImages as $imgNumber => $image): ?>
<div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
<img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">
<div class="carousel-text-block">
<h4 class="mb-1"> <?php echo $image['content_title']; ?></h4>
<p class="small m-0"> <?php echo $image['content']; ?></p>
</div>
</div>
<?php endforeach ?>
</div>

Wordpress Bootstrap carosel image not displaying

I am building my Wordpress theme with Bootstrap and am using a carousel on my front page right below the navbar. I found a great way to loop through the images here [1]: http://www.lanexa.net/2012/04/how-to-make-bootstrap-carousel-display-wordpress-dynamic-content/
I wanted to run a test on one of the slider areas so I put in the code for the first loop and it displays the title and the excerpt but my image does not show up at all. How can I fix this issue and what is a good way to loop through the rest of the images in the carousel. I added the second loop with the code I found in the link and it messed up my page so I took it out. Here is what I have so far still with some static content that I want to loop through as well. I am going to have 3 images going in the carousel. This is my first real project with Wordpress as I am trying to build my portfolio theme so any help is appreciated!
<!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<?php
$the_query = new WP_Query(array(
'category_name' => 'Home Carousel',
'posts_per_page' => 1
));
while ($the_query->have_posts() ) :
$the_query->the_post();
$attachment_ids[] = get_the_ID();
endwhile;
wp_reset_postdata();
?>
<!-- Indicators -->
<ol class="carousel-indicators">
<?php foreach($attachment_ids as $attachment_id_k=>$attachment_id_v ){
if($attachment_id_k == 0){$class_active = 'class="active"';}
else{$class_active = 'class=""';}
?>
<li data-target="#myCarousel" <?php echo $class_active;?> data-slide-to="<?php echo $attachment_id_k;?>"></li>
<?php } ?>
</ol>
<div class="carousel-inner" role="listbox">
<div class="carousel-inner">
<?php foreach($attachment_ids as $attachment_id_k=>$attachment_id_v ){
if($attachment_id_k == 0){$class_active = 'class="item active"';}
else{$class_active = 'class="item"';}
?>
<div <?php echo $class_active;?>>
<div class="display_table">
<div class="display_table_cell">
<?php
$default_attr = array(
'class' => "gallery_images",
'alt' => 'Gallery Images',
);
echo wp_get_attachment_image( $attachment_id_v, 'full', 1, $default_attr );
$image_data = get_post( $attachment_id_v );
if($image_data->post_excerpt != ''){
?>
<div class="carousel-caption my_caption">
<p><?php echo $image_data->post_excerpt;?></p>
</div>
<?php }?>
</div>
</div>
</div>
<?php } ?>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div><!-- /.carousel -->
Here is the Working Code !!!
First through the while loop get all the post IDS in an array
<?php
$the_query = new WP_Query(array(
'category_name' => 'Home Carousel',
'posts_per_page' => 1
));
while ($the_query->have_posts() ) :
$the_query->the_post();
$attachment_ids[] = get_the_ID();
endwhile;
wp_reset_postdata();
?>
Now through the array $attachment_ids get the carousel working
<div id="myCarousel" class="carousel slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php foreach($attachment_ids as $attachment_id_k=>$attachment_id_v ){
if($attachment_id_k == 0){$class_active = 'class="active"';}
else{$class_active = 'class=""';}
?>
<li data-target="#myCarousel" <?php echo $class_active;?> data-slide-to="<?php echo $attachment_id_k;?>"></li>
<?php } ?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php foreach($attachment_ids as $attachment_id_k=>$attachment_id_v ){
if($attachment_id_k == 0){$class_active = 'class="item active"';}
else{$class_active = 'class="item"';}
?>
<div <?php echo $class_active;?>>
<div class="display_table">
<div class="display_table_cell">
<?php
$default_attr = array(
'class' => "gallery_images",
'alt' => 'Gallery Images',
);
echo wp_get_attachment_image( $attachment_id_v, 'full', 1, $default_attr );
$image_data = get_post( $attachment_id_v );
if($image_data->post_excerpt != ''){
?>
<div class="carousel-caption my_caption">
<p><?php echo $image_data->post_excerpt;?></p>
</div>
<?php }?>
</div>
</div>
</div>
<?php } ?>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
put this javascript code at footer.php, since its bootstrap carousel so need some javascript to set the options
<script type="text/javascript">
$('.carousel').carousel({
interval: 3000
})
</script>
In your code item section you write
<?php the_post_thumbnail('full');?>
but I found that in that tutorial link they used
<?php the_post_thumbnail('large');?>.
Can you check it. Is this the reason for your error ?

Categories