I need PHP code to add every 2 items inside DIV in WordPress loop.
For example, I need like this:
<div class="wrap">
post
post
</div>
<div class="wrap">
post
post
</div>
<div class="wrap">
post
post
</div>
This is my wordpress loop, but not working, I need every 2 posts inside DIV:
<?php if ( have_posts() ) : // If have post start. ?>
<?php $i = 0; ?>
<?php while ( have_posts() ) : the_post(); // Start Loop: ?>
<?php if ( $i % 2 == 0) : ?>
<div class="wrap">
<?php endif; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
</article>
<?php if ( $i % 2 == 0 ) : ?>
</div>
<?php endif; ?>
<?php $i++; endwhile; // End Loop. ?>
<?php endif; // If have post end. ?>
Thanks.
The problem is that you print both <div> and </div> on even values of $i. That's why they always wrap only one and the second post stands aside.
You have to echo the <div> on even numbers and </div> on odd:
<?php if ( have_posts() ) : // If have post start. ?>
<?php $i = 0; ?>
<?php while ( have_posts() ) : the_post(); // Start Loop: ?>
<?php if ( $i % 2 == 0) : ?>
<div class="wrap">
<?php endif; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
</article>
<!-- changed == 0 to != 0 -->
<?php if ( $i % 2 != 0 ) : ?>
</div>
<?php endif; ?>
<?php $i++; endwhile; // End Loop. ?>
<!-- added closing </div> for odd number of posts -->
<?php if ( $i % 2 != 0 ) : ?>
</div>
<?php endif; ?>
<?php endif; // If have post end. ?>
I added a second </div> after the loop, because without it you wouldn't get the closing tag at all if you have an odd number of posts.
I think this should do the job:
<div class="wrap">
<?php
$query = new WP_Query();
if ( $query->have_posts() ):
$i=0;
while ( $query->have_posts() ) :
$query->the_post();
if($i%2==0 && $i<$query->post_count-1 && $i>0):
echo '</div><div class="wrap">'
endif;
?>
<!--html here-->
<?php
$i++;
endwhile;
endif;
?>
</div>
You should do something like this:
<?php
if ( have_posts() ) {
$i=0;
while ( have_posts() ) {
if($i%2==0):?>
<div class="wrap">
<?php
else : ?>
</div>
<?php
endif;
$i++;
}
}
?>
Ok, based on the answer by Aviram here I created this:
$i = 1;
$out = '';
$endingNeeded = false;
if ( have_posts() ) {
while ( have_posts() ) {
if ( $i % 2 == 1) {
$out .= '<div class="wrap">';
$endingNeeded = true;
}
the_post(); // Start Loop:
$out .= '<article id="post-'. get_the_ID().'" class="'.implode(get_post_class(), ', ').'">
'.get_the_content().'
</article>';
if ( $i % 2 == 0 ) {
$out .= '</div>';
$endingNeeded = false;
}
$i++;
}
}
if($endingNeeded) {
$out .= '</div>';
}
echo $out;
Should be working fine.
Correct loop should look like this:
<?php if(have_posts()) : ?>
<?php $no_of_posts = wp_count_posts()->publish; ?>
<div class="wrap">
<?php $i=1; while(have_posts()) : the_post(); ?>
<div class="post">
post text
</div>
<?php if($i%2==0 && $i!=$no_of_posts) : ?>
</div>
<div class="wrap">
<?php endif; ?>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>
You meaning this like?
<?php
if ( have_posts() ) {
while ( have_posts() ) {
for ($i=0; $i<2; $i++) {
// what do you like to do
?>
<!-- HTML CODE -->
<?php
}
}
}else{
// ...
}
?>
Why do you not add a simple for loop in the WordPress loop?
Related
My page is http://prowrestlingnews.co. I am looking to remove the excerpts from the homepage, as this is a news headline website. I prefer for the main content (middle section) to display headlines like it does on the right sidebar with the "Latest Wrestling News" widget.
I have been playing around with the index and functions file, but can't seem to remove the excerpts. Any help would be greatly appreciated!
Here's the index.php file:
<?php
/**
* The main template file.
*
* Used to display the homepage when home.php doesn't exist.
*/
?>
<?php $mts_options = get_option(MTS_THEME_NAME); ?>
<?php get_header(); ?>
<div id="page">
<div class="article">
<div id="content_box">
<?php if ( !is_paged() ) { ?>
<?php if ( is_home() && $mts_options['mts_featured_slider'] == '1' ) { ?>
<div class="primary-slider-container clearfix loading">
<div id="slider" class="primary-slider">
<?php if ( empty( $mts_options['mts_custom_slider'] ) ) { ?>
<?php
// prevent implode error
if ( empty( $mts_options['mts_featured_slider_cat'] ) || !is_array( $mts_options['mts_featured_slider_cat'] ) ) {
$mts_options['mts_featured_slider_cat'] = array('0');
}
$slider_cat = implode( ",", $mts_options['mts_featured_slider_cat'] );
$slider_query = new WP_Query('cat='.$slider_cat.'&posts_per_page='.$mts_options['mts_featured_slider_num']);
while ( $slider_query->have_posts() ) : $slider_query->the_post();
?>
<div>
<a href="<?php echo esc_url( get_the_permalink() ); ?>">
<?php the_post_thumbnail('schema-slider',array('title' => '')); ?>
<div class="slide-caption">
<h2 class="slide-title"><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php } else { ?>
<?php foreach( $mts_options['mts_custom_slider'] as $slide ) : ?>
<div>
<a href="<?php echo esc_url( $slide['mts_custom_slider_link'] ); ?>">
<?php echo wp_get_attachment_image( $slide['mts_custom_slider_image'], 'schema-slider', false, array('title' => '') ); ?>
<div class="slide-caption">
<h2 class="slide-title"><?php echo esc_html( $slide['mts_custom_slider_title'] ); ?></h2>
</div>
</a>
</div>
<?php endforeach; ?>
<?php } ?>
</div><!-- .primary-slider -->
</div><!-- .primary-slider-container -->
<?php } ?>
<?php
$featured_categories = array();
if ( !empty( $mts_options['mts_featured_categories'] ) ) {
foreach ( $mts_options['mts_featured_categories'] as $section ) {
$category_id = $section['mts_featured_category'];
$featured_categories[] = $category_id;
$posts_num = $section['mts_featured_category_postsnum'];
if ( 'latest' == $category_id ) { ?>
<?php $j = 0; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="latestPost excerpt <?php echo (++$j % 3 == 0) ? 'last' : ''; ?>">
<?php mts_archive_post(); ?>
</article>
<?php endwhile; endif; ?>
<?php if ( $j !== 0 ) { // No pagination if there is no posts ?>
<?php mts_pagination(); ?>
<?php } ?>
<?php } else { // if $category_id != 'latest': ?>
<h3 class="featured-category-title"><?php echo esc_html( get_cat_name( $category_id ) ); ?></h3>
<?php
$j = 0;
$cat_query = new WP_Query('cat='.$category_id.'&posts_per_page='.$posts_num);
if ( $cat_query->have_posts() ) : while ( $cat_query->have_posts() ) : $cat_query->the_post(); ?>
<article class="latestPost excerpt <?php echo (++$j % 3 == 0) ? 'last' : ''; ?>">
<?php mts_archive_post(); ?>
</article>
<?php
endwhile; endif; wp_reset_postdata();
}
}
}
?>
<?php } else { //Paged ?>
<?php $j = 0; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="latestPost excerpt <?php echo (++$j % 3 == 0) ? 'last' : ''; ?>">
<?php mts_archive_post(); ?>
</article>
<?php endwhile; endif; ?>
<?php if ( $j !== 0 ) { // No pagination if there is no posts ?>
<?php mts_pagination(); ?>
<?php } ?>
<?php } ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Functions.php file viewed at https://gist.github.com/pwz2k/16a8aa858ee3d739276c67d56fbccca9
Im having some trouble with a else statement i'm trying to write. Basicly if the repeater field is there and displays the content do that, if no content is in the repeater field display this.
Here is the original code without the else statement that I started from
<div class="tabs-panel" id="panel5">
<?php
if ( have_rows( 'ar_content' ) ):
$i = 0;
$n = count( get_field('ar_content') );
?>
<div class="row">
<?php
while ( have_rows( 'ar_content' ) ):
the_row();
$i++;
?>
<div class="small-12 medium-4 columns">
<?php the_sub_field('ar_block'); ?>
</div>
<? if ($i % 3 == 0 && $i < $n) : ?>
</div>
<div class="row">
<?php
endif;
endwhile;
?>
</div>
<? endif; ?>
</div><!-- end panel 5 -->
Here is the code I am trying to get working. I would think its would be just replace the last endif with a else and move on the to else content?
<div class="tabs-panel" id="panel5">
<?php
if ( have_rows( 'ar_content' ) ):
$i = 0;
$n = count( get_field('ar_content') );
?>
<div class="row">
<?php
while ( have_rows( 'ar_content' ) ):
the_row();
$i++;
?>
<div class="small-12 medium-4 columns">
<?php the_sub_field('ar_block'); ?>
</div>
<? if ($i % 3 == 0 && $i < $n) : ?>
</div>
<div class="row">
<?php
endif;
endwhile;
?>
</div>
<? } else { ?>
<h2>content to show if nothing is above</h2>
<?php endif; ?>
</div><!-- end panel 5 -->
Not working though, any thoughts. And yes if this is totally jacked i'm new to PHP
You are using wrong syntax, please read here, change:
<? } else { ?>
to:
<? else: ?>
You should use either the colon or the brackets syntax, but not both.
<div class="tabs-panel" id="panel5">
<?php
if ( have_rows( 'ar_content' ) ) {
$i = 0;
}
$n = count( get_field('ar_content') );
?>
<div class="row">
<?php
if(have_rows( 'ar_content' )){
while ( have_rows( 'ar_content' ) ){
the_row();
$i++;
?>
<div class="small-12 medium-4 columns">
<?php the_sub_field('ar_block'); ?>
</div>
<? if ($i % 3 == 0 && $i < $n) { ?>
</div>
<div class="row">
<?php
} // endif;
} // endwhile;
?>
</div>
<?php } else {
echo "<h2>content to show if nothing is above</h2>";
}
?>
This is what you need.
I have a piece of php code and I need to close the php tags in the right way, but I am not sure what the best way is because I have html and php mixed up. I have removed the tags from the part I am not sure about.
<div id="fphItems">
$i = 1;
query_posts( 'posts_per_page=4&cat=3' );
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if ( $i < 4 ) {
echo '<div class="fphItem">';
}
else {
echo '<div class="fphLastItem">';
}
if ( has_post_thumbnail() ) {
the_post_thumbnail();
<div class="fphItemTitle">
<?php the_title(); ?>
</div>
}
}
}
else {
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
}
<?php wp_reset_query();
</div>
You need to open and close php tag anytime you use php code
<div id="fphItems">
<?php
$i = 1;
query_posts( 'posts_per_page=4&cat=3' );
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if ( $i < 4 ) {
echo '<div class="fphItem">';
}
else {
echo '<div class="fphLastItem">';
}
if ( has_post_thumbnail() ) {
the_post_thumbnail();
?>
<div class="fphItemTitle">
<?php the_title(); ?>
</div>
<?php
}
}
}
else {
?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php
}
?>
<?php wp_reset_query(); ?>
</div>
Use this modified code. See the comments inside the code where you need to open/close them appropriately.
<div id="fphItems">
<?php //<--- Open Here
$i = 1;
query_posts( 'posts_per_page=4&cat=3' );
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if ( $i < 4 ) {
echo '<div class="fphItem">';
}
else {
echo '<div class="fphLastItem">';
}
if ( has_post_thumbnail() ) {
the_post_thumbnail();
?> <!-- Close here -->
<div class="fphItemTitle">
<?php the_title(); ?>
</div>
<?php // Open here again
}
}
}
else {
echo "<p>";
echo('Sorry, no posts matched your criteria.');
echo "</p>";
}
wp_reset_query(); ?> <!-- Close here -->
</div>
<?php $counter = 3; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('3') ): ?>
<?php else: ?>
<?php endif; ?>
<?php if($counter%2 == 0){echo 'floatRight';} else { echo 'floatLeft'; } ?>
<?php the_ID(); ?>
<h1 > <?php the_title(); ?></h1>
<?php the_post_thumbnail('full'); ?>
<?php the_content(__('(more...)')); ?>
<?php comments_template(); // Get wp-comments.php template ?>
<?php if($counter%2 == 0){ echo "<div class='clear'></div>";} ?>
<?php $counter++; ?>
<?php endwhile; else: ?>
<?php endif; ?>
i am trying to display in this way and i want to display particular category of post
post1
post2
post3
post4
please give me the solution ...
use the below code.
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
<?php $i = 0; rewind_posts(); ?>
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
You can use this css3 property to do it (This would take care odd and even elements, you don't have to write down the loop explicitly):
p:nth-child(odd) //you can do the same for div
{
float:left;
}
p:nth-child(even)
{
float:right;
}
You can use query_post() or Wp_query() in wordpress .
Use this code. to get the particular categories post .
<?php
// The Query
query_posts( 'cat=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
My client is looking to change her homepage to look similar to this site: http://www.eatliverun.com/
Where the most recent post is up top in full in one column and the others are in two column excerpts.
She wants to have just one recent post display and the rest in the two column format. I'm just not sure how to accomplish this correctly.
Her website is located at http://pamelastable.com/
Integrating a counter into your loop can easily help you achieve this layout. Try something as follows (note: the 'six columns' class represents your 2 column layout in a 12 column grid):
<?php $count = 1; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ($count == 1) { ?>
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
<?php $count++; ?>
<?php } else { ?>
<?php if($count % 2 == 0) { ?>
<div>
<? } ?>
<div class="six columns">
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
</div>
<?php if($count % 2 == 1) { ?>
</div>
<? } ?>
<?php $count++; ?>
<? } ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Just add a counter to the loop and style the html differently for the first iteration.
<?php $i = 0; while ( have_posts() ) : the_post(); //inside loop ?>
<?php if ($i++ == 0) : ?>
<div>First Post</div>
<?php else: ?>
<div>Other Posts</div>
<?php endif; ?>
<?php endwhile; ?>
In your index.php or other template file^
Just use a counter to see which post number you are on.
<?php
if (have_posts()) {
$i = 0;
while (have_posts()) {
the_post();
if (++$i == 1) {
echo '<div class="entry-large">';
} else {
echo '<div class="entry-small">';
}
the_content();
echo '</div>'
}
}