Wordpress Loop Special Html Output - php

I am trying to get a special wordpress loop html output:
<div id="main">
<!-- Line number 1 -->
<div class="article-thumbnail" data-target="article-1" >
<div class="article-open"><img src="" /></div>
</div>
<div class="article-thumbnail" data-target="article-2" >
<div class="article-open"><img src="" /></div>
</div>
<div class="article-thumbnail" data-target="article-3" >
<div class="article-open"><img src="" /></div>
</div>
<div class="container">
<article id="article-1" class="article-entry">
<header class="article-header">
<h2>This is the article 1 text</h2>
</header>
<div class="article-body">
<p>This is the article 1 body</p>
</div>
</article>
<article id="article-2" class="article-entry">
<header class="article-header">
<h2>This is the article 2 text</h2>
</header>
<div class="article-body">
<p>This is the article 2 body</p>
</div>
</article>
<article id="article-3" class="article-entry">
<header class="article-header">
<h2>This is the article 3 text</h2>
</header>
<div class="article-body">
<p>This is the article 3 body</p>
</div>
</article>
</div>
<!-- Line number 2 -->
<div class="article-thumbnail" data-target="article-4" >
<div class="article-open"><img src="" /></div>
</div>
<div class="article-thumbnail" data-target="article-5" >
<div class="article-open"><img src="" /></div>
</div>
<div class="article-thumbnail" data-target="article-6" >
<div class="article-open"><img src="" /></div>
</div>
<div class="container">
<article id="article-4" class="article-entry">
<header class="article-header">
<h2>This is the article 4 text</h2>
</header>
<div class="article-body">
<p>This is the article 4 body</p>
</div>
</article>
<article id="article-5" class="article-entry">
<header class="article-header">
<h2>This is the article 5 text</h2>
</header>
<div class="article-body">
<p>This is the article 5 body</p>
</div>
</article>
<article id="article-6" class="article-entry">
<header class="article-header">
<h2>This is the article 6 text</h2>
</header>
<div class="article-body">
<p>This is the article 6 body</p>
</div>
</article>
</div>
</div>
Check this fiddle file: http://jsfiddle.net/PkZrZ/5/ to get an idea of what I am trying to achieve.
Basically, what I am trying to get from The Wordpress Loop is 1 line of 3 thumbnails (featured post's images) and beneath them post entry for each of that thumbnail, afterwards another line and so on.
I've managed to achieve something, but to be honest, it is incredibly buggy (once it worked, and afterwards it didn't) and seems wrong. Anyway, here is The Wordpress Loop I have so far:
<?php
$post_array = array();
$i = 0;
$j = 0;
$index = 0;
$post_total = $wp_query->post_count; // buggy
/*$post_total = get_term_by('name','ventures','category');
$post_total = $post_total->count;*/
// echo $post_total;
if (have_posts()) : while (have_posts()) : the_post();
$i++;
// echo $i;
$post_array[] = get_object_vars($post);
?>
<div id="post-ventures-image-<?php the_ID();?>" class="post-ventures-image">
<?php the_post_thumbnail( 'hnp-thumb-ventures-180' ); ?>
</div>
<?php if($i%3 == 0 && $post_total >= 3) : ?>
<?php for($j = 0; $j < 3; $j++) : ?>
<article id="post-<?php echo $post_array[$index + $j]['ID'];//the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header class="article-header">
<h3 class="h2"><?php echo $post_array[$index + $j]['post_title'];//the_title(); ?></h3>
</header> <!-- end article header -->
<section class="entry-content clearfix">
<?php echo $post_array[$index + $j]['post_content'];//the_content(); ?>
</section> <!-- end article section -->
</article> <!-- end article -->
<?php endfor; $index = $index + $j; $j = 0; $post_total = $post_total - 3; $i = 0; ?>
<?php elseif($i%2 == 0 && $post_total == 2 ) : ?>
<?php for($j = 0; $j < 2; $j++) : ?>
<article id="post-<?php echo $post_array[$index + $j]['ID'];//the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header class="article-header">
<h3 class="h2"><?php echo $post_array[$index + $j]['post_title'];//the_title(); ?></h3>
</header> <!-- end article header -->
<section class="entry-content clearfix">
<?php echo $post_array[$index + $j]['post_content'];//the_content(); ?>
</section> <!-- end article section -->
</article> <!-- end article -->
<?php endfor; $index = $index + $j ; $j = 0; $post_total = $post_total - 2; $i = 0; ?>
<?php elseif($i%1 == 0 && $post_total == 1) : ?>
<?php for($j = 0; $j < 1; $j++) : ?>
<article id="post-<?php echo $post_array[$index + $j]['ID'];//the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header class="article-header">
<h3 class="h2"><?php echo $post_array[$index + $j]['post_title'];//the_title(); ?></h3>
</header> <!-- end article header -->
<section class="entry-content clearfix">
<?php echo $post_array[$index + $j]['post_content'];//the_content(); ?>
</section> <!-- end article section -->
</article> <!-- end article -->
<?php endfor; $index = $index + $j + 1; $j = 0; $i = 0; ?>
<?php endif;?>
<?php endwhile; ?>
<?php endif; ?>
I think it's out of discussion that this the worst improvized coding ever seen.
If anyone can write me a quick markup on how to write the loop that would be swell :)

