Rerun loop after every third post? (Wordpress) - php

I have a problem in my template (wordpress).
I want to create a portfolio page which contains 3 columns and which can display posts in my portfolio page (without jumping a new page). And I need to repeat these three posts after each third post. I will assign "hidden" class to my duplicate posts and when clicking at the column, class shall set as "block".
I have a code:
<?php get_header(); ?>
<section> <div class="container container-bazar container-gallery"><?php
$array = array();
$count = 0;
$i = 0;
$args = array(
'posts_per_page' => -1,
'post_type' => 'gallery',
);
$gallery = new WP_Query( $args );
if($gallery->have_posts()) :
while($gallery->have_posts()) :
$gallery->the_post(); ?>
<div class="col-1 boxes<?php if( $count%3 == 0 ) { echo '-1'; }; $count++; ?>">
<div class="post" id="post-<?php the_ID(); ?>">
<figure class="indent-bot">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_post_thumbnail(array(380,220,true)); ?>
</a>
</figure>
<div class="col-1-content">
<strong class="title-3">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_title(); ?>
</a>
</strong>
<div class="entry">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_excerpt(); ?>
</a>
</div><!-- .entry -->
</div><!-- .col-1-content-->
</div><!-- .post -->
</div> <!-- .boxes -->
<?php endwhile; ?>
<?php while($gallery->have_posts()) :
$gallery->the_post();?>
<?php $imgaddr1 = get_post_meta($post->ID, 'imgaddr1', true);
$imgaddr2 = get_post_meta($post->ID, 'imgaddr2', true);
$imgssilka1 = get_post_meta($post->ID, 'imgssilka1', true);
$imgssilka2 = get_post_meta($post->ID, 'imgssilka2', true);
$namecolor1 = get_post_meta($post->ID, 'namecolor1', true);
$namecolor2 = get_post_meta($post->ID, 'namecolor2', true);
$numbercolor1 = get_post_meta($post->ID, 'numbercolor1', true);
$numbercolor2 = get_post_meta($post->ID, 'numbercolor2', true); ?>
</div>
<div class="full clearfix">
<div class="inner">
<figure class="indent-bot1">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_post_thumbnail(array(960,690)); ?>
</a>
</figure>
<div class="row">
<div class="col-md-5">
<div class="inf-1">
<h4>Информация</h4>
</div>
<div class="inf-2">
<h5><?php the_title(); ?></h5>
<div class="desc">
<?php the_excerpt(); ?>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="col-md-7 border-left">
<div class="inf-1">
<h4>Приложенные Цвета</h4>
</div>
<div class="inf-2">
<ul>
<li class="first-child">
<a href="<?php echo $imgssilka1; ?>" class="img-block">
<img src="<?php echo $imgaddr1; ?>">
</a>
<div class="txt">
<strong><?php echo $namecolor1; ?></strong>
<span><?php echo $numbercolor1; ?></span>
</div>
</li>
<li class="last-child">
<a href="<?php echo $imgssilka2; ?>" class="img-block">
<img src="<?php echo $imgaddr2; ?>">
</a>
<div class="txt">
<strong><?php echo $namecolor2; ?></strong>
<span><?php echo $numbercolor2; ?></span>
</div>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
</div><!-- .inner -->
</div>
<div class="container container-bazar container-gallery">
<?php endwhile;
else:
endif; ?>
</div><!-- .container -->
</section>
<?php get_footer(); ?>
but this code displays posts sequentially.

$i = 1;
//added before to ensure it gets opened
echo '<div>';
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// post stuff...
// if multiple of 3 close div and open a new div
if($i % 3 == 0) {echo '</div><div>';}
$i++; endwhile; endif;
//make sure open div is closed
echo '</div>';

