Wordpress Loop repeating twice - wordpress

Hello I have a while loop in WordPress and I am adding an active class on the first instance of the loop and this works fine but the same instance then again repeats afterwards without the active class.
here is my code any help would be greatly appreciated.
<div class="carousel-inner" role="listbox">
<?php
query_posts( array( 'post_type' => 'deal','dealtype' => 'deals' ) );
while (have_posts()) : the_post();
?>
<?php if( $wp_query->current_post == 0 && !is_paged() ) { ?>
<div class="item active">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php } ?>
<div class="item">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php endwhile; // end of the loop. ?>
</div>

Believe you need to add an else statement after your <?php if ($wp_query->current_post == 0 && !is_paged() ) { ?> statement ends. Should look something like this:
<div class="carousel-inner" role="listbox">
<?php
query_posts( array( 'post_type' => 'deal','dealtype' => 'deals' ) );
while (have_posts()) : the_post();
?>
<?php if ( $wp_query->current_post == 0 && !is_paged() ) { ?>
<div class="item active">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php } else { ?>
<div class="item">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php } ?>
<?php endwhile; // end of the loop. ?>
</div>

Related

While implementing this code on my wp-admin the content is okay but in the customers views the slider is not okay and the image is strechd/ distorted

/img/scrubber-pov.png" id="pov-robot" alt="Robot Perspective" />
" style="background: url();">
/img/scrubber-pov.png" class="inside-slide-robot" alt="Robot Perspective" />
<?php if( have_rows('technology_slides') ):?>
<div class="mobile-slider-presentation">
<?php $slide_count = 1;
while( have_rows('technology_slides') ): the_row();
$slide_background = get_sub_field('slide_background');
$slide_content = get_sub_field('slide_content');?>
<div class="slide-background" id="slideNumber-<?php echo $slide_count;?>" style="background: url(<?php echo $slide_background['url'];?>);">
<div class="container-fluid content-container">
<div class="row">
<div class="col-xl-4 offset-xl-8">
<div class="slide-copy">
<?php echo $slide_content;?>
</div>
</div>
</div>
<img src="<?php bloginfo('template_url'); ?>/img/scrubber-pov.png" class="inside-slide-robot" alt="Robot Perspective" />
</div>
</div>
<?php $slide_count = $slide_count + 1;?>
<?php endwhile; reset_rows();?>
</div>
<?php endif; ?>
/img/scrubber-pov.png" id="pov-robot" alt="Robot Perspective" />

How to insert some code in the else loop of wordpress "the loop"?

<section class="container-fluid jp-section jp-bg-red jp-color-white h-m-md-auto first" data-section-label="Get Paid">
<div class="row no-gutters h-100 h-m-md-auto">
<div class="col-lg-6 col-12 ph-100 h-m-md-auto jp-p-l-0 jp-p-r-0 jp-p-t-90 jp-p-b-80 jp-md-p-t-20 jp-md-p-b-20">
<div class="vertical-title h-100 float-left">
<div class="wrapper">
<h2>YOUTH ARTISTS</h2>
</div>
</div>
<?php query_posts('post_type=jxta_shop&posts_per_page=6'.'&paged='.$paged);?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>
<div class="misson-wrapper h-100 h-m-md-auto">
<div class="row h-100 h-m-md-auto">
<div class="col-12">
<div class="owl-carousel owl-theme" data-childslider="#homeSectionImageSlider" data-dots="#homeSectionDots">
<div class="item jp-bg-red">
<p class="jp-font-journal display-3 text-uppercase"><?php echo'<p class="jp-font-journal display-3 text-uppercase" style="width:80%;" >'. get_field('slider_header_1') .'</p>' ?>
</p>
<p class="h2 jp-font-journal mb-4"><?php echo'<p class="h2 jp-font-journal mb-4" style="width:80%;" >'. get_field('slider_description_1') .'</p>' ?>
</p>
<p class="h2 jp-font-journal"></p>
<?php
$slider_button_link_1 = get_field('slider_button_link_1');
if( $slider_button_link_1 ): ?>
<a class="btn btn-outline-white rounded-0" style="color:#000;" href="<?php echo $slider_button_link_1; ?>"><b>READ MORE</b></a>
<?php endif; ?>
<br/>
</div>
<div class="item jp-bg-red">
<p class="jp-font-journal display-3 text-uppercase"><?php echo'<p class="jp-font-journal display-3 text-uppercase" style="width:80%;" >'. get_field('slider_header_2') .'</p>' ?></p>
<p class="h2 jp-font-journal mb-4"><?php echo'<p class="h2 jp-font-journal mb-4" >'. get_field('slider_description_2') .'</p>' ?></p>
<?php
$slider_button_link_2 = get_field('slider_button_link_2');
if( $slider_button_link_2 ): ?>
<a class="btn btn-outline-white rounded-0" style="color:#000;" href="<?php echo $slider_button_link_2; ?>"><b>READ MORE</b></a>
<?php endif; ?>
</div>
</div>
</div>
<div id="homeSectionDots" class="col-12 align-self-end jp-owl-dots white"></div>
</div>
</div>
<div class="col-lg-6 ph-100 col-12">
<div id="homeSectionImageSlider" class="owl-carousel owl-theme h-100">
<div class="item h-100 bg-cover bg-bottom" style="background-image:url('<?php the_field( 'slider_image_1'); ?>')">
</div>
<div class="item h-100 bg-cover bg-bottom" style="background-image:url('<?php the_field( 'slider_image_2'); ?>')">
</div>
</div>
</div>
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
</div>
</div>
</section>
This is my code. I am using WordPress loop to show the post content. But I want to put some codes in the else loop. Right now if I put any codes in the else loop, it is not displaying like the if loop. It is showing "Sorry, nothing to display." I want to put the following code in the else loop.
<div class="item jp-bg-red">
<p class="jp-font-journal display-3 text-uppercase"><?php echo'<p class="jp-font-journal display-3 text-uppercase" style="width:80%;" >'. get_field('slider_header_1') .'</p>' ?>
</p>
<p class="h2 jp-font-journal mb-4"><?php echo'<p class="h2 jp-font-journal mb-4" style="width:80%;" >'. get_field('slider_description_1') .'</p>' ?>
</p>
<p class="h2 jp-font-journal"></p>
<?php
$slider_button_link_1 = get_field('slider_button_link_1');
if( $slider_button_link_1 ): ?>
<a class="btn btn-outline-white rounded-0" style="color:#000;" href="<?php echo $slider_button_link_1; ?>"><b>READ MORE</b></a>
<?php endif; ?>
<br/>
</div>
How to do it?
Instead of query_posts use WP_Query
$args = array(
'post_type' => 'jxta_shop',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
} else {
}

Wordpress - Advanced Custom Fields gallery

I'm using ACF plugin and i create a flexible content to show title, content and gallery but i can't get the images.
My code is this:
<div id="services-pages">
<?php
if( have_rows('serviços') ):
while ( have_rows('serviços') ) : the_row();
?>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="content">
<div class="title">
<hr>
<span><?php echo the_sub_field('title');?></span>
</div>
<div class="info">
<p><?php echo the_sub_field('content');?></p>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="row images">
<?php
$images = the_sub_field('images');
var_dump($images);
?>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="slider-service-element">
<?php foreach( $images as $image ): ?>
<div>
<img src="<?php echo $image['url']; ?>" />
</div>
<?php endforeach; ?>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="slider-service-main">
<?php foreach( $images as $image ): ?>
<div>
<img src="<?php echo $image['url']; ?>" />
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
endwhile;
else :
?>
<div>
<span>no records</span>
</div>
<?php
endif;
?>
</div>
My ACF field:
I already try to get gallery with get_field() and the_sub_field().
How can i solve that?
Thank you.
Try something like this
$images = get_sub_field('images');
if( $images ):
foreach( $images as $image ):
?><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /><?php
endforeach;
endif;
There's a post on acf forum about it
https://support.advancedcustomfields.com/forums/topic/gallery-field-within-a-repeater-field/
<?php if( have_rows('glp_images') ):
// loop through the rows of data
while ( have_rows('glp_images') ) : the_row();
$images = get_sub_field('glp_img');
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<div>
<img src="<?php echo $image; ?>" alt="" />
</div>
<?php endforeach; endif;?>
<?php endwhile; endif; ?>

Related posts WordPress

I'm looking for a solution to show related posts from a different CPT.
There are two CPT's (Projects and Products) both have multiple sub categories (don't know if that ok...).
When on a: Single Post > Projects CPT > SportsCars
in the footer it must show the latest two posts of: Products CPT > Sportscars
The code i have now just filters on the CPT, not the SubCategrories. This is the code of single_products.php:
<section class="content">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</section>
<div class="container">
<div class="divider-small"></div>
</div>
<section class="news">
<div class="container">
<h2>Related projects</h2>
<div class="row">
<?php
$wp_query = new WP_Query(array('post_type' => 'projects', 'post__not_in' => array( $post->ID ), 'showposts' => 2));
$i = 0;
if ( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="col-md-6 item">
<div class="<?php if($i++ == 1): echo 'green'; else: echo 'blue'; endif; ?> clearfix">
<div class="col-sm-6">
<div class="text">
<h3><?php the_title(); ?></h3>
<p><?php echo excerpt(20); ?></p>
</div> <!-- end text -->
<div class="meer-news">
Lees meer >
</div> <!-- end meer-news -->
</div> <!-- end col-sm-6 -->
<div class="col-sm-6">
<div class="image">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail( 'news_latest' );
else:
// backup
endif;
?>
</div> <!-- end image -->
</div> <!-- end col-sm-6 -->
</div>
</div> <!-- end item -->
<?php endwhile; endif; wp_reset_query(); ?>
</div> <!-- end row -->
</div> <!-- end container -->
</section>

Add div after every three items in a loop?

I am working with a WP website and in my template I am running the loop like this:
<!-- START LOOP -->
<?php while ( have_posts() ) : the_post(); ?>
<div class="row" style="margin:15px 0;">
<div class="twelve columns">
<div class="four columns">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'medium' );
} else {
echo 'No Preview Available';
}
?>
</a>
</div>
<div class="eight columns">
<h3><?php the_title() ?></h3>
<p><?php the_excerpt() ?></p>
<p><?php echo esc_html( get_post_meta( get_the_ID(), 'portfolio_website', true ) ); ?></p>
</div>
</div>
</div>
<hr />
<?php endwhile; ?>
Because this is a responsive website, I am trying to get a grid column look. The problem I am having is how I can insert a div with a class or "row container" after every third item?
I know I probably just confused the crap out of you because I confuse myself but in short the html would look like this:
<div class="row container">
<!-- item 1 -->
<div class="twelve columns">
<div class="four columns">IMAGE HERE</div>
<div class="eight columns">TEXT HERE</div>
</div>
<!-- item 2 -->
<div class="twelve columns">
<div class="four columns">IMAGE HERE</div>
<div class="eight columns">TEXT HERE</div>
</div>
<!-- item 3 -->
<div class="twelve columns">
<div class="four columns">IMAGE HERE</div>
<div class="eight columns">TEXT HERE</div>
</div>
<!-- item 4 -->
<div class="twelve columns">
<div class="four columns">IMAGE HERE</div>
<div class="eight columns">TEXT HERE</div>
</div>
</div>
and so on, instead I would like it to display in a grid so the HTML should look more like this:
<div class="row container">
<!-- row 1 -->
<div class="twelve columns">
<div class="four columns">
<!-- item 1 -->
<div class="four columns">IMAGE HERE</div>
<!-- item 2 -->
<div class="four columns">IMAGE HERE</div>
<!-- item 3 -->
<div class="four columns">IMAGE HERE</div>
</div>
</div>
<!-- row 2 -->
<div class="twelve columns">
<div class="four columns">
<!-- item 4 -->
<div class="four columns">IMAGE HERE</div>
<!-- item 5 -->
<div class="four columns">IMAGE HERE</div>
<!-- item 6 -->
<div class="four columns">IMAGE HERE</div>
</div>
</div>
</div>
I can do everything else Im just not sure how to implement the below so I get the results I pasted above? I have found this online and feel like it is a step in the right direction:
<?php ($i % 3) == 0 ?>
Your feelings are right.
You can use the $current_post property of the WP_Query class to get the index of the post currently displayed in the loop, and then use the modulus operator to make sure you are targeting multiples of three.
So your loop could look like this:
<div class="row container">
<!-- row -->
<div class="twelve columns">
<div class="four columns">
<?php while ( have_posts() ) : the_post(); ?>
<!-- item -->
<div class="four columns">IMAGE HERE</div>
<?php if( $wp_query->current_post % 3 == 0 ) : ?>
</div>
</div>
<!-- row -->
<div class="twelve columns">
<div class="four columns">
<?php endif; ?>
<?php endwhile; ?>
</div>
You might need to improve this implementation. Specifically, make sure that, whatever happens, your HTML closes correctly.
What I needed was a counter:
<!-- START LOOP -->
<?php $counter = 1 ?>
<div class="row" style="margin:15px 0;">
<div class="twelve columns">
<?php while ( have_posts() ) : the_post(); ?>
<div class="four columns">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'medium' );
} else {
echo 'No Preview Available';
}
?>
</a>
</div>
<?php if ($counter % 3 == 0){echo '</div></div></hr /><div class="row" style="margin:15px 0;"><div class="twelve columns">';} ?>
<?php $counter++ ;
endwhile; ?>
</div>
</div>
<?php
$currentPage = get_query_var('paged');
// General arguments
$posts = new WP_Query(array(
'post_type' => 'post', // Default or custom post type
'posts_per_page' => 11, // Max number of posts per page
'paged' => $currentPage
));
// Content display
$i = 0;
if ($posts->have_posts()) :
while ($posts->have_posts()) :
$posts->the_post();
if( $i % 3 == 0 )
echo '<section class="columns">';
echo "<div class='column'>";
?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<?php
echo "</div>";
$i++;
if( $i % 3 == 0 )
echo '</section>';
endwhile;
endif;
?>
<?php
// Bottom pagination (pagination arguments)
echo "<div class='page-nav-container'>" . paginate_links(array(
'total' => $posts->max_num_pages,
'prev_text' => __('<'),
'next_text' => __('>')
)) . "</div>";
?>

Resources