WP if condition showposts - wordpress

I have this code which works okay for what I wanted but I will probably need to put a condition if there's only 2 posts then wrap it in a <div class="large-6"> then if there's 3 posts then wrap it in large-4.
Just a little confused how to add a condition statement.
<?php
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'showposts' => '3', 'offset' => '1' ) );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="large-4 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
To Pieter:
something like this? if so, that didn't work though unless I'm doing it totally wrong and noobishly.
<?php
$query = new WP_Query(array(
'posts_per_page' => '3',
'post_type' => 'portfolio',
'offset' => '1'
));
while ($query->have_posts()): $query->the_post(); ?>
<?php if(!isset ($query->posts[2])){ ?>
<div class="large-6 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php } else {?>
<div class="large-4 columns"> </div>
<?php }?>
<?php endwhile; ?>

This is just a theory but should work. Here is the idea:
$loop->posts holds an array with all the posts with all their resepective postdata. So, with that in mind, it should be easy to determine whether you have one, two or three posts in that array by simply checking if a certain array key exists
So you basically needs to check if you have a third post, so you can try something like this
if( !isset( $loop->posts[2] ) ) {
//add div if less than 3 posts
}else{
//add div if you have 3 posts
}
EDIT
Just a note. The OP has changed the query variable from $loop to $query in his original code. $loop should be changed accordingly

You can put conditionals like
<?php if(condition) : ?>
code to execute if condition == true
<?php elseif(condition2) :?> // repeat as many times as necessary
...
<?php else :?>
final else if needed, you can just end the if statement if it's only one condition, if you have else if else this is needed
<?php endif; ?>
That's basically it...

Related

WP Query - if/else using custom field

