using an if statement inside a while with wordpress - php

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.

Related

Reset counter after each post loop

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; ?>

Display total number of items from while loop outside of it php

I have a PHP while loop and i'm trying to use a counter to display the total number of items (.question) that are within it. I'm trying to echo the contents of $child_i outside of the while loop to do this but the code below doesn't work. Whats the best way of achieving this?
EDIT
Updated to show full code
<?php if( have_rows('faqs') ): ?>
<ul class="responsive-accordion responsive-accordion-default bm-larger">
<?php while( have_rows('faqs') ): the_row(); ?>
<li>
<div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">
<!-- TRYING TO ECHO $CHILD_I ON THE LINE BELOW BUT NOT WORKING -->
<?php echo($child_i); ?> Questions</span></div>
<!-- $CHILD_I IS DEFINED ON THE NEXT LINE -->
<?php if( have_rows('questions') ): $child_i = 0; ?>
<!-- THIS ECHO WORKS -->
<div class="responsive-accordion-panel <?php echo($child_i); ?>">
<?php while( have_rows('questions') ): the_row(); ?>
<div class="question">
<h6><?php the_sub_field('question'); ?></h6>
<p><?php the_sub_field('answer'); ?></p>
</div>
<?php $child_i++; endwhile; ?>
</div>
<?php endif; //if( get_sub_field('questions') ): ?>
</div>
</li>
<?php endwhile; // while( has_sub_field('to-do_lists') ): ?>
</ul>
<?php endif; // if( get_field('to-do_lists') ): ?>
Try this
$questions = get_field('questions'); $size=count($questions); echo $size;

Sticky Post appears twice in homepage loop. Is there a way to make it appear only once?

So in WordPress, if you run the regular loop, Sticky Posts appear twice. Once at the beginning (as desired) and then once in the chronological order among regular posts. Is there a way to make a post sticky and not appear the second time?
Here's the code from the index.php file.
<?php get_header(); ?>
<div id="content">
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Default') ) : ?>
<?php endif; ?>
</div>
<div class="textContainer">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h1><?php the_title(); ?></h1>
<div class="featuredImage">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'main-thumb' ); } ?>
</div>
<div class="readMore">
<?php // the content of the post
the_content('Read More'); ?>
</div>
<div class="comments">
<?php _e('', 'surplus'); ?> <?php comments_popup_link('{0 Comments}', '{1 Comment}', '{% Comments}'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="nav3">
<?php posts_nav_link(); ?>
</div>
<?php get_footer(); ?>
Thank you!
Liz

My code causing infinite loop, can't see the bug

I am working on a WordPress project, using the Advanced Custom Fields plugin.
However I am getting a crazy infinite loop from my code, so I've obviously messed things up somewhere, however after a good two hours looking at it I can't see the problem.
<?php get_header(); ?>
<?php
// If the loop has posts
if (have_posts()) :
// For each post this page has
while (have_posts()) : the_post();
// Display the Flexible Content
while(has_sub_field('add_row')):
// If the content type is Tagline
if(get_row_layout() == 'tagline'): ?>
<div class="row paddingBottom paddingTop">
<div class="col-xs-12">
<h3 class="tagLine"><?php the_sub_field('tagline'); ?></h3>
</div>
</div>
<?php
// If the content type is a Featured Image
/*elseif ( get_row_layout() == 'featured_image' ): ?>
<div class="col-xs-12 textCenter">
<?php
$attachment_id = get_sub_field('featured_image');
$size = "full-width"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
</div>
<?php*/
// If the content type is a horizontal rule
elseif ( get_row_layout() == 'horizontal_rule' ) : ?>
<div class="row">
<div class="col-xs-12">
<hr />
</div>
</div>
<?php
// If the content type is Columns
elseif ( get_row_layout() == 'columns' ):
// If there is at least one column section
if(get_sub_field('column')):
// For each column section
while(has_sub_field('column')): ?>
<div class="row paddingTop paddingBottom">
<?php
if(get_sub_field('column_content_type')):
$count_rows = 0;
// Counting
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
// Loop
while(has_sub_field('column_content_type')): ?>
<div class="
<?php /*
if ( $count_rows == 1 ) :
echo "col-xs-12";
elseif ( $count_rows == 2 ) :
echo "col-xs-6";
elseif ( $count_rows == 3 ) :
echo "col-xs-6 col-sm-4";
elseif ( $count_rows == 4 ) :
echo "col-xs-6 col-sm-6 col-md-4";
endif; */
?>
"> <?php
// Title
if ( get_sub_field('title') ) : ?>
<h2 class="smallTitle"><?php the_sub_field('title');?></h2><?php
endif;
// Text Area
if ( get_sub_field('text') ) :
the_sub_field('text');
endif;
// Link
if ( get_sub_field('link') ) : ?>
<?php // eventually need to make it so you can change the link text ?>
<a class="textCenter" href="<?php the_sub_field('link'); ?>">
More info
</a> <?php
endif; ?>
</div> <?php
endwhile;
endif;
?>
</div>
<?php
endwhile; // end for each column section
endif; // end checking for at least one column section
endif; // end checking content type is columns
endwhile; // end flexible content
endwhile; // end for each post
endif; // end checking for at least one post
?>
<?php get_footer(); ?>
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
That's your problem... But it could be one of many based on what I'm seeing. It looks like you think while loops work like if statements... All you are doing in that loop is incrementing it over and over. It will never end.
I think the mistake is
// Display the Flexible Content
while(has_sub_field('add_row')):
i think you need to use if(){
// Code
}
instead of "while"

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.

Categories