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; ?>
Related
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...
This is what my code looks like:
<?php $loop = new WP_Query( array( 'post_type' => 'home_templates_list', 'posts_per_page' => 20, 'orderby' => 'ID', 'order' => 'DESC' ) ); ?>
<?php $isFirstTemplate = true; ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item<?php if($isFirstTemplate) { echo ' active'; $isFirstTemplate = false; } ?>">
<?php $templateLink = get_the_excerpt(); ?>
<div class="col-sm-6"><img class="img-responsive" src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" /></div>
<?php if ($loop->current_post + 1 < $loop->post_count) { ?>
<?php $loop->next_post(); ?>
<?php $templateLink = get_the_excerpt(); ?>
<div class="col-sm-6"><img class="img-responsive" src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" /></div>
<?php } ?>
</div>
<?php endwhile; ?>
I am trying to make the main loop to be incremented by two. ie, two posts per iteration. Tried using the next_post() method but it appears to be that the get_the_excerpt() and $post->ID are still pointing to the first post!
Is there a better way for this? Thank you
EDIT
What I want to do is, two items(posts) per loop. Say for example, a simple while loop example of what am trying to achieve would look like this:
while(i < 10)
{
echo a[i];
echo a[i+1];
i=i+2;
}
Like this, I want to print two posts per iteration.
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.
I'm building a simple sidebar widget to display a loop of a custom post type "shows".
Each "show" has about 3 custom fields, which i'd like to output via the loop as well. This is the code i'm using:
This is the loop within my plugin code:
<?php
// WIDGET CODE GOES HERE
$args = array( 'post_type' => 'shows', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$month = get_post_meta($post->ID,'month-abbreviation',true);
$date = get_post_meta($post->ID,'date',true);
$citystate = get_post_meta($post->ID,'city-state',true); ?>
<article class="sidebar-show clearfix">
<a class="show-link" href="<?php the_permalink(); ?>">
<div class="date-box">
<span class="month"><?php echo $month; ?></span>
<span class="date"><?php echo $date; ?></span>
</div>
<div class="venue-box">
<?php echo "<h4>".get_the_title()."</h4>"; ?>
<?php echo "<p>".$citystate."</p>"; ?>
</div>
</a>
</article>
<?php endwhile;
wp_reset_query();
wp_reset_postdata();
?>
<?php
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("ShowsSidebarWidget");') );?>
This code pulls in the post titles, but doesn't display the custom fields month-abbreviation, date, and city-state.
What's missing here?
EDIT: Removed double quotes after avexdesign's reply.
are you sure $post->ID is available in this context?
maybe you should try: get_the_ID() instead.
Ok, I see a few things you can try.
month-abbreviation - should be single quotes. Like 'month-abbreviation'
Try taking out: $month= $date= and $citystate =
Might need echo
So Try:
echo get_post_meta($post->ID,'month-abbreviation', true);
echo get_post_meta($post->ID,'date', true);
echo get_post_meta($post->ID,'city-state', true);
Try something like:
<?php $loop = new WP_Query( array( 'post_type' => 'shows', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<span class="month"><?php echo get_post_meta($post->ID, 'month-abbreviation', true); ?></span>
<span class="date"><?php echo get_post_meta($post->ID, 'date', true); ?></span>
and so on... for your other custom IDs
I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.
Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.
Try this code for custom loop with pagination:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source:
WordPress custom loop with pagination