PHP split list items into 2 columns - php

I am running a Wordpress site, that uses individual ACF fields to store bullet point text.
<?php if( get_field('bullet_points') ): ?>
<div class="row">
<div class="col-3">
<p class="big"><?php the_field('bullet_point_text'); ?></p>
</div>
<div class="col-3">
<ul>
<?php while( has_sub_field('bullet_points') ): ?>
<?php $post_counter++; ?>
<li><?php the_sub_field('individual_bullet_point'); ?></li>
<?php
if ( 0 == $post_counter % 5 ) {
echo '</ul></div><div class="col-3"><ul>';
}
?>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endif; ?>
The above code takes the fields (bullet_points) and outputs them in multiple columns every 5 points - I hardcoded this to make sure the code works. How would I get it to output these into 2 (as evenly) spaced lists?
So, if I have 6 points, I want 3 in the first DIV and 3 in the second DIV. Similarly if I have 7 points, I want 3 in the first DIV and 4 in the second DIV.
Any help would be appreciated!

I believe this will work...
<?php $count_bullets = count(get_sub_field('bullet_points')); ?>
<?php $col_items = ceil( $count_bullets / 2 ); ?>
<?php while( has_sub_field('bullet_points') ): ?>
<?php $post_counter++; ?>
<li><?php the_sub_field('individual_bullet_point'); ?></li>
<?php
if ( 0 == $post_counter % $col_items ) {
echo '</ul></div><div class="col-3"><ul>';
}
?>
<?php endwhile; ?>

Related

using an if statement inside a while with wordpress

Hi guys I'm using a while loop to display my posts and while I have posts I want to display the odd and even post numbers differently. I currently have the following code:
<div class="container">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
</div>
<?php query_posts('post_type=portfolio&post_status=publish&posts_per_page=8&paged='. get_query_var('paged'));
if( have_posts() ):
echo '<div>';
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
$i = 0;
if($i % 2 == 0)
{?>
<div class="class1">
html content 1 to go here
</div>
<?php
$i++;
}else{
?>
<div class="class2">
html content 2 to go here
</div>
<?php $i++; } ?>
<?php endif; ?>
<?php endwhile; ?>
<div class="container">
<div class="navigation">
<span class="newer"><?php previous_posts_link(__('« See Previous Case Studies','example')) ?></span>
<span class="older"><?php next_posts_link(__('See Next Case Studies »','example')) ?></span>
</div>
</div> <!-- /.navigation -->
<?php else: ?>
<div id="post-404" class="noposts">
<p><?php _e('None found.','example'); ?></p>
</div><!-- /#post-404 -->
<?php endif; wp_reset_query(); ?>
I haven't used php for a while, so I'm unsure the best way to do the above. I'm currently getting a few errors relating to syntax such as unexpected end of file or unexpected } on line...
Can anyone figure out where I am going wrong?
Reverse Position
<?php endwhile; ?>
<?php endif; ?>
First While statement should get complete than if.

ACF Repeater Fields different counts

