I'm using a WP_Query in a WordPress loop and for some reason I can not get the "else" portion to work. Basically I'm looking for posts in the category of 59. If there are none I want to display some text. The loop works fine when any posts in the category are present, but if there are none, nothing shows up. Can't seem to figure out why this isn't working. Here's my code. Any help is much appreciated!
<?php
//The Query
$custom_posts = new WP_Query();
$custom_posts->query('cat=59');
//The Loop
if ( have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
<article>
<div class="thenews">
<h1><?php the_title(); ?></h1>
<h2>Posted on: <?php the_time('F jS, Y') ?></h2>
<?php the_excerpt(); ?>
</div><!-- thenews div ENDS here -->
<div class="clearfloats"><!-- clears the elements above --></div>
</article>
<?php endwhile; else: ?>
<article>
<div class="thenews">
<p>Nothing here to see.</p>
</div><!-- thenews div ENDS here -->
<div class="clearfloats"><!-- clears the elements above --></div>
</article>
<? endif;
//Reset Query
wp_reset_query();
?>
You're not using the have_posts method of your custom loop which is why else isn't firing.
Change:
//The Loop
if ( have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
To:
//The Loop
if ( $custom_posts->have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
Related
I am following along with this tutorial and what I have made so far is not working as it should. I have gotten to the point where I am starting to use the loop - this is my code:
index.php
<?php get_header(); ?>
<div class="row">
<div class="col-sm-8 blog-main">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;
?>
</div><!-- /.blog-main -->
<?php get_sidebar(); ?>
</div><!-- /.row -->
<?php get_footer(); ?>
</div><!-- /.container -->`
content.php
<div class="blog-post">
<h2 class="blog-post-title"><?php the_title(); ?></h2>
<p class="blog-post-meta"><?php the_date(); ?><?php the_author(); ?></p>
<?php the_content(); ?>
</div><!-- /.blog-post -->
I have two posts but only the latest post is getting displayed (by the loop) and within the post that gets displayed - the only part that is displayed is the title. So it seems like the_date(), the_author(), and the_content() functions are not working for some reason, and the loop is only getting one of the posts (the latest one).
What is wrong? Thanks.
I'm not sure what I did, but I went to WP admin and clicked "customize" for the theme, and it loaded correctly in the theme editor - and then I went to the site, and it was working. I think maybe I didn't save a file, and I didn't realize it - and as I was trying to figure it out, I ended up saving the file, and it started working.
I'm really struggling at what I am sure is a simple problem. I cannot seem to get the posts which fall under a certain tag to display on that tags page ie: (/tag/blog/) with blog being the Tag.
So far following the Official Tag Templates page on Wordpress I still cannot seem to get it working.
I do not need a heirarchy so tag.php works fine. Using single_tag_title() it does correctly display the tag at the top of the page.
The rest of the Official Tag Templates does not really give much more detail on wether or not I can use the default Loop or a custom one. I have tried with the custom one as shown below but that does not work. (I've kept it down to the minimum not currently worried about styling.)
<?php get_header(); ?>
<p>Tag: <?php single_tag_title(); ?></p>
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_title();
endwhile; // end while
endif; // end if
?>
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
So this code currently does display the title of the Tag but not the title of the posts which I am trying to get out.
To wrap the question up. "How do I display the posts which fall under a certain tag."
You're not telling the loop to look for that particular tag anywhere...all you're doing is displaying the current selected tag at the top of the page. Add a PHP if statement into your loop to grab the posts with that tag:
<?php get_header(); ?> // Get the header
<p>Tag: <?php single_tag_title(); ?></p> // Display the tag name
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> // Start your loop
<?php if(tag_slug( ' THE SLUG ' ) ) : ?> // Look for the tag
// Post HTML stuff
<?php endif; ?>
<?php endwhile; endif ?> // finish the loop
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
This page from the Codex has examples of how to grab categories and tags.
You just need the the_post() function, you have to call it inside the while, and it should be the first function you call, as it loads the template tags.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
the_title();
endwhile; // end while
endif; // end if
?>
if the tags is just used in custom post types you should add something like this to you functions.php:
add_action( 'pre_get_posts', 'custom_function' );
function custom_function($query){
if(! is_admin()){
if(is_tag()){
$query->set('post_type', array('your_post_type', 'your_other_post_type', 'post'));
}
}
}
I found that I had to use a WP Query loop to bring them in due to being custom post types.
<?php if ( is_tag() ) {$term_id = get_query_var('tag_id'); $taxonomy = 'post_tag'; $args ='include=' . $term_id; $terms = get_terms( $taxonomy, $args );} ?>
<!-- This gets the tags slug -->
<?php $query = new WP_Query(array( "post_type" => array('blog', 'portfolio'), "tag" => $terms[0]->slug ) ); while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
I've got a next post/prev post link outside of the loop (it's in the footer.php) which won't accept the command to only show posts in the same category as the current post you are viewing.
The code is:
<section class="navigation">
<?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="left">
<?php previous_post_link('%link', 'Prev', TRUE); ?>
</div><!-- left -->
<div class="right">
<?php next_post_link('%link', 'Next', TRUE); ?>
</div><!-- right -->
<?php endwhile; endif; ?>
</section><!-- navigation -->
Presumably its because the loop above is self contained and doesn't know what the current category of the single.php is?
How would I get the code above to show posts in the same Category only, outside of the main loop?
Yes, you need the know what the current category of the current post, for that write this code :
//Get the active post's category
$category = get_the_category();
$cat = $category[0]->cat_ID;
//Run Loop on Posts of Specific Category
<?php query_posts('cat='.$cat.''); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="left">
<?php previous_post_link('%link', 'Prev', TRUE); ?>
</div><!-- left -->
<div class="right">
<?php next_post_link('%link', 'Next', TRUE); ?>
</div><!-- right -->
<?php endwhile; endif; ?>
I'm trying to figure out how to display a custom loop with only certain elements in a wordpress loop.
My loop currently contains the title, creation date, author & content.
However I am trying to remove the content for each post in the loop as I do not want it displayed in this list with no luck. I have even removed the_content() out of the loop & it still displays a listing of the post content under the looped section.
Any help would be greatly appreciated,
here is my code:
<?php query_posts('category_name=halloween &posts_per_page=6'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- IF articles then display them -->
<h6><?php the_title(); ?></h6>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<?php endwhile; else: ?>
<!-- IF no articles were created then show -->
NO Posts Present
<?php endif; ?>
I seemed to have found what i was missing in my code as there needed to be the following snippet of code required at the end of my loop which fixed the issue:
here is the code which I added after my endif
<?php wp_reset_query(); ?>
I've been trying to make a single sticky post display on the second (or third etc) position instead of the default which is the first.
I have been playing with the loop for hours but I always run into problems.
Here is what I have done so far:
<!-- Wordpress Main Loop Start -->
<?php query_posts(array("post__not_in" =>get_option("sticky_posts"))); ?>
<?php if ( have_posts() ) : $loop_count = 0; while ( have_posts() ) : the_post(); $loop_count++; ?>
<div <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<div class="byline postinfo">by <?php the_author_posts_link(); ?></div>
<div class="postdate postinfo"><?php the_time('l F d, Y'); ?></div>
<div class="commentscounter postinfo"><?php comments_number('0', '1', '%'); ?></div>
<?php
if ($loop_count == 2) :
$sticky = get_option( 'sticky_posts' );
query_posts( 'p=' . $sticky[0] );
wp_reset_query();
else:
the_excerpt('Read More...');
endif;
?>
</div>
<?php endwhile; else: ?>
<p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>
<div class="navi">
<div class="right">
<?php previous_posts_link('Previous'); ?> / <?php next_posts_link('Next'); ?>
</div>
</div>
<!-- Wordpress Main Loop End -->
What I have done is this:
I used query_posts() to filter out all sticky posts from the loop.
Used the loop to get the posts and a counter which increments everytime a post is fetched.
On the div which displays the fetched post, I added an if statement which checks if the counter is a specific number and if it is, it rewrites the query_posts and gets the sticky one. If not, it displays the fetched post.
I then reset the query and the loop continues as it should (almost).
The problem is, after the count reaches the value I want, the loop instead of continuing from the last post, it start from the beginning.
If I remove the wp_reset_query() function, the loop just stops when the sticky post is found.
If I try to rewrite the query to include all posts, I get infinite looping because I'm already inside the loop.
I don't want to break the loop in two or three so if anyone can help fix this using one loop, that would be great!