I need to separate WordPress posts loop in to 2 columns , left and right.
Currently I am doing this with 2 separate loops but would like to make it with one if possible.
<div class="left-side">
<?php
$row_start = 1;
while ( have_posts() ) : the_post();
if( $row_start % 2 != 0) {// odd
get_template_part( 'blog','item');
}
++$row_start;
endwhile;
?>
</div>
<div class="right-side">
<?php
$row_start = 1;
while ( have_posts() ) : the_post();
if( $row_start % 2 == 0) {// even
get_template_part( 'blog','item');
}
++$row_start;
endwhile;
?>
</div>
Any help is appreciated!
Move the loops into a separate template files.
Create two files in your themes directory, one named posts-odd.php and the other named posts-even.php and add the post loops respectively, i.e:
posts-odd.php:
<?php
$row_start = 1;
while ( have_posts() ) : the_post();
if( $row_start % 2 != 0) {// odd
get_template_part( 'blog','item');
}
++$row_start;
endwhile;
?>
posts-even.php:
<?php
$row_start = 1;
while ( have_posts() ) : the_post();
if( $row_start % 2 == 0) {// even
get_template_part( 'blog','item');
}
++$row_start;
endwhile;
?>
In your main template you can now use get_template_part function to include the partial template into your main template:
<div class="left-side">
<?php get_template_part('posts', 'odd') ?>
</div>
<div class="right-side">
<?php get_template_part('posts', 'even') ?>
</div>
If you're displaying the odd and even posts in various places this will give you the benefit of only having to define the loop templates once.
https://developer.wordpress.org/reference/functions/get_template_part/
Not easy but possible.
<?php
$i = 0;
$column = array();
$column[1] = $column[2] = '';
if (have_posts()) :
while (have_posts()) :
the_post();
$i++;
$column[$i] .= '<div class="'.esc_attr(implode(' ', get_post_class())) .'">';
$column[$i] .= '<div class="post_inner">
'.get_the_content().'
</div>
</div>';
$i = ($i==2) ? 0 : $i;
endwhile; ?>
<div id="grid_posts">
<div class="span6"><?php echo $column[1]?></div>
<div class="span6"><?php echo $column[2]?></div>
</div>
<?php else: ?>
<p><?php esc_html_e('No posts were found. Sorry!', 'mytheme'); ?></p>
<?php endif; ?>
Basically you create columns array and put your contents in there in two keys (1 and 2), and every 2 posts you put in one column, and keep increasing the counter to 2 and resetting it once it gets to 2. So you keep filling the array with posts - first to key 1 and then to key 2 and so on.
Then you just output those two into 2 columns (span6 in my case).
Hope this helps.
Related
I am trying to have 3 boxes across my WP page.
<div class="row text-center">
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
The object is to have one different specific post in each of the three boxes.
I am trying to make a page that has every block of text easily editable from the admin rather than the code. I am using a plugin that allows me to add a shortcode to a post where I want my specific post to show.
I have the boxes in place and have everything correct.
The first box populates with the correct post as it should but the other two won't display their posts.
It seems that maybe I can only use the loop once on the page, if so how can I achieve my aims?
Thanks for reading, any understandable advice will be very welcome, I have only recently started custom themes and PhP is a bit of a struggle too so I would be grateful if the help could be dumbed down a bit.
NB: I have tried replacing break with continue as suggested but the second and third post still don't show. Is it possible that there is more than just replacing the word break with continue?
Your loop breaks out of the foreach loop on the second iteration since $count = 1. You might have wanted to do continue to skip the 2nd post?
(Don't be tempted to think that break just breaks out of the conditional block. The conditional logic simply chooses which path or block of code to execute.)
EDIT
First of all, you are doing a loop for each box...The purpose of the loop is so that you don't need to repeat your code.
Second, you are restarting the loop each time..
Use the loop like this:
$query = new WP_Query( array( 'post_type' => 'post' ) );
<?php if ( $query->have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
<?php the_title();
the_content();
?>
</div>
<?php endwhile;
wp_reset_postdata();
endif;
?>
There a couple ways to do the query. I do like the get_posts better for simple queries because you don't need to reset the postdata using that way. You'd pass in the same arguments to the WP_Query.
Also I recommend you create a page template for this and assign it to a page. This way if you want to change your template you can just assign the page a different one instead of modifying your index.php code.
I have the following so far:
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<?php the_title();?>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<?php the_title();?> blah
<?php the_excerpt();?>
<?php endif; ?>
<?php endwhile; ?>
I want to wrap the first 4 in a '<div class="top"></div>' and the bottom 6, I want all of them wrapped in a '<div class="bottom"></div>'
Any tips of how this can be done using the while loop would be greatly appreciated
Try the below snippet.
<?php
$count = 0;
while (have_posts()) : the_post(); $count++;
if ( $count == 1 ) echo '<div class="top">';
if ( $count > 5 ) echo '</div><div class="bottom">';
the_title();
if ( $count > 5 ) the_excerpt();
endwhile;
echo "</div>"
?>
This might help you.
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<div class="top">
<?php the_title();?>
</div>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<div class="bottom">
<?php the_title();?> blah
<?php the_excerpt();?>
</div>
<?php endif; ?>
<?php endwhile; ?>
I have a WordPress starter theme and one of the features is the ability to chose different archive formats by selecting different template parts. My index.php essentially looks like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- To see additional archive styles, visit the /parts directory -->
<?php get_template_part( 'parts/loop', 'archive' ); ?>
<?php endwhile; ?>
<?php joints_page_navi(); ?>
<?php else : ?>
<?php get_template_part( 'parts/content', 'missing' ); ?>
<?php endif; ?>
One of the archive formats is a grid format, which essentially needs to output like so:
Start Row
Post 1
Post 2
Post 3
End Row
Start Row
Post 4
Post 5
Post 6
End Row
.....
Normally, I use this method:
<?php foreach (array_chunk($posts, 2, true) as $posts) : ?>
<div class="row">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<div class="six columns">
<?php the_content(); ?>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
However, that piece of code requires a different type of loop than the standard WP loop, which makes it hard to integrate into the theme without the user having to also make adjustments to the loop.
So my question is, is it possible to wrap X amount of posts with a div without modifying the standard WordPress loop?
You can start iterating (counting) over posts by using the iterator $i;
In your archive.php or index.php (the file which has the main query):
<?php if (have_posts()) :
$i = 1; //Start counting
while (have_posts()) : the_post(); ?>
<!-- To see additional archive styles, visit the /parts directory -->
<?php get_template_part( 'parts/loop', 'archive' ); ?>
<?php $i++; //Increase $i ?>
<?php endwhile; ?>
<?php joints_page_navi(); ?>
<?php else : ?>
<?php get_template_part( 'parts/content', 'missing' ); ?>
<?php endif; ?>
And in your parts/loop file (which has the loop "<article></article>"), make calculations to check the current post index and decide whether to skip, start or close the wrapper tag:
<?php
//Doing some math
$x = 4 //Change this to any number you want
if ( $i == 1 || $i == $x || $i % $x == 1 ) {
$before_article = '<div class="wrapper">'; //The wrapper start tag
}
if ( $i % $x == 0 || $wp_query->current_post + 1 == $wp_query->post_count ) {
$after_article = '</div>'; //The wrapper end tag
}
<?php echo $before_article; ?>
<article>
<!-- post content here -->
</article>
<?php echo $after_article; ?>
<?php $before_article = ''; $after_article = ''; ?>
In Wordpress, I want to have a div wrapping around every three posts (because the posts are in a grid, three per line, and I want each line to have a uniform height so the "read more" buttons line up at the bottom - http://restartcomputer.com/category/products/mac-products/). I figured out (logically) how to do this - it is basically outlined in the accepted answer of this question: PHP loop: Add a div around every three items syntax
However, I've tried everything and cannot get the code to work. The divs do not get added, at all. Here is the code:
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
Any idea why?
You have not entered back into Script (Line 4):
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
I did something similar in my theme to align posts side by side:
<?php if ( have_posts() ) : ?>
<?php $col = 1; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($col == 1) echo "<div class='row'>"; ?>
<div class="post col<?php echo $col; ?>" id="post-<?php echo the_ID(); ?>">
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php (($col==1)?$col=2:$col=1); ?>
</div>
<?php if($col == 1) echo "</div>"; ?>
<?php endwhile; ?>
<?php kubrick_paging_nav(); ?>
<?php else : ?>
Can also try moving the DIV within the WHILE statement.
I am running a wordpress if while statement and would like my items to display in a column format.
I am currently using 960.gs which is your standard 960 grid system, and by default adds 10px padding to left and right, by simply passing a class of alpha or omega you can get rid of these.
How do I get php to execute a statment for every 1st one to add alpha and 4th one omega?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php endwhile; endif; ?>
Try maybe something like this. I wasn't sure what you meant by every 1st and 4th so I made it the way you can see. You should be able to customize it yourself.
<?php $counter = 0; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$div = '<div class="entry';
if ($counter % 4 == 0)
$div .= " alpha";
else if($counter % 4 == 3)
$div .= " omega";
echo $div . '">';
?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php $counter++; ?>
<?php endwhile; endif; ?>
Try this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry <?php if (!($i++ % 4)) { ?>alpha<?php } ?> <?php if (!(($j++)+1 % 4)) { ?>omega<?php } ?>">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php endwhile; endif; ?>
Looks kind of like a repeat of something I've seen recently. (Not your fault, it was poorly named and you wouldn't have been able to search on it). Help needed improving a foreach loop
I think what you're looking for would be more precisely described as class=alpha on row 4k+1, class=omega on row=4k
I'm not familiar with your if/while syntax, and the embedded PHP throws me off a little. But I think what you want to do:
is initialize a counter to 0 before the while loop ($i = 0). Increment it at the end of the loop ($i++)
I'm not sure where you want to put these classes, but put if ($i%4==1) class="alpha" and if(i%4==0) class="omega" (add php tags as appropriate, for some reason they werent showing up right for me).