Loading Wordpress posts into multiple DIVs (Columns) - php

I'm trying to load all Wordpress posts into three different Divs as equal,
like this
<div class="row">
<div class="col-4">
POSTS HERE 1/3
</div>
<div class="col-4">
POSTS HERE 1/3
</div>
<div class="col-4">
POSTS HERE 1/3
</div>
</div>
I've already done something similar on other pages, but the difference is I'm only loading 6 posts there, so it's easier,
but now I wanna list all posts in the page into 3 columns,
this is the code I've used for 6 posts:
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count1++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count2++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=4'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count3++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Try the below code. you will get post list in below format.
<?php
$loop_counter = $innerBreak = 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6));
if($wpb_all_query->have_posts()):
while($wpb_all_query->have_posts()) :
$wpb_all_query->the_post();
if($innerBreak == 1){
?>
<!-- when complete listing of 3 post open the new div -->
<div class="col-sm-4">
<?php
}
?>
<div class="article_list">
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
</div>
<?php
//when complete listing of 3 post closed previously div.
if($loop_counter%3==0){ echo '</div>'; $innerBreak = 1;}else{$innerBreak = 0;}
$loop_counter++; endwhile;
else:
echo "<div>No Results Found</div>";
endif;
?>

Thanks to Shivendra Singh
after some small tweaks, here is the code to equally split all posts into three columns
<?php
$loop_counter = $innerBreak = 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
if($wpb_all_query->have_posts()):
while($wpb_all_query->have_posts()) :
$wpb_all_query->the_post();
if($innerBreak == 1){
?>
<?php $posts_count = round((($wpb_all_query->post_count) / 3)+0.5); ?>
<!-- when complete listing of 3 post open the new div -->
<div class="col-sm-4">
<div class="article_list" data-parallax='{"y" : -150, "distance": 2000, "smoothness": 10}'>
<?php
}
?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php
//when complete listing of 3 post closed previously div.
if($loop_counter%$posts_count==0){ echo '</div></div>'; $innerBreak = 1;}else{$innerBreak = 0;}
$loop_counter++; endwhile;
else:
echo "<div>No Results Found</div>";
endif;
?>

In line $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?> you use 'posts_per_page'=>6.
Try to: 'posts_per_page'=>-1 it's load all posts.
Then try instead of your code:
<!-- If you use bootstrap -->
<div class="container">
<div class="row">
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php while( $wpb_all_query->have_posts() ){ $wpb_all_query->the_post(); ?>
<div class="col-md-4">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); // or the_content(); or any ?>
</div>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</div>

Related

Wrap every 2 posts on a row

I'm using Bootstrap, I have a list of posts and I want to wrap every 2 posts on a row. Each post is wrapped on a <div col>. You can see live here.
I tried with this but it wrap the row each one post:
<?php
$num = 0;
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// The Loop
while ( have_posts() ) : the_post();
if($num%2) {
echo "<div class='row' style='margin-bottom: 2.3em;''>";
}
?>
<div class="col-xs-12 col-sm-6 col-md-6">
<h2 class="category-encabezado">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Enlace a <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<small>Hace
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ''; ?>
</small>
<div class="entry">
<p>
<?php the_excerpt(); ?>
</p>
<?php
$my_shortcode = get_field('audio-field');
echo do_shortcode( $my_shortcode );
?>
</div>
</div>
<?php
if($num %2) {
echo '</div>';
}
$num++
?>
<?php endwhile; // End Loop
?>
</div>
<?php
You have to put div.row Out of the Loop while

Add class to div wrapped around custom post type based off posts category

