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.
Related
I'm having an ACF repeater inside a custom post type loop with a counter that makes a row around every two col-md-6's. This works great when I have an even number of col's but not when it's uneven. When a post has an uneven number of col's the counter somehow remembers that for the next post and only displays one col in the first row.
Down here you'll find the current code and a little picture of what is happening.
Somehow I need to reset the counter after each post loop but can't figure it out. wp_reset_postdata doesn't seem to work.
<?php $args = array( 'post_type' => 'posttypename', 'posts_per_page' => '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Just a small change. You need to make sure that your counter is reset to 0 before the loop for the acf starts.
<?php $counter = 0; //Initialize your Counter here before the Loop Starts for each Post ?>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
I've built a form on a WordPress custom post type archive page that filters said posts using values pulled from a taxonomy and set with select multiple fields. The form submits to the same page. It's probably built "the long way round" but it works.
However, now I'm being asked to change the select multiple fields to Checkboxes and I can't seem to get it right. The checked values aren't being passed when the form submits.
Here's my code with my attempt to turn one of the two selects into a set of checkboxes. This first batch just sets the values to be pulled in:
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post();
$topics = get_field('program_topic');
foreach( $topics as $topic ): ?>
<?php $program_topic_names[] = $topic->name ; ?>
<?php $program_topic_links[] = $topic->slug ; ?>
<?php $topic_name_results = array_unique($program_topic_names) ?>
<?php $topic_link_results = array_unique($program_topic_links) ?>
<?php endforeach;
$types = get_field('program_type');
foreach( $types as $type ): ?>
<?php $program_type_names[] = $type->name ; ?>
<?php $program_type_links[] = $type->slug ; ?>
<?php $type_name_results = array_unique($program_type_names) ?>
<?php $type_link_results = array_unique($program_type_links) ?>
<?php endforeach;
endwhile;
$final_topic_results = array_combine($topic_name_results, $topic_link_results);
$final_type_results = array_combine($type_name_results, $type_link_results);
endif;
//print_r($final_topic_results);
//print_r($final_type_results);
wp_reset_postdata();
And the Filter Row / Form:
<!-- Filter row -->
<div class="row filter_row">
<div class="container">
<div class="col span_3_of_12">
<h2>Find a Program</h2>
</div>
<div class="col span_9_of_12">
<form id='program_filters' class='program-filters multiple' method="post" onsubmit="showHide(); return false">
<h4 class="span_12_of_12 no_padding" style="font-weight: 700;">Select one or more Topics and/or Types</h4>
<div class="styled-select multiple span_7_of_12">
<fieldset name="program_topic" id="program_topic">
<?php
foreach( $final_topic_results as $key => $value ):
echo "<input type='checkbox' ".checked( $_GET['program_topic'], $key )." value='$key' name='$key' /> $key <br>";
endforeach;
?>
</fieldset>
</div>
<div class="styled-select span_3_of_12">
<select multiple name="program_type" id="program_type">
<?php
foreach( $final_type_results as $key => $value ):
echo "<option ".selected( $_GET['program_type'], $key )." value='$value'>$key</option>";
endforeach;
?>
</select>
</div>
<div class="button_col span_2_of_12">
<input type='button' id='submitFormData' value='Find' onclick="this.form.submit()";>
<input type='button' value='Reset' onclick="window.location.href='/programs/'">
</div>
</form>
</div>
<div style="clear:left;"></div>
</div>
</div>
<!-- Filter row -->
And here's the Post type code I didn't include before:
<!-- Program row -->
<div class="container program-list-wrap"><ul>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$dateformatstring = "l, F d";
$unixtimestamp = strtotime(get_field('program_date'));
$instructor = get_field('program_instructor_name');
$instructorInfo = get_field('program_instructor_info');
$program_date = get_field('program_date');
$current = time(); ?>
<?php if($current < $unixtimestamp || empty($program_date) ) : ?>
<li><div class="program list">
<div class="row program-intro">
<div class="col span_9_of_12">
<div class="program-vitals">
<h2><?php the_title(); ?></h2>
<?php if(get_field('program_date')) : ?><div class="program-date-time"><h4><?php echo date_i18n($dateformatstring, $unixtimestamp); if(get_field('program_time')) : ?> — <?php the_field('program_time');?><?php endif; ?></h4></div><?php endif; ?>
<?php $types = get_field('program_type');
if( $types ): ?><div class="types">
<?php $i = 1; ?>
<?php foreach( $types as $type ): ?>
<strong><?php echo $type->name; ?></strong><?php $rowCount = count( get_field('program_type') ); if($i < $rowCount): echo ', '; endif; $i++; ?>
<?php endforeach; ?></div>
<?php $topics = get_field('program_topic');
if( $topics ): ?><div class="topics">
<?php $i = 1; ?>
<?php foreach( $topics as $topic ): ?>
<strong><?php echo $topic->name; ?></strong><?php $rowCount = count( get_field('program_topic') ); if($i < $rowCount): echo ', '; endif; $i++; ?>
<?php endforeach; ?></div>
<?php endif; ?>
<?php if($instructor):?><strong>Instructor:</strong> <?php echo $instructor; if($instructorInfo):?>, <?php echo $instructorInfo; endif; endif; ?>
<?php endif; ?></div>
<?php if(get_field('program_intro')) : ?><div><?php the_field('program_intro'); ?></div><?php endif; ?>
</div>
<div class="col span_3_of_12">
<?php $register = get_field('program_registration_url'); $registerAlum = get_field('program_alum_registration_url'); $cost = get_field('program_amount'); $brochure = get_field('program_brochure_url') ?>
<div class="button_col"><h2><?php echo $cost ?></h2>Register HereUMKC Alumni Registration <?php if( $brochure) :?>Download a brochure<?php endif; ?></div>
Learn More
</div>
</div>
<div class="program-extras">
<?php if(get_field('program_includes')) : ?><div><h4><strong><?php the_title() ?> Includes:</strong></h4><?php the_field('program_includes'); ?></div><?php endif; ?>
<?php if(get_field('program_learn')) : ?><div><h2>What You Will Learn</h2><?php the_field('program_learn'); ?></div><?php endif; ?>
<?php if(get_field('program_key_topics')) : ?><div><h2>Key Topics</h2><?php the_field('program_key_topics'); ?></div><?php endif; ?>
<?php if(get_field('program_attendee')) : ?><div><h2>Who Should Attend</h2><?php the_field('program_attendee'); ?></div><?php endif; ?>
<?php if(get_field('program_instructor')) : ?><div><h2>About the Instructor</h2>
<?php $image = get_field('program_instructor_photo');
if( !empty($image) ): ?><div class="program_instructor_photo"><img src="<?php the_field('program_instructor_photo'); ?>);" /></div><?php endif; ?>
<?php the_field('program_instructor'); ?></div>
<?php endif; ?>
<div style="clear: left;"></div>
</div>
</div></li>
<?php endif; ?>
<div style="clear: left;"></div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear: left;"></div>
</ul></div>
<!-- Program row -->
I feel like I'm missing something obvious. Any help would be appreciated.
I'd like to wrap every three posts in a new row (div) with a total of nine posts on a page. There's an empty row at the end. I thought this (I need to wrap every 4 wordpress posts in a div) would work, but I have more posts on pages 2, 3, 4, etc. Below is a simplified version of my code. $i = 1.
<div class="row">
<?php while ( have_posts() ) : the_post(); ?>
<div class="column">
</div>
<?php if ($i % 3 == 0 ) : ?>
</div> <!-- .row -->
<div class="row">
<?php endif; $i++; ?>
<?php endwhile; ?>
</div> <!-- .row -->
You can use get_next_post() to check whether next post exists or not.
<?php if ($i % 3 == 0 ) : ?>
</div> <!-- .row -->
<?php
$next_post = get_next_post();
if (!empty( $next_post )): ?>
<div class="row">
<?php endif; ?>
I used this post (https://www.nosegraze.com/display-wordpress-posts-2-3-columns/) to solve my problem.
$i = 0;
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( $i == 0 ) : ?>
<div class="row">
<?php endif; ?>
<div class="column">
</div> <!-- .column -->
<?php
$i++;
if( $i == 3 ) :
$i = 0; ?>
</div> <!-- .row -->
<?php endif; ?>
<?php endwhile; ?>
<?php if ( $i > 0 ) : ?>
</div> <!-- .row -->
<?php endif; ?>
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?
I'm trying to achieve a 3x3 grid view of all the WordPress posts on the "blog" page (index.php). I'm building the site based on Bootstrap 3.
Therefore the loop has to create the columns and rows with PHP.
I'd like to have it set up in rows, so that potential height differences are being reset every row. The bootstrap grid would look like this:
<div class="row">
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
</div>
<div class="row">
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
</div>
<div class="row">
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
</div>
Lacking the PHP skills for setting up the loop properly, I tried hacking my way around, coming up with 3 times this (modifying the offsets):
<?php query_posts('posts_per_page=1&offset=0'); while (have_posts()) : the_post(); ?>
<div class="row">
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php endwhile; ?>
<?php query_posts('posts_per_page=1&offset=1'); while (have_posts()) : the_post(); ?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php endwhile; ?>
<?php query_posts('posts_per_page=1&offset=2'); while (have_posts()) : the_post(); ?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
</div>
<?php endwhile; ?>
It has obvious disadvantages:
a lot of unnecessary PHP requests/loops
filtering by categories, tags, etc doesn't work
Could you help me out with creating the PHP loop?
The most related question I found is this, but the column layout is somehow skewed!
Thanks a lot! Philipp
The easiest would be to use one container and put all the contetn items in it, then equal their height via js like that.
PHP
<?php query_posts('posts_per_page=9');while (have_posts()) : the_post();?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php endwhile?>
JS:
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.each(function() { $(this).height(tallest); });
}
$(document).ready(function() {
equalHeight($(".thumb"));
});
If thats no option, you could do sth. like that:
PHP
<div class="row">
<?php
$count=0;
query_posts('posts_per_page=9');
while (have_posts()) : the_post();
?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php
$count++;
if($count == 3 || $count == 6 ) echo '</div><div class="row">';
endwhile;
?>
</div>
Every three post objects must be contained within a row. So it will be like <div class="row"> <!-- post - post - post -> </div> <div class="row"> <!-- post - post - post -> </div>
If you would like to do this in php, and still maintain proper 'rowage' your code could look something like this:`
<div class="container">
<?php
$countturtle = 0 ;
$countbang = 0 ;
$count_posts = wp_count_posts( 'portobello' )->publish;
$args = array( 'post_type' => 'portobello', 'posts_per_page' => 32 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $countbang++ ?>
<?php if ( $countbang >= 2 ) {
$countturtle = $countturtle + 1 ; } ?>
<?php if ( $countbang == 1 ) {
echo '<div class="row first-training">'; } elseif ( ( $countturtle % 3 ) == 0 ) {
echo '<div class="row">'; } ; ?>
<div id="post-<?php the_ID(); ?>" class="training-block <?php echo $countbang; ?>-block-training col-sm-4" >
<header class="entry-header training-header">
<h1 class="entry-title train">
<?php the_title(); ?>
</h1>
</header><!-- .entry-header -->
<div class="entry-imogin">
ddd
</div><!-- .entry-imogin -->
</div><!-- #post -->
<?php if ( $countbang % 3 == 0 ) {
echo '</div>'; }
elseif ( $countposts == $countbang ) { echo '</div>';} ; ?>
<?php endwhile; ?>
</div>
Here a solution for 3 columns
layout :
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
...
<div class="row">
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'news',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
</div>
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'news',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 2): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
</div>
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'news',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 3): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
</div>
</div>