Updated:
$posts = get_posts();
$first_three = array_chunk($posts, 3);
$count = 0;
foreach($first_three as $posts){
foreach($posts as $post){?>
<div class="article-thumbnail" data-target="article-1" >
<div class="article-open"><?php echo $post->post_title; ?></div>
</div><?php
}?>
<div class="container"><?php
foreach($posts as $post){$count++?>
<article id="article-<?php echo $count; ?>" class="article-entry">
<header class="article-header">
<h2>This is the article <?php echo $count; ?> text</h2>
</header>
<div class="article-body">
<p>This is the article <?php echo $count; ?> body</p>
</div>
</article><?php
}?>
</div><?php
}
Works perfect for me, just change the markup whatever u need or array.

Related

Looking to make archive.php display in two columns for Wordpress site

Very new Wordpress apprentice here. Trying to get my archive page to display posts in two columns going like:
Post 1 Post 2
Post 3 Post 4
Here is an example from a figma we were working on: https://ibb.co/N3XwtwD
My question is, what code can I add to my files to allow this? I currently have some bootstrap classes on the html portion I inserted here and it is displaying as one column, so I don't know if those classes will interfere with anything. Here is my archive.php code below:
<?php
get_header();
?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
while(have_posts()) {
the_post(); ?>
<div class="row">
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<img class="img-fluid mb-3"<?php
the_post_thumbnail();
?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
</div>
<?php }
echo paginate_links();
?>
</div>
<?php
get_footer();
?>
First time posting here so apologies if I left anything important out. Really appreciate this place!
Thanks!
You need to create a row every two posts and you have to add <div class="col-lg-6"> two times in a row. check below code.
<?php get_header(); ?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
$count = 1;
while( have_posts() ) { the_post();
if ( $count % 2 == 1){ ?>
<div class="row">
<?php } ?>
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<?php the_post_thumbnail(); ?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
<?php if ( $count % 2 == 0 ){ ?>
</div>
<?php } $count++; ?>
<?php }
if ( $count % 2 != 1 ) echo "</div>";
echo paginate_links(); ?>
</div>
Please replace with the below code that helps you.
<?php
get_header();
?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
$counterval = 0;
while(have_posts()) {
the_post();
$counterval++;
if($counterval % 2 != 0)
{ ?>
<div class="row">
<?php } ?>
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
if(!empty($featured_img_url)){ ?>
<img class="img-fluid mb-3" src="<?php echo esc_url($featured_img_url); ?>" />
<?php } ?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
<?php
if($counterval % 2 == 0)
{ ?>
</div>
<?php } ?>
<?php } ?>
<?php echo paginate_links();
?>
</div>
<?php
get_footer();
?>

Display container for every two divs in while statment