I'm looking for a way to inject a class into a div wrapped around a custom post type based off the category that post is attributed to.There will be three categories;
ebook (class name cat1)
infographic (class name cat2)
case study (class name cat3)
Here is my code so far to call the custom post type. Currently, the page displays all posts as the last category - case study (cat3).
<?php $loop = new WP_Query( array( 'post_type' => 'resources', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$post = $wp_query->post;
if ( in_category('ebook', $post->ID) ) { ?>
<div <?php body_class('tile scale-anm cat1 all end'); ?>>
<section class="small-12 medium-4 large-3 columns end">
<section class="grid">
<figure class="effect-sarah">
<img src="<?php the_field('img'); ?>" alt="img13"/>
<figcaption>
<h2>Resource</h2>
<p class="signika"><?php the_field('desc'); ?></p>
<p class="bold"><?php the_field('btnText'); ?></p>
<?php the_field('btnText'); ?>
</figcaption>
</figure>
</section>
</section>
</div>
<?php
}
elseif ( in_category('infographic', $post->ID) ) { ?>
<div <?php body_class('tile scale-anm cat2 all end'); ?>>
<section class="small-12 medium-4 large-3 columns end">
<section class="grid">
<figure class="effect-sarah">
<img src="<?php the_field('img'); ?>" alt="img13"/>
<figcaption>
<h2>Resource</h2>
<p class="signika"><?php the_field('desc'); ?></p>
<p class="bold"><?php the_field('btnText'); ?></p>
<?php the_field('btnText'); ?>
</figcaption>
</figure>
</section>
</section>
</div>
<?php
}
elseif ( in_category('casestudy', $post->ID) ) { ?>
<div <?php body_class('tile scale-anm cat3 all end'); ?>>
<section class="small-12 medium-4 large-3 columns end">
<section class="grid">
<figure class="effect-sarah">
<img src="<?php the_field('img'); ?>" alt="img13"/>
<figcaption>
<h2>Resource</h2>
<p class="signika"><?php the_field('desc'); ?></p>
<p class="bold"><?php the_field('btnText'); ?></p>
<?php the_field('btnText'); ?>
</figcaption>
</figure>
</section>
</section>
</div>
<?php
}
?>
<?php endwhile; wp_reset_query(); ?>
DRY, Don't repeat yourself. There are also some problems with your loop.
After using WP_Query, use wp_reset_postdata() not wp_reset_query().
get rid of $post = $wp_query->post;, get the id from in the loop using get_the_ID()
Then we check the category and assign the class name to a variable $catclass, which we insert into the argument for body_class(), note the use of " instead of '
try this out:
<?php $loop = new WP_Query( array( 'post_type' => 'resources', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$catclass = "";
if (in_category('ebook', get_the_ID())) {
$catclass="cat1";
} else if(in_category('infographic', get_the_ID())) {
$catclass="cat2";
} else if(in_category('casestudy', get_the_ID())){
$catclass="cat3";
}
?>
<div <?php body_class("tile scale-anm {$catclass} all end"); ?>>
<section class="small-12 medium-4 large-3 columns end">
<section class="grid">
<figure class="effect-sarah">
<img src="<?php the_field('img'); ?>" alt="img13"/>
<figcaption>
<h2>Resource</h2>
<p class="signika"><?php the_field('desc'); ?></p>
<p class="bold"><?php the_field('btnText'); ?></p>
<?php the_field('btnText'); ?>
</figcaption>
</figure>
</section>
</section>
</div>
<?php
endwhile;
wp_reset_postdata(); ?>
If you're really feeling frisky you can replace the if/else statements with:
switch(true){
case in_category('ebook', get_the_ID()):
$catclass = "cat1";
break;
case in_category('infographic', get_the_ID()):
$catclass = "cat2";
break;
case in_category('casestudy', get_the_ID()):
$catclass = "cat3";
break;
default:
$catclass = "";
}

How do I style alternating WordPress posts?

I have some posts displaying from a specific category but need them to have a little different styling on every second post.
This code I have displays all posts in a list with the same styling.
How do I edit this code and make it so I can edit the styling of ONLY every second post?
As it stands there's text on the left and an image on the right - the second posts just need to switch sides - it's simple styling but I'm unsure how to break the posts up to edit #case-left & #case-right are the two divs that need to switch.
Thanks in advance.
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php endforeach;
}
}
?>
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
$counter = 1;
$class = '';
foreach ($posts as $post):
setup_postdata($post);
if($counter%2 == 0) {
$class = "even-no";
} else {
$class = '';
}
?>
<div class="serve-inner-split <?php echo $class; ?>">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php $counter++; ?>
<?php endforeach;
}
}
?>
Ok, so when editing your question I saw that you want to style it so that the divs switch places. You can do that like this in php
<?php // PAGE LINK/TITLE
if (is_page()){}
$i = 0;
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
$i++;
if ($i % 2 == 0):?>
<div class="serve-inner-split">
<div id="case-split">
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endif;
?>
<?php endforeach;
wp_reset_postdata();
} ?>
But in my opinion, that's kinda unnecessary. You can also do it like this:
<?php // PAGE LINK/TITLE
if (is_page()){}
$i = 0;
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
$class = '';
$i++;
if ($i % 2 == 0){
$class = 'right_image'
}
?>
<div class="serve-inner-split <?php echo $class; ?>">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
} ?>
And then float the image the other way (or use absolute positioning or whatever suits you), with the '.right_image' class in the css.