I have a slice of code:
<?php
if( have_rows('our_people') ):
$i = 0;
while ( have_rows('our_people') ) : the_row(); ?>
<?php $i++; ?>
<?php if( $i > 3 ):
break; ?>
<?php endif; ?>
<a class="people-thumb fancybox-inline" href="#fancybox-<?php echo $i;?>">
<div class="people-thumb-image" style="width:172px;height:172px;overflow:hidden;background:transparent url(<?php the_sub_field('people_image'); ?>) no-repeat center center; background-size:cover;"></div>
<div class="people-thumb-details">
<h3><?php the_sub_field('people_name_acf'); ?></h3>
<p><?php the_sub_field('people_title'); ?></p>
</div>
</a>
<div class="peopleoverlay">
<div id="fancybox-<?php echo $i;?>">
<img src="<?php the_sub_field('people_image'); ?>" width="172" style="float:left;margin:0 10px 10px 0;"> <?php the_sub_field('people_details'); ?>
</div>
</div>
<?php endwhile;
else :
endif;
?>
What this does it simply count the entries and then stop after 3 - works fine.
However, with the other rows in the repeater field (after 3) how do I get those to also display? Reasoning is these 3 are in a seperate styling div, and all other entries are placed in another div.
However, if I try to repeat this code, nothing shows. So I must need to reset this somehow? I want all repeater rows after 3 to show in a different section.
It looks that your code specifically says not to display more than 3 results! Annotated below.
$i = 0; // Start a counter
while ( have_rows('our_people') ) : the_row(); ?> // As long as there are posts to display
<?php $i++; ?> // increment the counter
<?php if( $i > 3 ): // if the counter is 4 or more...
break; ?> // don't execute the code below; jump out of the while loop.
<?php endif; ?>
That being said, remove $i = 0, and the following lines:
<?php $i++; ?>
<?php if( $i > 3 ):
break; ?>
<?php endif; ?>
This will allow ACF to display all the people stored in that metafield.
However, if you'd like to add a different class to the 4th div and beyond, change the conditional, to remove the break statement, and to wrap the result in a div that identifies it.
<?php $i++; ?>
<?php if( $i > 3 ): ?>
<div class="other-style">
<?php endif; ?>
Then, you'd have to close it at the end with another check.
...
<?php if( $i > 3 ): ?>
</div> <!-- .other-style -->
<?php endif; ?>

PHP - Adding divs to a foreach loop every 4 times, working with $post_object