This is quite an unusual setup which had me thinking. There is a way without rerunning the loop
HERE IS HOW
You need to run your loop only once. In stead of the default loop, we will pull out our posts array from our query and run our posts through a foreach loop. This is where we will start things
We need to split our content up so we can get two blocks with post data, and this need to be saved into an array which we will use later. To achieve this, build two concatenated strings of data ( one string with the first block of data and the second one with the second block of data ) which will be saved in two separate variables.
Once this is done, we need to add our divs to form blocks of posts containing three posts, each with a unique class. This goes for both sets of string
Now we need to calculate new array keys so we can build a new array of post data sorted so we have a sequence of a block of post data with three posts from string one, then a block of post data with the same three posts from string two etc
Finally, because our array of posts is still mixed and is out of order, we will sort the array so the keys are numerical, then we can use a last foreach loop to output our post data
HERE IS THE CODE
Just a note or two before I post the code
You need to modify the classes etc to suite your needs
The code is not fully tested, but the div blocks and sorting works as expected
I have commented the code to make it easier to follow
Finally, the code
$args = array(
'posts_per_page' => -1,
'post_type' => 'gallery',
);
$gallery = new WP_Query( $args );
// Check if we have posts before we continue
if( $gallery->have_posts() ) {
// Use the array of posts and a foreach loop to build out super array
foreach ( $gallery->posts as $key=>$post ) {
// Setup postdata so we can make use of template tags
setup_postdata( $post );
// Setup/define our first variable which will hold our first set of post data
$output = '';
// Open a new div on the first post and every 3rd one there after to hold three posts
if ( $key%3 == 0 ) {
// We will call this class "first-x" where x represents the block count
$output .= '<div class="first-' . floor( $key / 3 ) . '">';
}
// Concatenate your first loop into a string to our first variable $output
$output .= '<div class="post" id="post-' . $post->ID . '">
<figure class="indent-bot">
<a href="' . get_the_permalink() . '" rel="nofollow">
' . get_the_post_thumbnail( $post->ID, array( 380,220,true ) ) . '
</a>
</figure>
<div class="col-1-content">
<strong class="title-3">
<a href="' . get_the_permalink() . '" rel="nofollow">
' . get_the_title() . '
</a>
</strong>
<div class="entry">
<a href="' . get_the_permalink() . '" rel="nofollow">
' . get_the_excerpt() . '
</a>
</div><!-- .entry -->
</div><!-- .col-1-content-->
</div><!-- .post -->
</div> <!-- .boxes -->';
// Add our closing div after every third post or the last post if there is less than three
if ( $key%3 == 2 || !array_key_exists( ( $key + 1 ), $gallery->posts ) ) {
$output .= '</div>';
}
// Create our new array of post data split in two and use with new array keys
$new_posts_array[floor( $key / 3 ) * 3 + $key] = $output;
// Setup/define our second variable which will hold the second set of post data from our posts
// This is the set that you would like to hide
$output_1 = '';
// Open a new div on the first post and every 3rd one there after to hold three posts
if ( ( $key%3 ) == 0 ) {
// This block of posts will use class "second-x" where x represents the block count
$output_1 .= '<div class="second-' . floor( $key / 3 ) . '">';
}
$imgaddr1 = get_post_meta( $post->ID, 'imgaddr1', true );
$imgaddr2 = get_post_meta( $post->ID, 'imgaddr2', true );
$imgssilka1 = get_post_meta( $post->ID, 'imgssilka1', true );
$imgssilka2 = get_post_meta( $post->ID, 'imgssilka2', true );
$namecolor1 = get_post_meta( $post->ID, 'namecolor1', true );
$namecolor2 = get_post_meta( $post->ID, 'namecolor2', true );
$numbercolor1 = get_post_meta( $post->ID, 'numbercolor1', true );
$numbercolor2 = get_post_meta( $post->ID, 'numbercolor2', true );
// Concatenate your second set of post data into a string to our second variable $output_1
$output_1 .= '<div class="full clearfix">
<div class="inner">
<figure class="indent-bot1">
<a href="' . get_the_permalink() . '" rel="nofollow">
' . get_the_post_thumbnail( $post->ID, array( 960, 690 ) ) . '
</a>
</figure>
<div class="row">
<div class="col-md-5">
<div class="inf-1">
<h4>Информация</h4>
</div>
<div class="inf-2">
<h5>' . get_the_title() . '</h5>
<div class="desc">
' . get_the_excerpt() . '
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="col-md-7 border-left">
<div class="inf-1">
<h4>Приложенные Цвета</h4>
</div>
<div class="inf-2">
<ul>
<li class="first-child">
<a href="' . $imgssilka1 . '" class="img-block">
<img src="' . $imgaddr1 . '">
</a>
<div class="txt">
<strong>' . $namecolor1 . '</strong>
<span>' . $numbercolor1 . '</span>
</div>
</li>
<li class="last-child">
<a href="' . $imgssilka2 . '" class="img-block">
<img src="' . $imgaddr2 . '">
</a>
<div class="txt">
<strong>' . $namecolor2 . '</strong>
<span>' . $numbercolor2 . '</span>
</div>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
</div><!-- .inner -->
</div>';
// Add our closing div after every third post or the last post if there is less than three
if ( $key%3 == 2 || !array_key_exists( ( $key + 1 ), $gallery->posts ) ) {
$output_1 .= '</div>';
}
// Create our new array of post data split in two and use with new array keys
$new_posts_array[( floor( $key / 3 ) + 1 ) * 3 + $key] = $output_1;
}
wp_reset_postdata();
// Sort our new array so that the keys are numerical again
ksort( $new_posts_array );
// Run a foreach loop to output our posts as we need. No need to modify anything here
foreach ( $new_posts_array as $v )
echo $v;
}