Query_posts wrap every 6 posts together

I'm using query_post to call all posts within a custom post type I have created called 'partners'.
What I would like so it to wrap the posts in a div in groups of 6's. For example:
<div class="item">
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
</div>
<!-- 6 images/posts wrapped -->
<div class="item">
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
</div>
<!-- 6 images/posts wrapped -->
This is my code so far:
<?php
query_posts('post_type=partners');
if (have_posts()) : while (have_posts()) ;
$posts = the_post();
if( $posts ): ?>
<? $lastIndex = count($posts) - 1; ?>
<? foreach($posts as $index => $post) : ?>
<? setup_postdata($post); ?>
<? if($index % 6 === 0) { ?>
<div class="item <?=$index === 0 ? 'active' : '' ?>">
<? } ?>
<div class="car-part-logo">
<? the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<? if(($index + 1) % 6 === 0 || $index === $lastIndex) { ?>
</div>
<? } ?>
<? endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
However I get the following error: unexpected T_ENDWHILE
try this code
<?php
query_posts('post_type=partners');
while ( have_posts() ) : the_post();
//if (have_posts()) : while (have_posts()) ;
$posts = the_post();
if( $posts ): ?>
<? $lastIndex = count($posts) - 1; ?>
<? foreach($posts as $index => $post) : ?>
<? setup_postdata($post); ?>
<? if($index % 6 === 0) { ?>
<div class="item <?=$index === 0 ? 'active' : '' ?>">
<? } ?>
<div class="car-part-logo">
<? the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<? if(($index + 1) % 6 === 0 || $index === $lastIndex) { ?>
</div>
<? } ?>
<? endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I managed to rebuild the code based on this previous question. I altered my code to work with 6 items instead of 3.
<?php query_posts('post_type=partners'); ?>
<?php $variable=0;?>
<div class="item active">
<?php while ( have_posts() ) : the_post(); ?>
<?php if(($variable+1)<7){ ?>
<div class="car-part-logo">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<?php $variable+=1; ?>
<?php }else{ ?>
<?php $variable=1; ?>
</div>
<div class="item">
<div class="car-part-logo">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<?php }?>
<?php endwhile; ?>
</div>
<?php wp_reset_query(); ?>

First post different layout