I have a WP query that is looping all my projects with the same code. It's working great, but now I need to change the code based on a custom taxonomy. I basically want to make an if/else loop where if the custom taxonomy contains a certain value, to have x code, and if the custom taxonomy does not contain that certain value, to have y code. Is there a way to add this if/else statement to the "if ( $the_query->have_posts() ) :" section? Or do I need to lay this out differently?
Thank you!
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'projects',
'posts_per_page' => 24,
'paged' => $paged,
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post();
?>
EDIT: FluffyKitten (great name) asked for more info so here it is.
My custom taxonomy is called "project_services" and I want all projects with the service "commercial-damages" to loop through with one code, and all other projects to loop through with another code. I tried my crack at it below, but know that my part in the if loop is extremely wrong. I'm really not sure how to add that if condition in there since it's a WP query.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'projects',
'posts_per_page' => 24,
'paged' => $paged,
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post();
if (get_field('project_services') == 'commercial-damages'):
?>
<div class="projects-column">
<div class="project-image">
<?php $image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" class="project-image-tester" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<div class="project-info">
<span class="proj-name" style="font-weight: bold;"><b><?php the_title(); ?></b></span>
</div>
<div class="project-onhover">
<span class="proj-title"><p><b><?php the_title(); ?></b></p></span>
<span class="proj-details">
<p><span style="font-weight: bold;">LOCATION: </span><?php the_field('location'); ?></p>
<p><span style="font-weight: bold;">CLAIM VALUE: </span><?php the_field('project_value'); ?></p>
<p><span style="font-weight: bold;">SERVICES: </span> <?php echo wp_strip_all_tags( get_the_term_list( $post->ID, 'project_services', '', ', ', '' ) ); ?> </p></span>
</div><!--projects-on-hover-tester -->
</div><!--project-image-->
</div><!--projects-column -->
<?php else: ?>
<div class="projects-column">
<div class="project-image">
<?php $image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" class="project-image-tester" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<div class="project-info">
<span class="proj-name" style="font-weight: bold;"><b><?php the_title(); ?></b></span>
</div>
<div class="project-onhover">
<span class="proj-title"><p><b><?php the_title(); ?></b></p></span>
<span class="proj-details">
<p><span style="font-weight: bold;">LOCATION: </span><?php the_field('location'); ?></p>
<p><span style="font-weight: bold;">PROJECT VALUE: </span><?php the_field('project_value'); ?></p>
<p><span style="font-weight: bold;">SERVICES: </span> <?php echo wp_strip_all_tags( get_the_term_list( $post->ID, 'project_services', '', ', ', '' ) ); ?> </p></span>
</div><!--projects-on-hover-tester -->
</div><!--project-image-->
</div><!--projects-column -->
<?PHP endif; ?>
<?php
endwhile;
endif;
?>
<?php wp_reset_postdata(); ?>
You just want to change the way you are processing the post details in your loop, so you don't need to make any changes to the loop itself. What you want to do is check the custom field inside the loop and then decide what to do with it.
Also FYI, you are using the_field - this will display the result to the screen immediately. Instead, to use the value without automatically displaying it, use get_field
Take a look at this - there are additional comments to help explain what it happening:
<?php
$the_query = new WP_Query($args);
// Standard while loop to get each of the returned posts one by one -
// we don't care what the post type is here, we just load it into the global "post" so we can use it...
if( $the_query->have_posts(); while ($the_query->have_posts() ) : $the_query-
>the_post();
// Now the details of that post are loaded into the global "post",
// so we can check the values in that post
// Check if the current post in out loop has commercial-damages
if (get_field('project_services') === 'commercial-damages'):
?>
// do whatever you want with the commercial-damages posts...
<?php else: ?>
// do whatever you want with the other posts
<?php endif; ?>
<?php
endwhile;
endif;
?>
Thank you to FluffyKitten who helped me work through this. I wasn't able to get an if loop to work exactly how outlined in the question, but I tried a few other approaches and just got one to work. I created an if / else statement in the HTML that I am looping. There I put if this field is empty, use another which I created and labeled differently to solve my problem. Below is the code:
<?php if( get_field('project_value') ): ?><p><span style="font-weight: bold;">PROJECT VALUE: </span><?php the_field('project_value'); ?></p>
<?php else: ?>
<p><span style="font-weight: bold;">CLAIMS VALUE: </span><?php the_field('claims_value'); ?></p><?php endif; ?>

Wordpress page shows content from the previous loop (wp_reset_postdata not working?)

I am rather new to Wordpress, and I can't really get what is going wrong in my example.
In my template I have page-standard.php which has a Slider in the Header working by means of the following code:
<div class="slider">
<div class="slider__wrapper">
<?php
$posts = get_posts( array(
'numberposts' => -1,
'category_name' => 'slidertop',
'orderby' => 'date',
'order' => 'ASC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
<!-- ! Here goes SLIDE content -->
... <!-- ! End of SLIDE content -->
<?php
}
wp_reset_postdata();
?>
And it works fine. But then I need to show contents of the page and make another wp loop on the same page this way:
<?php
/*
Template Name: Standard Page
*/
get_header();
?>
<section class="blog-posts-section">
<div class="container">
<?php
while( have_posts() ) : the_post();
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
?>
</div>
</section>
<?php
get_footer();
?>
content.php looks as follows:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<div class="entry-content">
<?php
the_content();
?>
</div>
</article>
So it should be showing the contents of the page which has the Page Standard template.
But instead it shows the contents from the SLIDER which it got in the very first wp loop. It seems like wp_reset_postdata() is not working properly. I used the same approach before, but just now it started to show this kind of behaviour.
I've tried using wp_reset_query() as well, but no effect.
Just use proper methods provided by Wordpress to run an extra query instead of $posts = get_posts. The code should look like that:
<?php
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
and then your standard query. It's all described in details in here:
https://developer.wordpress.org/reference/classes/wp_query/

Wordpress highlight recent posts in front page

I'm trying to highlight the three most recent posts in the home page with bigger cover images and an excerpt so that if they're highlighted they don't appear on the smaller post list. Something like this, but with the most recent posts on top:
I've tried using multiple WP_Query with an offset but it doesn't seem to quite work (it's possible I'm not doing it right).
Any ideas? Thanks!
EDIT: Here's what I tried. The problem with this is that I get "undefined offset" errors.
<?php
/* Featured posts */
?>
<div class="featured-posts columns">
<?php
$featured_query = new WP_Query( array( 'posts_per_page' => 3) );
while ( $featured_query->have_posts() ) : the_post(); ?>
<div class="featured-post col-4">
<?php dge_post_thumbnail(); ?>
<div class="entry-meta">
<?php
dge_posted_on();
?>
</div><!-- .entry-meta -->
<h2>
<?php the_title(); ?>
</h2>
<p><?php the_excerpt(); ?></p>
<p>continue reading</p>
</div>
<?php endwhile; ?>
</div>
<?php
/* Start the Loop */
$query = new WP_Query( array( 'posts_per_page' => 5, 'offset' => 3 ) );
while ( $query->have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
/*get_template_part( 'template-parts/content', get_post_type() );*/
get_template_part( 'template-parts/preview-content');
endwhile;
?>
Try this script:
<?php
$featured_query = new WP_Query( array( 'posts_per_page' => 3,"orderby"=>"date","order"=>"DESC") );
while ( $featured_query->have_posts() ) : $featured_query->the_post();
echo '<p>'.the_title().'</p>';
endwhile;
?>
<?php
echo '<hr/><h1>After offset set</h1> <br/>';
$featured_query = new WP_Query( array( 'posts_per_page' => 5,"orderby"=>"date","order"=>"DESC","offset"=>3) );
while ( $featured_query->have_posts() ) : $featured_query->the_post();
echo '<p>'.the_title().'</p>';
endwhile;
?>
Response:

Wordpress, how to make an "if in category then" inside a loop?

How do I make this conditional work inside a loop? This seems so easy but it does not work.
The relevant part:
<?php if (is_category('Sup-FAQ')) : ?>
<p>If in cat sup-faq</p>
<?php else : ?>
<p>If not then print this</p>
<?php endif; ?>
The whole loop:
<?php
$cats = get_categories();
foreach ($cats as $cat) {
$args = array(
'post_type' => 'support_post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID,
),
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ): ?>
<h4><?php echo $cat->cat_name ; ?></h4>
<?php if (is_category('Sup-FAQ')) : ?>
<p>If in cat sup-faq</p>
<?php else : ?>
<p>If not then print this</p>
<?php endif; ?>
<ul>
<?php
while($query -> have_posts()) : $query -> the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php
wp_reset_query() ;
}
?>
The loop takes all posts from the custom post type 'support post' and lists them by category. Now I need a conditional inside that loop that will display some text if a category is met. Thanks in advance.
From the is_category() documentation: To test if a post is in a category use in_category().
On the other hand, in your case you've already got the category in your outer loop. Just check $cat->name:
<?php if ($cat->name == 'Sup-FAQ'): ?>

Displaying Custom Category loop in homepage

Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.

Resources