As we all know, WordPress is an open source tool and all plugins are available to manage such formatting.
I recommend to go with plugins to manage your requirements. I have used columns plugin for formatted output.

I got the total number of records and made the loop.
Inside the loop, two-cycle.
First loop displays a table. The second cycle displays a list.
<section>
<div class="container container-gallery">
<?php
$offset = 0;
$offset1 = 0;
$i =0;
$count = 0;
$reset =0;
$reset1 = 0;
$args = array(
'posts_per_page' => -1,
'post_type' => 'gallery',
);
$gallery = new WP_Query( $args );
$numberposts = $gallery->post_count;
if ($numberposts){
$id1=0;
$id2=0;
while($count < $numberposts){
// print_r($arr1);
$count++;
//echo "<h2>".$count."</h2>";
$arr1 = array(
'posts_per_page' => 400,
'post_type' => 'gallery',
'offset'=>$offset
);
$arr2 = array(
'posts_per_page' => 400,
'post_type' => 'gallery',
'offset'=>$offset1
);
$loop1 = new WP_Query($arr1);
$loop2 = new WP_Query($arr1);
while($loop1->have_posts()) : $loop1->the_post();
if ($reset<3) :
$reset++;
?>
<?php
$colorfilter1 = get_post_meta($post->ID, 'checkboxwhite', true);
$colorfilter2 = get_post_meta($post->ID, 'checkbox_beige', true);
$colorfilter3 = get_post_meta($post->ID, 'checkbox_brown', true);
$colorfilter4 = get_post_meta($post->ID, 'checkbox_gray', true);
$colorfilter5 = get_post_meta($post->ID, 'checkbox_black', true);
$colorfilter6 = get_post_meta($post->ID, 'checkbox_vvid', true);
if ($colorfilter1 != "") $colorfilter1 ="white ";
if ($colorfilter2 != "") $colorfilter2 ="beige ";
if ($colorfilter3 != "") $colorfilter3 ="brown ";
if ($colorfilter4 != "") $colorfilter4 ="gray ";
if ($colorfilter5 != "") $colorfilter5 ="black ";
if ($colorfilter6 != "") $colorfilter6 ="vivid ";
$class_color = $colorfilter1.$colorfilter2.$colorfilter3.$colorfilter4.$colorfilter5.$colorfilter6;
?>
<div class="col-1 mcol boxes<?php if( $i%3 == 0 ) { echo '-1'; }; $i++; echo ' '.$class_color;?>" id="colbox<?php echo $id1; $id1++;?>" data-id="click" >
<div class="post" id="post-<?php the_ID(); ?>">
<figure class="indent-bot">
<?php the_post_thumbnail(array(380,220,true)); ?>
</figure>
<div class="col-1-content">
<strong class="title-3">
<?php the_title(); ?>
</strong>
<div class="entry">
<?php the_excerpt(); ?>
</div><!-- .entry -->
</div><!-- .col-1-content-->
</div><!-- .post -->
</div><!-- .boxes -->
<?php else : break;?>
<?php endif; ?>
<?php endwhile; ?>
<?php
$reset = 0;
$offset +=3;
?>
<?php wp_reset_postdata(); ?>
<?php
while($loop2->have_posts()) : $loop2->the_post();
if ($reset1<3) :
$reset1++;
?>
<?php
$numbercolor1 = get_post_meta($post->ID, 'numbercolor1',true);
$numbercolor2 = get_post_meta($post->ID, 'numbercolor2', true);
$imgaddr1 = get_post_meta($post->ID, 'imgaddr1', true);
$imgaddr2 = get_post_meta($post->ID, 'imgaddr2', true);
$imgssilka1 = get_post_meta($post->ID, 'imgssilka1', true);
$imgssilka2 = get_post_meta($post->ID, 'imgssilka2', true);
$namecolor1 = get_post_meta($post->ID, 'namecolor1', true);
$namecolor2 = get_post_meta($post->ID, 'namecolor2', true);
?>
</div>
<div class="full clearfix active colbox<?php echo $id2; $id2++;?>" id="">
<div class="inner">
<figure class="indent-bot1">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_post_thumbnail(array(960,690)); ?>
</a>
</figure>
<div class="row">
<div class="col-md-5">
<div class="inf-1">
<h4>Информация</h4>
</div>
<div class="inf-2">
<h5><?php the_title(); ?></h5>
<div class="desc">
<?php the_excerpt(); ?>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="col-md-7 border-left">
<div class="inf-1">
<h4>Приложенные<</h4>
</div>
<div class="inf-2">
<ul>
<li class="first-child">
<a href="<?php echo $imgssilka1; ?>" class="img-block">
<img src="<?php echo $imgaddr1; ?>">
</a>
<div class="txt">
<strong><?php echo $namecolor1; ?></strong>
<span><?php echo $numbercolor1; ?></span>
</div>
</li>
<li class="last-child">
<a href="<?php echo $imgssilka2; ?>" class="img-block">
<img src="<?php echo $imgaddr2; ?>">
</a>
<div class="txt">
<strong><?php echo $namecolor2; ?></strong>
<span><?php echo $numbercolor2; ?></span>
</div>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="c_btn"></div>
</div>
</div>
</div><!-- .inner -->
</div>
<div class="container container-gallery">
<?php else : break;?>
<?php endif; ?>
<?php endwhile; ?>
<?php
$reset1 = 0;
$offset1 +=3;
?>
<?php wp_reset_postdata(); ?>
<?php
} //end if ($count <= $numberposts)
} //end if ($numberposts)
?>
<?php
if ( have_posts() ) while ( have_posts() ) : the_post(); // старт цикла ?>
<article id="post-<?php the_ID(); ?>">
<?php the_content(); ?>
</article>
<?php endwhile; ?>
</div><!-- .container -->
</section>

