I've tried to separate search results by categories in 2 tabs. Everything works fine except showing the message "No results". This message is shown only when in both categories are nothing found. But when one tab has results and another hasn't - nothing is shown.
I'm looking for way to show "No results" for every tab. I mean, if nothing is found in cat 1 and some results found in cat 2 -> Show "No results" in Tab 1 and show results in Tab 2.
Any suggestions?
Code here:
<div id="tab-content1" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 3,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--text">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><?php the_excerpt(); ?> </span>
<span> </span>
</span>
</div>
<div class="post-item--thumbnail">
<div class="post-thumbnail">
<div class="backgroundImage_1hK9M post-thumbnail--image" style="background-image: url('<?php echo $url; ?>');"></div>
<span></span>
<span></span>
</div>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content1 -->
<div id="tab-content2" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 4,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--circle">
<?php the_field('digest_number'); ?>
</div>
<div class="post-item--text no-margin">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><p><?php the_field('short_description'); ?></p> </span>
<span> </span>
</span>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content2 -->
</div>
You can use your if (have_posts()) : on each loop to determine if to display or not...
if (have_posts()) :
//while loop
else:
echo '<h1>no posts found</h1>';
endif;
Related
Struggle to get all the records mapped by custom taxonomies.
I have registered a custom post type i.e. job_post and custom taxonomy i.e. countries. I wanted to show all job posts (having job_title, job_location, and permalink to specific job post) which are filtered by Countries (i.e. India & USA). I had Mapped almost 10 job posts under the India category and 18 job posts under the USA category.
I have displayed the records through toggle tabs (i.e. India and USA). I can fetch only one record by each category through the below code:
<section class="container container_filter">
<div class="filters filter-button-group">
<ul class="nav nav-tabs">
<?php
$terms= get_terms('countries');
foreach($terms as $cat_term)
{ ?>
<li><a data-toggle="tab" href="#<?php echo $cat_term->slug;?>"><?php echo $cat_term->name; ?></a></li>
<?php } ?>
</ul>
<div class="tab-content">
<?php
global $post;
$args= array('post_type'=> 'job_post', 'post_per_page' => -1);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
while ($the_query->have_posts()) : $the_query->the_post();?>
<?php
$termsArray= get_the_terms(get_the_ID(), 'countries');
$term_id=$termsArray->id;
foreach ($termsArray as $term){
$termsSlug = $term->slug;
}
?>
<div id="<?php echo $termsSlug;?>" class="tab-pane fade">
<div class='col-md-6'>
<a class='job-card readmoreCustom' href='<?php the_permalink($term_id); ?>'>
<div class='content'>
<h3 class='title'><?php echo get_the_title($term_id) ?></h3>
<p class='job_location'><?php the_field('job_location'); ?></p>
</div>
<span class='more'>KNOW MORE<span class="readIcon"> > </span></span>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
<?php else: ?>
<?php _e('Sorry, no jobs matches your criteria'); ?>
<?php endif; ?>
</div>
</div>
</section>
How can I fetch all the records from both categories under specific tabs? Help is appreciated.
<section class="container container_filter">
<div class="filters filter-button-group">
<ul class="nav nav-tabs">
<?php
$terms= get_terms('countries');
foreach($terms as $cat_term)
{ ?>
<li><a data-toggle="tab" href="#<?php echo $cat_term->slug;?>"><?php echo $cat_term->name; ?></a></li>
<?php } ?>
</ul>
<div class="tab-content">
<?php
global $post;
$args= array('post_type'=> 'job_post', 'post_per_page' => -1);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
while ($the_query->have_posts()) : $the_query->the_post();?>
<?php
$termsArray= get_the_terms(get_the_ID(), 'countries');
$term_id=$termsArray[0]->term_id;
foreach ($termsArray as $term){
$termsSlug = $term->slug;
}
?>
<div id="<?php echo $termsSlug;?>" class="tab-pane fade">
<div class='col-md-6'>
<a class='job-card readmoreCustom' href='<?php the_permalink(); ?>'>
<div class='content'>
<h3 class='title'><?php echo get_the_title(); ?></h3>
<p class='job_location'><?php the_field('job_location'); ?></p>
</div>
<span class='more'>KNOW MORE<span class="readIcon"> > </span></span>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
<?php else: ?>
<?php _e('Sorry, no jobs matches your criteria'); ?>
<?php endif; ?>
</div>
</div>
</section>
The below loop is fine if the post is in only 1 country but if it's in multiple counties the loop should be closed after div
foreach ($termsArray as $term){
$termsSlug = $term->slug;
}
On my project I am displaying posts by category.
I have a "summary-section" and a "detail-section". The "detail-section" opens with a click on an "more-less-button" with a javascript-function.
The goal I don't achieve is, that i only want to display the button, if there are more than 6 blog-posts in the category.
Can anybody help me in programming an if/else statement or is there a much easier way?
<div class="slide">
<div class="summary">
<?php $catquery = new WP_Query( 'cat=5&posts_per_page=6' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<article>
<li><?php the_title(); ?></li>
</article>
<?php endwhile;
wp_reset_postdata();
?>
</ul>
</div>
<div class="details">
<?php $catquery = new WP_Query( 'cat=5&posts_per_page=6&offset=6' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<article>
<li><?php the_title(); ?></li>
</article>
<?php endwhile;
wp_reset_postdata();
?>
</ul>
</div><!-- end .details -->
<a class="more-less-button-d" href="#" title="mehr Referenzen zur Ingenieurgeologie">mehr
<span class='ti-arrow-down'></span>
</a>
</div>
You would have to query to get the number of posts within that category first. Assuming post type is post.
<?php
$args = array(
'cat' => 5,
'post_type' => 'post'
);
$query = new WP_Query($args);
$totalPost = $query->found_posts;
?>
This would get you the total number of posts within that category.
So then you could do this
<?php if ($totalPost > 6) : ?>
<a class="more-less-button-d" href="#" title="mehr Referenzen zur Ingenieurgeologie">mehr<span class='ti-arrow-down'></span></a>
<?php endif; ?>
in Wordpress, I am trying to display my most recent posts in a list. I have being able to list the links, but cant do the same with post thumbnail, title, date and category. What am I doing wrong?
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo the_date();?></p>
<h3 class="article_title"><?php echo get_the_title( $post_id ); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Another way to do it, using WP_Query
<?php
// args query
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat-a, cat-b'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php echo the_category(); ?></p>
<h3 class="article_title"><?php echo get_the_title(); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
// reset query
wp_reset_postdata();
?>
Try below code its working for me.
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo get_the_category( $recent->ID );?></p>
<h3 class="article_title"><?php echo get_the_title( $recent->ID); ?></h3>
<p class="date"><?php echo get_the_date( $recent->ID );?></p>
</div>
<div class="mask">
<img src="<?php echo get_the_post_thumbnail_url($recent->ID,'full'); ?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>
I have a Wordpress Site or its Blog section I did a WP_Query post.
The loop is within a custom template its showing only one record while I have few post entries.
I think I have done correctly...
Please do let me why it is not looping the records.
<?php get_header();
?>
<div id="body">
<div class="container">
<div class="row-fluid">
<div class="span9">
<?php
$arr = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$query = new WP_Query($arr);
if(count($query->posts) > 0):
$i = 0;
?>
<?php while($query->have_posts()): $i++;?>
<?php $query->the_post();
if(has_post_thumbnail(get_the_ID())){
$url_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
$url_thumbnail_news = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
} else {
$url_thumbnail = '<image src="'.get_template_directory_uri().'/images/no-thumbnail.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
$url_thumbnail_news = '<image src="'.get_template_directory_uri().'/images/no-thumbnail-news.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
}
?>
<?php if($i==1):?>
<div class="drop-shadow lifted"> <a class="nile-thumnail" href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>"><?php echo $url_thumbnail;?></a> </div>
<div class="the_post">
<h2 class="bj-title"><a href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>">
<?php the_title();?>
</a></h2>
<div class="bj-date">
<?php the_date();?>
<?php the_time();?>
</div>
<div class="bj-des">
<?php the_excerpt();?>
</div>
<a href="<?php the_permalink();?>" title="<?php the_title()?>" class="read-more">
<?php _e('Read more', APP_TD);?>
</a> </div>
<?php endif;?>
<?php endwhile;?>
<?php endif;?>
<?php // Reset Query
wp_reset_query(); ?>
<div class="navigation">
<div class="alignleft">
<?php previous_posts_link('« Previous') ?>
</div>
<div class="alignright">
<?php next_posts_link('More »') ?>
</div>
</div>
</div>
<div class="span3"> <?php echo get_default_sidebar();?> </div>
</div>
</div>
</div>
<?php get_footer();?>
Try removing the 'if($i==1)' and corresponding 'endif;' line
I am displaying previews of the three most recent news articles on my homepage. The most recent post will be displayed in a different format to the second and third most recent posts.
I am currently displaying all three the same with the following code
<?php query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
endwhile;
endif;
?>
How can I add another loop which will separate the second and third most recent posts from the most recent post?
I did not want to use postID as the posts will change.
The two arguments in the WP_Query function below combine to retrieve the second most recent post, then the HTML below displays that post.
<div class="video-message">
<p>
<ul>
<!-- // Define our WP Query Parameters -->
<?php
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 1 ) );
?>
<!-- // Start our WP Query -->
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<!-- // Display the Post Title with Hyperlink -->
<p><?php the_title(); ?></p>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
</p>
</div>
</div>
To display the third most recent post would require changing the offset value to 2, so that the program skips over the two most recent posts.
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 2 ) );
This method is discussed in the Pagination Parameters section of the WordPress Code Reference.
Untested but you could try:
<?php
$count = 0;
query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post();
if($count == 0)
{
?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
$count = 1;
}
else
{
//Some other layout here
}
endwhile;
endif;
?>
The above will check if $count is 0 and if it is, then do the layout and the count will then equal to 1. So the next time around $count won't be 0, so it will run what is in the else (which will be your layout).
I use WP_Query like this to show all posts from the fourth most recent one:
<?php
$query4 = new WP_Query( 'posts_per_page=4&offset=3' );
?>