I need to have a different layout for the first post in every page, but don't know where to start. The code is the following:
<?php get_header(); ?>
<div class="row">
<section class="small-12 columns grid-style">
<?php $i = 0; $counter = range(0, 200, 3); ?>
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array('offset'=> 0, 'paged'=>$paged);
$all_posts = new WP_Query($args);
if (have_posts()) : while($all_posts->have_posts()) : $all_posts->the_post();?>
<?php if ($i % 3 == 0) { echo '<div class="row journal" data-equal=".post">'; } ?
<!--Post -->
<article class="grid-style post" id="post-<?php the_ID(); ?>"
<div class="post-img">
<?php get_template_part( 'inc/postformats/grid-style' ); ?>
</div>
<div class="post-box">
<div class="post-title">
<h2><?php echo ShortenText(get_the_title(), 50); ?></h2>
</div>
<aside class="post_categories">
<?php the_category(', '); ?>
</aside>
<div class="post-content">
<?php echo ShortenText(get_the_excerpt(), 170); ?>
</div>
</div>
</article>
<!--/Post -->
<?php if (in_array($i + 1, $counter)){ echo '</div>'; } ?>
<?php $i++; endwhile; ?>
<div class="small-12 columns">
<?php theme_pagination($all_posts->max_num_pages, 1, true); ?>
</div>
<?php else : ?>
<p><?php _e( 'Please add posts from your WordPress admin page.', THB_THEME_NAME ); ?></p>
<?php endif; ?>
</section>
</div>
<?php get_footer(); ?>
I need to use a different image size, so I need to output everything inside < ! - - Post - - > again for the first post only.
Are you trying to include div.row.journal to the first post?
If it's the case try doing this:
Replace <?php if ($i % 3 == 0) { echo '<div class="row journal" data-equal=".post">'; } ? by the following: (It'll echo it only on the first page, for the first post)
<?php
if ($all_posts->current_post == 0 && !is_paged()) {
echo '<div class="row journal" data-equal=".post">';
}
?>
Doing the same for the closing div by replacing <?php if (in_array($i + 1, $counter)){ echo '</div>'; } ?> by :
<?php
if ($all_posts->current_post == 0 && !is_paged()) {
echo '</div>';
}
?>
You have to define a counter and ask with if...else... inside the loop
$post_counter = 1;
while (start_loop):
if ($post_counter == 1){
echo "<div id='1'></div>";
else:
echo "<div id='2'></div>";
$post_counter += 1;
//end loop
I think that should help you.
This is the code I have now, I was able to output the HTML for the first post, and a different one for ALL the posts, but I am getting 2 first posts. One using the first layout and the second using the second layout
<?php
/*
Template Name: Blog - Grid Style
*/
?>
<?php get_header(); ?>
<div class="row">
<section class="small-12 columns grid-style">
<?php $i = 0; $counter = range(0, 200, 3); ?>
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array('offset'=> 0, 'paged'=>$paged);
$all_posts = new WP_Query($args);
$post_counter = 0;
if (have_posts()) : while($all_posts->have_posts()) : $all_posts->the_post(); $post_counter++;?>
<?php if ($i % 3 == 0) { echo '<div class="row journal" data-equal=".post">'; } ?>
<?php if ($post_counter == 1){ ?>
<!--Layout Post 1-->
<div class="row">
<article class="grid-style post small-9 small-centered column" id="post-<?php the_ID(); ?>">
<div class="post-img">
<?php get_template_part( 'inc/postformats/grid-style' ); ?>
</div>
<div class="post-box">
<div class="post-title">
<h2><?php echo ShortenText(get_the_title(), 50); ?>1</h2>
</div>
<aside class="post_categories">
<?php the_category(', '); ?>
</aside>
<div class="post-content">
<?php echo ShortenText(get_the_excerpt(), 170); ?>
</div>
</div>
</article>
</div>
<!--/Layout Post 1-->
<?php } ?>
<!--Other Posts -->
<div class="row">
<article class="grid-style post small-9 small-centered column" id="post-<?php the_ID(); ?>">
<div class="post-img">
<?php get_template_part( 'inc/postformats/grid-style' ); ?>
</div>
<div class="post-box">
<div class="post-title">
<h2><?php echo ShortenText(get_the_title(), 50); ?></h2>
</div>
<aside class="post_categories">
<?php the_category(', '); ?>
</aside>
<div class="post-content">
<?php echo ShortenText(get_the_excerpt(), 170); ?>
</div>
</div>
</article>
</div>
<!--/Other Posts -->
<?php if (in_array($i + 1, $counter)){ echo '</div>'; } ?>
<?php $i++; endwhile; ?>
<div class="small-12 columns">
<?php theme_pagination($all_posts->max_num_pages, 1, true); ?>
</div>
<?php else : ?>
<p><?php _e( 'Please add posts from your WordPress admin page.', THB_THEME_NAME ); ?></p>
<?php endif; ?>
</section>
</div>
<?php get_footer(); ?>

Categories