Related

Wordpress error on latest_post() function

im my single.php file I have two functions that displays related posts to the one currently reading. When I add a new post and do not add tags to it, an error will appear when I try to preview it.
Error thrown
Call to a member function have_posts () on null
When I add tags to it, everything displays fine.
<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) :
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>6,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) :
?>
<div class="d-block d-lg-none text-center" >
<?php get_sidebar(); ?>
<div class="col-12 col-lg-7 offset-lg-1">
<div id="powiazane">
<h2> Powiązane artykuły: </h2>
<div class="card-deck mb-50px">
<?php
$post_wrap = 0;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('card border-0 ' . $termsString); ?>>
<img class="card-img-top secondary-post-img p-3" src="<?php echo the_post_thumbnail_url('post-thumb'); ?>" alt="<?php the_title_attribute(); ?>">
<div class="card-body">
<a href="<?php the_permalink(); ?>" class="stretched-link" alt="<?php the_title_attribute(); ?>">
<?php
if(get_field('tytul_krotki'))
{
echo '<h3>' . get_field('tytul_krotki') . '</h3>';
} else {
echo '<h3>' . get_the_title() . '</h3>';
}
?>
</a>
<div class="small text-muted">
<p class="mb-0">
<?php the_field('miasto'); ?> | <?php echo hubdab_relative_time(); ?>
</p>
</div>
<p class="mt-25px"><?php the_field('lid_krotki') ?> </p>
</div>
<div class="card-footer py-0">
</div>
</article><!-- #post-## -->
<?php
if ($post_wrap % 2 == 1){
echo '<div class="w-100 d-none d-sm-block"><!-- wrap every 2 on sm+--></div>';
}
$post_wrap++; ?>
<?php
endwhile;
?>
You have a problem in this code
$tags = wp_get_post_tags($post->ID);
if ($tags) :
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>6,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
$tags = wp_get_post_tags($post->ID) will return
(array|WP_Error) Array of WP_Term objects on success or empty array if no tags were found. WP_Error object if 'post_tag' taxonomy doesn't exist.
So if you check for if($tags) always will be true, as WP_Error will be true in case of no tags.
You can modify your condition to execute the query as:
if (!is_wp_error($tags) and count($tags)):
....
Hope this help

