WordPress Loop Display Content if Have Posts - wordpress

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I'm using the loop to pull a few custom posts to display on the site. I have a designated <div> to hold the posts. I've run into the problem when there are no posts to be pulled, the div box still displays with no content in it. How would I insert the code of my div container within the "if" statement so that the div is only created if there are posts?

<?php if (have_posts()) : ?>
<div>
<?php while (have_posts()) : the_post(); ?>
…
<?php endwhile; ?>
</div>
<?php endif; ?>
You may find the documentation on PHP's alternative syntax useful

Try the following code snippet:
<?php if (have_posts()) { ?>
<div>
<?php } ?>
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; ?>
<?php if (have_posts()) { ?>
</div>
<?php } ?>
<?php endif; ?>
I hope this helps!

Create a 404.php for your theme and drop the if (have_posts()) completely. You can start the loop with the while statement, and WordPress will use the 404.php if there are no posts.

Related

How to show extra content with a slug?

This is a single.php and for example url is
http://url.com/sample-post/
I added some extra content with Advanced Custom Fields plugin and show it in the content. Everything is all right here but I want to show this extra content like http://url.com/sample-post/extra-content/
Is it possible? If yes how can I make it? I've been searching for hours but I have not found anything.
I hope you understand me and so sorry for my bad english. Thank you everyone who will help.
<?php get_header(); ?>
<main class="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php the_field('some-extra-content'); ?> // Extra Content
<?php endwhile; ?>
</main>
<?php get_footer(); ?>
please try below code for ACF
<?php get_header(); ?>
<main class="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php echo get_field('some-extra-content', $post->ID); ?>
<?php endwhile; ?>
</main>
<?php get_footer(); ?>
Not exactly the same url but you could use a query string.
http://url.com/sample-post?extra=true
<?php
if($_GET['extra'] === 'true'){
the_field('some-extra-content');
}
?>

have_post() does return null value in wp

I used the following code in my page template:
<?php
while(have_posts()):the_post();
the_content();
?>
But nothing is displayed. The loop is not working. I'm sure that, there is sufficient information as content in my template page.
You should use if condition to check if post exists else skip the loop. Make sure to ON the error log and check the exact error.
<?php wp_reset_query(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php
/**
* Template Name: My Template
*/
the_post();
the_content();
?>
If you save the following code as a page template and call the page template into a page by choosing it, then the page will show the page content without any hassle.
Note: it's THE minimal bit of code.
use the following code
Try using wp_reset_query()
<?php
wp_reset_query();
while(have_posts()):the_post();
the_content();
endwhile;
?>

Custom Post type templates not working in wp

I have some categories like about-us, services etc and want to show different templates for these categories. I have read from wp site to make files name like category-slug.php where slug may be about-us or services. I made these files but they didn't worked for me. Instead of these templates the index file displays the posts. I want to display posts from these categories in custom files.
Can any one tell me how to do this?
Are you looking for something like this?
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>

Why the_content doesn't work in wordpress single.php

My code:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="posts" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2>
<?php the_title(); ?>
</h2>
<div class="entry">
<?php the_content();?>
</div>
</div><!--post end-->
<?php endwhile; ?>
<?php else : ?>
<h3>no content</h3>
<?php endif; ?>
I put the code into my customed wordpress theme file single.php. Why it can't output the post content, it can output the post title. thank you.
You can try the following to see if it works instead of the_content
<?php echo wpautop($post->post_content); ?> // content with p tags
<?php echo $post->post_content; ?> //without p tags
Also an option
<?php echo wpautop( get_the_content() ); ?> // content with p tags
see if that works for you.
When developing Wordpress themes it's advisable for you to switch the debug mode (found on your installation's root in wp-config.php) to true. This will alert you if you have any errors.
In your case, try out the <?php the_excerpt(); ?>.
Also, this may sound a bit dumb, but do you actually have posts? Not pages or rather content in that post?
Many a times i have come across queries from developers or first time theme developers about not able to show the_content() on their custom template page.
The issue is that the_content() function runs inside the WP loop so for example
if (have_posts()) {
while (have_posts()) {
the_post();
the_content();
}
} ?>
This will work, but in some cases you want to call the function outside the loop – the solution for that is:
echo $post->post_content;

add a page to sidebar

So I have a page called "latest news" and using a custom template t_latest_news.php
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="latest-news" id="post-<?php the_ID(); ?>">
<?php the_content(); ?>
<?php edit_post_link('Edit this page','<p class="edit-link">','</p>'); ?>
</div><!-- /.latest-news -->
<?php endwhile; endif; ?>
I've created a page item and put some content into that page. Now, I want to show the content on the sidebar. How can I do that please?
I've tried something like:
<?php include(get_query_template('t_latest_news.php')); ?>
<?php include(TEMPLATEPATH . 't_latest_news.php'); ?>
<?php get_query_template('t_latest_news.php') ?>
<?php get_template_part( 't_latest_news.php' ); ?>
But none of them works. HELP!
<?php query_posts('page_id=76'); ?>
<?php while (have_posts()) { the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php } ?>
<?php wp_reset_query(); ?>
It works with "page_id" but not pagename. any idea?
To query a specific page by name you do this:
<?php
query_posts('pagename=about'); //retrieves the about page only
?>
You should remove the .php at the end so that it reads t_latest_news
I was just showing this as an example, please be advised:
The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should use get_posts() instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.
see: http://codex.wordpress.org/Template_Tags/get_posts for more information

Resources