I am building a wordpress page (based on quark, using ACF) showing info from other posts using foreach(get_field('exhibitions') as $post_object)
I want the 'col grid_3_of_12-divs' containing info from other posts to group up in divs, with four in each div. A similar problem as the author of this question had: Is there an easy way to do 4 at a time in a php foreach loop
I have tried with array_chunk, but can't seem to make it without messing with the retrieving of info from the other posts.
Can someone help me with this?
I am a self taught php beginner, so please excuse me if my code is stupid..
<?php get_header(); ?>
<div id="primary" class="site-content row clearfix" role="main">
<div class="col grid_12_of_12">
<?php while ( have_posts() ) : the_post(); ?>
<?php foreach(get_field('exhibitions') as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div class="col grid_3_of_12">
<h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>
<?php $attachment_id = $post_object->thumbnail;
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
<img src="<?php echo $image_attributes[0]; ?>">
<p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
</div>
</a>
<?php endforeach; ?>
<?php content_nav( 'nav-below' ); ?>
<?php endwhile; ?>
</div>
</div>
Add a counter. Then when the counter is at the right spot, add a close or open div.
<?php get_header(); ?>
<div id="primary" class="site-content row clearfix" role="main">
<div class="col grid_12_of_12">
<?php while ( have_posts() ) : the_post(); ?>
<?php $counter = 0; /* ADD THIS */ ?>
<?php foreach(get_field('exhibitions') as $post_object): ?>
<?php if ($counter % 4 == 0): /* ADD THIS */ ?>
<div class="group-of-4-posts-wrapper">
<?php endif; ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div class="col grid_3_of_12">
<h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>
<?php $attachment_id = $post_object->thumbnail;
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
<img src="<?php echo $image_attributes[0]; ?>">
<p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
</div>
</a>
<?php if ($counter % 4 == 3): /* ADD THIS */ ?>
</div>
<?php endif; ?>
<?php $counter++ ?>
<?php endforeach; ?>
<?php
// this closes the div if there is not a number of posts that is evenly
// divisable by 4, like 11 posts. with 11 posts, the last post would have
// ($counter % 4 == 3) equal to false, because $counter % 4 would = 2
// adding this, closes the div, if it was not already closed
if ($counter % 4 != 0): /* ADD THIS. */
?>
</div>
<?php endif; ?>
<?php content_nav( 'nav-below' ); ?>
<?php endwhile; ?>
</div>
</div>

Find oldest post — WordPress

Thanks to some answers on here, I've managed to distinguish my posts into the latest post and all the rest. However, it needs to be the oldest post. Here is my current loop:
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="down">a</div>
</div>
</div>
<?php else :?>
<!-- second post -->
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="up">b</div>
</div>
</div>
<?php endif; ?>`
I saw somewhere that you can use a while loop to target the last post from a post.length. However, I am unsure how to implement this.
Yes, you're right. Use count.
Suppose the total posts is 5 for $total_posts = count($posts);.
You'll have to check your $counter for total - 1 as the array is $posts[0], $posts[1], $posts[2], $posts[3], $posts[4].
<?php
if ( have_posts() ) :
$counter = 0;
$total_posts = count($posts) - 1;
while ( have_posts() ) :
the_post();
if( $counter == $total_posts ) {
?>
<div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
<?php
} else {
?>
<div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
<?php
}
$counter++;
endwhile;
endif;
Note that you only need to open <?php and close ?> when changing from PHP to HTML, using them at every line is very confusing.

Show if greater than 3 related articles within same category in wordpress

I seem to be having difficulty.
I need to show a specific piece of text if there are more than or equal to 3 post on a post page template in wordpress. (loop-single.php)
It should be dynamic enough to detect if the total number of post in related category is greater or equal to 3.
here is a code I found which works well on category template pages(archive.php) but it messes up when I use it in a post template.
<?php
$count = 1;
if (have_posts()) : while(have_posts()): the_post(); ?>
<!-- Less than 3 post - nothing shown at all -->
<?php $count++;
endwhile; endif; ?>
<?php if ($count > '3') { ?>
<div> This line shown when 3 or more posts are in current post category</div>
<?php } ?>
NOTE: I'm trying to get this to work on the loop-single.php template file.
Any help would be greatly appreciated,
Thank You
Code updated to include above solution. I fixed a few syntax errors, but its now throwing a T-STRING error: Parse error: syntax error, unexpected T_STRING
here is my full page code:
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php roots_post_before(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php roots_post_inside_before(); ?>
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>
<!-- POST DATE STAMP -->
<div class="post-top">
<div class="date-stamp">
<b><?php the_time('M d'); ?></b>
</div>
</header>
<div class="entry-content">
<?php the_content(); ?>
</div>
<footer>
<hr />
<?php
$cat = get_query_var('cat');
$posts = get_posts(array('category' => $cat));
if(count($posts) >= 3)
{
<!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) -->
<h5>Featured: <?php $cats=get_the_category(); echo $cats[0]->cat_name; ?></h5>
<div id="foot-container">
<?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p comments_class=comment-count]"); ?>
<div style="clear:both;"></div>
</div>
<hr />
}
else
{
Why hello there LESS than three
}
?>
</footer>
<?php comments_template(); ?>
<?php roots_post_inside_after(); ?>
</article>
<?php roots_post_after(); ?>
<?php endwhile; /* End loop */ ?>
This should get you started:
<?php
$cat = get_query_var('cat');
$posts = get_posts(array('category' => $cat));
if(count($posts) >= 3)
{
//CODE EXECUTED IF THREE OR MORE POSTS EXIST IN CURRENT CATEGORY
}
else
{
//CODE EXECUTED IF LESS THAN THREE POSTS EXIST IN CURRENT CATEGORY
}
?>
EXTRA INFO: The reason why it was failing was because your loop was only performing one iteration. Single posts won't go through the loop more than once because.... well...... it's a single post. What this approach does is take the existing category, and queries all Wordpress posts in the matching category. Using PHP's count function will give you the exact number of posts found with the given parameters.
Word of warning: the script above will not find ALL posts in the matching category. Only the five most recent ones in that given category. If you want an actual total of all matching posts, change one line to the following:
$posts = get_posts(array('category' => $cat, 'numberposts' => -1));
UPDATES TO CODE: This line:
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>"> //missing semicolon after post_class()
And this block:
<?php
$cats=get_the_category();
$posts = get_posts(array('category' => $cats[0]->cat_ID));
if(count($posts) >= 3)
{
?>
<!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) -->
<h5>Featured: <?php echo $cats[0]->cat_name; ?></h5>
<div id="foot-container">
<?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p comments_class=comment-count]"); ?>
<div style="clear:both;"></div>
</div>
<hr />
<?php
}
else
{
echo 'Why hello there LESS than three';
}
?>

Categories