php template code - all properties page display issue

My page: http://kevinkang.ca/en_US/all-properties/ was broken yesterday.
But I can't find any problem on these. Can anyone help me to find out the problem?
<div class="grid-x all-properties-page">
<?php
$args = array( 'category__not_in' => array( 4, 8, 9, 10 ) , 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $i=0; ?>
<div class="medium-12 cell padding-top-page">
<div class="home-data description-title-price">
<div class="grid-container">
<div class="grid-x">
<div class="medium-6 cell">
<?php the_title(); ?>
</div>
<div class="medium-6 cell text-right">
<?php the_field('price'); ?>
</div>
</div>
</div>
</div>
<a href="<?php the_permalink(); ?>"><div class="grid-x">
<ul class="images3">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('3-images', $post->ID);
$img3=0;
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$title = $image['title']; //The title
$caption= $image['caption']; //The caption
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1562, 760); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id);
$img3++;
?>
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<li style="background-image: url(<?php echo $full_image_url; ?>);" class="img-list<?php echo $img3; ?>">
<?php if( !empty($url) ){ ?></a><?php } ?></li>
<?php endforeach; endif; ?>
</ul>
<?php foreach ( get_the_category() as $value) {
$i++; ?> <h3 class="cat<?php echo $i; ?> <?php echo $value->name; ?>"><?php echo $value->name;} ?></h3>
</div></a>
<div class="grid-container pr-address-bg">
<div class="address-cont price-address">
<?php the_field('price'); ?>
<?php the_field('address_first_line'); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
I see you have a lot of problems in your HTML ( inside , multiple tags. First check your HTML with https://validator.w3.org/. It looks the main parts of the problems come from header.php. I would have a look.

WordPress Custom Post query else data

I recently developed a Wordpress theme, this theme has little bit complex custom post type.
Below is my custom post query code. I would like kind of a else content whenever the request doesn't return any post. Its currently just blank.
I am not a PHP developer though, would someone give me a hand adding such a alternative content ?
Thank you.
<div class="front_page_deal_holder">
<?php
// get the currently queried taxonomy term, for use later in the template file
$term = get_queried_object();
//second query - posts
// Define the query
$args = array(
'post_type' => 'products',
'product_cat' => $term->slug
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
// output the term name in a heading tag
echo'<h1>All Products about '. $term->name . '</h1>';
echo'<div class="row">';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-3">
<a style="display:block;text-decoration:none" href="<?php the_permalink(); ?>">
<div class="single_front_sec">
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'post-image'); ?>
<img class="post_thumb_cus" src="<?php echo $featured_img_url ?>" alt="Product Image">
<h2 class="featured_product_heading">
<?php the_title(); ?>
</h2>
<span class="offer_note"><p><?php echo get_post_meta($post->ID, 'Offer Note', true); ?></p></span>
<div class="vew_d_button_holder">
<a class="view_d_button" href="<?php the_permalink(); ?>">View Details</a>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<!-- row -->
<div class="my_Custom_pagination">
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
</div>
<?php } // end of check for query having posts
// use reset postdata to restore orginal query
wp_reset_postdata();
?>
<?php include_once('top-categories.php'); ?>
<!--==== Top catogory section ====-->
<!-- front deal closed -->
</div>
elseif(!$query->have_posts()){//HTML here}
or
else{ //HTML here}
right after the end of check for your query , if you want to follow an OOP rule to make your code cleaner , move everything into a function inside function.php file and call the function there instead
<div class="front_page_deal_holder">
<?php
// get the currently queried taxonomy term, for use later in the template file
$term = get_queried_object();
$args = array(
'post_type' => 'products',
'product_cat' => $term->slug
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
echo'<h1>All Products about '. $term->name . '</h1>';
echo'<div class="row">';
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-3">
<a style="display:block;text-decoration:none" href="<?php the_permalink(); ?>">
<div class="single_front_sec">
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'post-image'); ?>
<img class="post_thumb_cus" src="<?php echo $featured_img_url ?>" alt="Product Image">
<h2 class="featured_product_heading">
<?php the_title(); ?>
</h2>
<span class="offer_note"><p><?php echo get_post_meta($post->ID, 'Offer Note', true); ?></p></span>
<div class="vew_d_button_holder">
<a class="view_d_button" href="<?php the_permalink(); ?>">View Details</a>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="my_Custom_pagination">
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
</div>
<?php }else{
// Add your code here for else part
}
wp_reset_postdata();
?>
<?php include_once('top-categories.php'); ?>
</div>

PHP Wrapping custom items inside loop Wordpress

I am currently creating a slider which consists of products.
Currently I have managed to wrap every 2 items in div using this code:
<div class="frame crazy" id="crazy">
<div class="slidee">
<?php if ( $myposts->have_posts() ) :
$i = 0;
while ( $myposts->have_posts() ) : $myposts->the_post();
$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_id() ), 'single-post-thumbnail' );
$value = get_field( "alternate_image", get_the_id() );
$titl = get_field( "title", get_the_id() );
$big = get_field( "big_section", get_the_id() );
if ( $i % 2 == 0) : ?>
<div class="op" <?if ($big==1){?>style="width:850px !important;"<?}?>>
<?php endif;
$_pf = new WC_Product_Variable(get_the_id());
$variations = $_pf->get_available_variations();
$vrt = count($variations);
?>
<div data-hv="<?php echo $value; ?>" data-titleContent="<a href='<?php echo get_the_permalink();?>'><?php echo get_the_title(); ?></a>" data-tipso-content="<span class='varaition'>this item has <?php echo $vrt; ?> variation(s)</span><a class='bty' href='<?php echo get_the_permalink(); ?>'>details</a>" data-url="<? echo the_permalink(); ?>" class="cola <?php if($big==1){?>big<?}?>" style="background-image: url('<?php echo $image[0]; ?>')" data-mg="<?php echo $image[0];?>">
<?php if($titl==1) { ?>
<h2><a href='<?php echo get_the_permalink();?>'><?php echo get_the_title(); ?></a></h2>
<p class="slu"><a href='<?php echo get_the_permalink();?>'>shop now ></a> </p>
<?php } ?>
</div>
<?php if ( $i % 2 != 0 ) : ?>
</div>
<?php endif; ?>
<?php $i++; endwhile; ?>
<?php if ( $i % 2 != 0 ) : ?>
</div>
<?php endif; ?>
<?php endif;?>
</div>
</div>
This code wraps every two products like this:
<div class="op">
<div class="product1">
//content
</div>
<div class="product2">
//content
</div>
</div>
<div class="op">
<div class="product3">
//content
</div>
<div class="product4">
//content
</div>
</div>
But I need to fetch custom number of posts according to product meta. So that the number of products can be varying like this:
<div class="op">
<div class="product1">
//content
</div>
<div class="product1">
//content
</div>
<div class="product1">
//content
</div>
<div class="product1">
//content
</div>
<div class="op">
<div class="product1">
//content
</div>
<div class="product1">
//content
</div>
</div>
Is it possible using product meta or any better idea?
this line in your code:
if ( $i % 2 == 0) : ?>
contains the loop number, change that to be a variable and set that variable from the meta data, so:
$loopmeta=metadata_retriever();
if ( $i % $loopmeta == 0 ) : ?>
You will have to write the metadata_retriever() function and put some error checking around the retrieval of the $loopmeta variable to ensure it comes back as a valid integer (not 0, nor 12.735 for instance :-) )