I'm using foundation and Wordpress with Advanced Custom Field to build a services module.
I'm using the repeater field to insert the data.
Currently I have the row element outside of the while loop.
I need the div row to wrap every two "small-6 columns tgs-single-service" divs so the foundation grid displays properly. If I insert the row div in the while statement, it will wrap every single div in a row, which I do not want.
<?php if( have_rows('services_content') ):
$classNumber = 0
?>
<section class="full-width tgs-services-section">
<div class="row">
<?php while( have_rows('services_content') ): the_row(); ?>
<div class="small-6 columns tgs-single-service-<?php echo $classNumber = $classNumber + 1 ?>">
<figure class="effect-goliath">
<?php while( have_rows('service_images') ): the_row(); ?>
<img src="<?php the_sub_field('small_image');?>" srcset="<?php the_sub_field('medium_image');?> 1000w, <?php the_sub_field('large_image');?> 2000w" alt="Byoungz Poet Logo">
<?php endwhile; ?>
<figcaption>
<h3><?php the_sub_field('service_title');?></h3>
<?php the_sub_field('service_text');?>
</figcaption>
</figure>
</div><!-- end service section -->
<?php endwhile; ?>
</div><!--end row -->
</section>
<?php endif; ?>
Currently this displays as:
<div class="row">
<div class="small-6 columns tgs-service-section-1"></div>
<div class="small-6 columns tgs-service-section-2"></div>
<div class="small-6 columns tgs-service-section-3"></div>
<div class="small-6 columns tgs-service-section-4"></div>
</div>
I would like to achieve
<div class="row">
<div class="small-6 columns tgs-service-section-1"></div>
<div class="small-6 columns tgs-service-section-2"></div>
</div>
<div class="row">
<div class="small-6 columns tgs-service-section-3"></div>
<div class="small-6 columns tgs-service-section-4"></div>
</div>
Is there a way to have row wrap every two tgs-single-service divs?
<section class="full-width tgs-services-section">
<div class="row">
<!-- Create a counter i -->
<?php $i=1; while( have_rows('services_content') ): the_row(); ?>
<div class="small-6 columns tgs-single-service-<?php echo $classNumber = $classNumber + 1 ?>">
<figure class="effect-goliath">
<?php while( have_rows('service_images') ): the_row(); ?>
<img src="<?php the_sub_field('small_image');?>" srcset="<?php the_sub_field('medium_image');?> 1000w, <?php the_sub_field('large_image');?> 2000w" alt="Byoungz Poet Logo">
<?php endwhile; ?>
<figcaption>
<h3><?php the_sub_field('service_title');?></h3>
<?php the_sub_field('service_text');?>
</figcaption>
</figure>
</div><!-- end service section -->
<?php if($i % 2 == 0): ?><!-- Check if its divisable by 2 -->
</div>
<div class="row">
<?php endif; ?>
<?php $i++; ?>
<?php endwhile; ?>
</div><!--end row -->
</section>
Declare a counter, then determine if the count is a multiple of 2:
$i = 0;
//during your loop:
if(($i % 2) == 0){
//echo opening and closing tag
}
Please try this:
<?php
if( have_rows('services_content') ):
$classNumber = 0
$rowPerSection = 2;
$rows = get_field('services_content');
$sections = array_chunk($rows, $rowPerSection);
?>
<?php foreach ($sections as $section): ?>
<section class="full-width tgs-services-section">
<div class="row">
<?php foreach ($section as $row): ?>
# code...
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
<?php endif; ?>

how to display data from 2 in 2 loop php

I have a table in my database with similar data, but others with more data.
What I want is to echo the data through the loop. my problem is that I want to get data from 2 to 2.
My code to display data normally is
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i++) : ?>
<div class="item">
<?php echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;?>
</div>
<?php endfor; ?>
</div>
This returns
<div class="owl-4">
<div class="item">
<h3>Author 1</h3>
<p>Testimony 1</p>
</div>
<div class="item">
<h3>Author 2</h3>
<p>Testimony 2</p>
</div>
................
</div>
How do I turn gives me this
<div class="owl-4">
<div class="item">
<h3>Author 1</h3>
<p>Testimony 1</p>
<h3>Author 2</h3>
<p>Testimony 2</p>
</div>
<div class="item">
<h3>Author 3</h3>
<p>Testimony 3</p>
<h3>Author 4</h3>
<p>Testimony 4</p>
</div>
................
</div>
THANKS
Just increase the counter by 2 instead of 1 each time.
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i += 2) : ?>
<div class="item">
<?php
echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;
echo '<h3>'.$helper->get('authorname', $i + 1).'</h3>';
echo '<p>'.$helper->get('testimony', $i + 1).'</p>' ;
?>
</div>
<?php endfor; ?>
</div>
You can display <div class="item">and </div> each two iterations, for example checking if $i is an even number:
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i++) : ?>
<?php if ($i%2==0): ?>
<div class="item">
<?php endif; ?>
<?php echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;?>
<?php if ($i%2==0): ?>
</div>
<?php endif; ?>
<?php endfor; ?>

Two posts in same div - WP loop