wordpress: show posts in two columns

I have a theme that shows the latest four posts in one column. I want to convert this to two posts in two columns.
I made two divs next to each other and put the first in descending order and the other in ascending order. Then I set it to show only 2 posts.
But now it shows 2 posts in the left div and all four posts in the right div:
I don't understand why it is doing this. Here is the code:
<section class="container">
<div class="left-half">
<article>
<!-- =========================
SECTION: LATEST NEWS
============================== -->
<?php
$parallax_number_of_posts = get_option('posts_per_page');
$args = array( 'post_type' => 'post', 'posts_per_page' => $parallax_number_of_posts, 'order' => 'ASC','ignore_sticky_posts' => true );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$parallax_one_latest_news_title = get_theme_mod('parallax_one_latest_news_title',esc_html__('Latest news','parallax-one'));
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<div class="section-overlay-layer">
<div align="center" class="container">
<div class="row">
<!-- TIMELINE HEADING / TEXT -->
<?php
if(!empty($parallax_one_latest_news_title)){
echo '<div class="col-md-12 timeline-text text-left"><h2 class="text-left dark-text">'.esc_attr($parallax_one_latest_news_title).'</h2><div class="colored-line-left"></div></div>';
} elseif ( isset( $wp_customize ) ) {
echo '<div class="col-md-12 timeline-text text-left paralax_one_only_customizer"><h2 class="text-left dark-text "></h2><div class="colored-line-left "></div></div>';
}
?>
<div class="parallax-slider-whole-wrap">
<!--<div class="controls-wrap">
<button class="control_next icon icon-arrow-carrot-down"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Down','parallax-one')?></span></button>
<button class="control_prev fade-btn icon icon-arrow-carrot-up"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Up','parallax-one')?></span></button>
</div>-->
<!-- TIMLEINE SCROLLER -->
<div itemscope itemtype="http://schema.org/Blog" id="parallax_slider" class="col-md-6 timeline-section">
<ul class="vertical-timeline" id="timeline-scroll">
<?php
$i_latest_posts= 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
$i_latest_posts++;
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 1){
echo '<li>';
}
} else {
echo '<li>';
}
?>
<div itemscope itemprop="blogPosts" itemtype="http://schema.org/BlogPosting" id="post-<?php the_ID(); ?>" class="timeline-box-wrap" title="<?php printf( esc_html__( 'Latest News: %s', 'parallax-one' ), get_the_title() ) ?>">
<div itemscope itemprop="image" class="icon-container white-text">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail('parallax-one-post-thumbnail-latest-news');
else: ?>
<img src="<?php echo parallax_get_file('/images/no-thumbnail-latest-news.jpg'); ?>" width="150" height="150" alt="<?php the_title(); ?>">
<?php
endif;
?>
</a>
</div>
<div class="info">
<header class="entry-header">
<h1 itemprop="headline" class="entry-title"><br><br><br>
<?php the_title(); ?>
</h1>
<!-- .entry-meta -->
</header>
<div itemprop="description" class="entry-content entry-summary">
<?php the_excerpt(); ?>
<?php printf( esc_html__( 'Bekijk de fotos %s', 'textdomain' ), '<span class="screen-reader-text"> '.get_the_title().'</span>' ); ?>
</div>
</div>
</div>
<?php
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 0){
echo '</li>';
}
} else {
echo '</li>';
}
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div><!-- .parallax-slider-whole-wrap -->
</div>
</div>
</div>
</section>
<?php
}
} ?>
</article>
</div>
<!--rechts-->
<div class="right-half">
<article>
<!-- =========================
SECTION: LATEST NEWS
============================== -->
<?php
$parallax_number_of_posts = get_option('posts_per_page');
$args = array( 'post_type' => 'post', 'posts_per_page' => $parallax_number_of_posts, 'order' => 'DESC','ignore_sticky_posts' => true );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$parallax_one_latest_news_title = get_theme_mod('parallax_one_latest_news_title',esc_html__('Latest news','parallax-one'));
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<div class="section-overlay-layer">
<div align="center" class="container">
<div class="row">
<!-- TIMELINE HEADING / TEXT -->
<br>
<br>
<br>
<div class="parallax-slider-whole-wrap">
<div class="controls-wrap">
<button class="control_next icon icon-arrow-carrot-down"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Down','parallax-one')?></span></button>
<button class="control_prev fade-btn icon icon-arrow-carrot-up"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Up','parallax-one')?></span></button>
</div>
<!-- TIMLEINE SCROLLER -->
<div itemscope itemtype="http://schema.org/Blog" id="parallax_slider" class="col-md-6 timeline-section">
<ul class="vertical-timeline" id="timeline-scroll">
<?php
$i_latest_posts= 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
$i_latest_posts++;
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 1){
echo '<li>';
}
} else {
echo '<li>';
}
?>
<div itemscope itemprop="blogPosts" itemtype="http://schema.org/BlogPosting" id="post-<?php the_ID(); ?>" class="timeline-box-wrap" title="<?php printf( esc_html__( 'Latest News: %s', 'parallax-one' ), get_the_title() ) ?>">
<div itemscope itemprop="image" class="icon-container white-text">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail('parallax-one-post-thumbnail-latest-news');
else: ?>
<img src="<?php echo parallax_get_file('/images/no-thumbnail-latest-news.jpg'); ?>" width="150" height="150" alt="<?php the_title(); ?>">
<?php
endif;
?>
</a>
</div>
<div class="info">
<header class="entry-header">
<h1 itemprop="headline" class="entry-title"><br><br><br>
<?php the_title(); ?>
</h1>
<!-- .entry-meta -->
</header>
<div itemprop="description" class="entry-content entry-summary">
<?php the_excerpt(); ?>
<?php printf( esc_html__( 'Bekijk de fotos %s', 'textdomain' ), '<span class="screen-reader-text"> '.get_the_title().'</span>' ); ?>
</div>
</div>
</div>
<?php
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 0){
echo '</li>';
}
} else {
echo '</li>';
}
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div><!-- .parallax-slider-whole-wrap -->
</div>
</div>
</div>
</section>
<?php
}
} ?>
</article>
</div>
</section>
If I understood correctly what you want to achieve - to divide several posts into two columns, I can not understand exactly how you want to achieve this into code. If you rely on this condition $the_query->current_post % 2 == 1 to filter the posts, then in your code it only filter printing of the li element, but then the cycle continues and show the post itself, ie what you achieve with this code is to place two posts (div.timeline-box-wrap) elements in one li.If you want to use this method of separation, you should change the code a little (I will simplify it, but the main is, that you must stop current loop if your condition pass).
You don't need to query DB two times with the same query - this is performance issue, so you can use the same result and loop over two times.
You can use $the_query->current_post to get current post and filter.
<section class="container">
<div class="left-half">
<article>
<?php
$parallax_number_of_posts = get_option('posts_per_page');
$args = array( 'post_type' => 'product', 'posts_per_page' => $parallax_number_of_posts, 'order' => 'ASC','ignore_sticky_posts' => true );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<ul class="vertical-timeline" id="timeline-scroll">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
if($the_query->current_post % 2 == 1)
continue; ?>
<li><?php the_title() ?></li>
<?php endwhile; ?>
</ul>
</section>
<?php
}
} ?>
</article>
</div>
<div class="right-half">
<article>
<?php
$the_query->rewind_posts();
if ( $the_query->have_posts() ) {
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<ul class="vertical-timeline" id="timeline-scroll">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
if($the_query->current_post % 2 == 0)
continue; ?>
<li><?php the_title() ?></li>
<?php endwhile; ?>
</ul>
</section>
<?php
}
} ?>
</article>
</div>
</section>
P.S. You can simplify more, if you set 2 variables: $left and $right and use only one loop to set one or other with html, and then print their values.

Categories