I would need the following in Wordpress:
<div class="posts-wrapped">
<div id="post-1" class="post">
<h1>Title 1</h1>
</div>
<div id="post-2" class="post">
<h1>Title 2</h1>
</div>
</div>
<div class="posts-wrapped">
<div id="post-3" class="post">
<h1>Title 3</h1>
</div>
<div id="post-4" class="post">
<h1>Title 4</h1>
</div>
</div>
I know how to get posts with WP loop, but how can I wrap every two posts in .posts-wrapped div?
I fetch the posts with WP_Query.
Thanks!
EDIT:
So I tried few ways to do it. For example with this:
$index=0;
<div class="posts-wrapped">
<?php
while ( have_posts() ) {
$index++;
?>
<div class="post">
<h1><?php the_post(); ?></h1>
</div> <?php
if(($index % 2) ==0){
echo '</div><div class="posts-wrapped">';
}
} ?>
</div>
But this prints one extra empty posts-wrapped div:
<div class="posts-wrapped">
Lorem ipsum 1<br>
Lorem 2 ipsum est<br>
</div>
<div class="posts-wrapped">
Lorem ipsum 1<br>
Lorem 2 ipsum est<br>
</div>
<div class="posts-wrapped">
</div>
How can i get rid of this last empty div? It screws my carousel (this content is part of carousel).
Get the index that counts the loop iteration and check the modulus operation to know that its even or odd
$index=0;
<div class="posts-wrapped">
<?php
while ( have_posts() ) {
$index++;
?>
<div class="post">
<h1><?php the_post(); ?></h1>
</div> <?php
if(($index % 2) ==0){
echo '</div><div class="posts-wrapped">';
}
} ?>
</div>
this should work:
<?php
while(have_posts()){
the_post();
?>
<div class="posts-wrapped">
<div class="post">
<h1><?php the_title(); ?></h1>
</div>
<?php
if(have_posts()){ //Makes sure you have another post
the_post();
?>
<div class="post">
<h1><?php the_title(); ?></h1>
</div>
<?php
}
?>
</div>
<?php
}
?>
<?php
$i = 0;
if (have_posts()) :
while (have_posts()) : the_post();
if($i == 0) {
echo '<div class="posts-wrapped">';
} ?>
<div class="post">
<h1><?php the_title(); ?></h1>
</div>
<?php
$i++;
if($i == 2) {
$i = 0;
echo '</div>';
}
endwhile;
if($i > 0) {
echo '</div>';
}
?>
Would you please try above code?

Why does this duplicate the posts outside as well as inside the while loop - PHP?

The following produces 2 posts in the loop with the surrounding div row-fluid. Then it creates another 2 posts after. Why is this?
EDIT:code above as requested:
<?php get_header(); ?>
<section class="clearfix" id="home">
<div class="row-fluid">
<?php $count = 1; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="span4">
<div class="wrap-title">
<div class="position">
<div class="title"><?php the_title();?></div>
<div class="tags"><?php echo $cats; ?> </div>
</div>
</div>
</div>
<?php if($count == 2) {
echo '</div>';
}?>
<?php $count++; ?>
<?php endwhile; ?>
I posted here as apposed to wordpress as I'm anticipating it being a PHP syntax error, something with the count variable possibly or where the row-fluid div is positioned.
Although I would create a code differently, the solution to your problem I believe is to modify your code to this:
<?php get_header(); ?>
<section class="clearfix" id="home">
<?php $count = 1; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($count % 2 == 0) {
<div class="row-fluid">
}?>
<div class="span4">
<div class="wrap-title">
<div class="position">
<div class="title"><?php the_title();?></div>
<div class="tags"><?php echo $cats; ?> </div>
</div>
</div>
</div>
<?php if($count % 2 == 0) {
echo '</div>';
}?>
<?php $count++; ?>
<?php endwhile; ?>
EDIT:
You said it produces 2 posts after also - that is why you will add </div> after first two posts, and then after that you will continue printing. The solution would be to use the code above - with % or to print only two posts use this code:
<?php get_header(); ?>
<section class="clearfix" id="home">
<div class="row-fluid">
<?php $count = 1; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="span4">
<div class="wrap-title">
<div class="position">
<div class="title"><?php the_title();?></div>
<div class="tags"><?php echo $cats; ?> </div>
</div>
</div>
</div>
<?php if($count == 2) {
break;
}?>
<?php $count++; ?>
<?php endwhile; ?>
</div>
But the above code could be optimized not to search for all posts, but it should will work as is.

Categories