Why i need a wp loop in single.php - wordpress

In WordPress theme development we can use single.php to show the specific single post.
For this purpose the common practice is:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content();?>
<?php endwhile; ?>
<?php endif; ?>
Why do I need looping to show a single post? Can any one give some valid reason?

this was something that killed me for years and I found the answer:
No, you don't need to use the LOOP in single pages, you just call the_post() and you have all data needed
.... BUT ....
If you don't use the loop (while(have_posts())....) a hook "loop_end" is not called and if a plugin/process has any action on this hook, it won't work. So for safety reasons you should use the loop.
Also, people ask do I need to check for existence before the loop: if(have_posts())?
<?
if( have_posts() ):
while( have_posts() ):
the_post();
.....
endwhile;
endif
?>
No, you don't need to check
.... BUT ....
Checking allows you to include headers/titles before the loop and not having them if the loop is empty.

The WordPress Loop instantiates some functions like the_title(), the_content() and others.
In other words, your post is loaded in that loop, and the loop is gone through once if you are on a single post. Although it might be strange to have a loop, it is actually quite useful.
WordPress uses a template hierarchy, which is a way of choosing which template to load for a given post/page. In your single.php, the loop will run once. But if you do not have a single.php file, WordPress will use index.php instead for that same post.
For the sake of consistency, having a loop which works for any number of posts is helpful. Else, in you index.php, you would have needed a case for one post and another case for multiple posts and keeping a consistent templating method would be difficult.

Related

Wordpress loop iterates over the last 15 posts only

I'm using a wordpress framework (WooFramework) that uses the following code to create a list of all posts of a specific category:
<?php while ( have_posts() ) { the_post(); ?>
<li><?php the_title(); ?></li>
<?php } ?>
I check the Wordpress documentation and this code seems right. However the list outputted contains only the 15 most recent posts. It seems like something is making the have_posts() stop earlier than it should. Any ideas on what may be causing this problem?
Tip: The index page shows (by default) the 15 most recent posts. Can that be related with the problem? Could it be possible that the framework redefined wordpress' have_posts() function?
Try going to Settings > Reading and changing the value of "Blog Pages Show At Most". If you want to show all your posts you can just set it to something really high like 9999999999.

Getting a list of all ID's on the site

Sounds inane, but I think it would be handy to have an id list of the name of every element on my site. By every element I mean Posts, Pages, Comments, Users, the works.
Id, Title
That's it.
I do not know how to loop through PHP code for this. My PHP skills are weak. Is this too ridiculous to answer? Maybe, but I'd still find it handy. I've looked through every Plug-in name and description that was close to fitting this task and found nothing.
Thanks,
Mike
The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post. When WordPress documentation states "This tag must be within The Loop", such as for specific Template Tag or plugins, the tag will be repeated for each post.
For example, among the information The Loop displays by default: the Title (the_title()), Time (the_time()), and Categories (the_category()) for each post. Other information about each post can be displayed with the appropriate Template Tags or (for advanced users) by accessing the $post variable, which is set with the current post's information while The Loop is running.
Simple Example:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
What you need to use is the_id() function. It displays the numeric ID of the current post. This tag must be within The Loop.
Put something like this in your loop
<p>Post Number: <?php the_ID(); ?></p>
References:
http://codex.wordpress.org/Function_Reference/the_ID
http://codex.wordpress.org/The_Loop

Adding a second loop to a Wordpress theme on a separate page

I'm trying to add two loops to a theme on two separate pages: home and blog.
Blog is basically an index of the posts. It's what most Wordpress pages default to as a home page. To accomplish this I went to "reading settings" and set "front page displays" as 'static' with "front page" set to a Home page I set up in Wordpress pages and "posts page" set to a Blog page.
Now the problem is that when I add the loop to the Home page, it doesn't work, presumably because I have posts page set to a different page.
So how do I get the loop to work on the Home page as well as the blog page? Btw, the Home page loop is just post title + date + maybe excerpts. Do I need to completely rework the theme or is this is just not a possibility under Wordpress?
Oh and the loop I'm using is:
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post() ?>
There are at least three wayst to run custom queries in WordPress.
Query_posts() can define the query string of your second loop. It is easy and very common to do. This code is a basic structure I copied from the codex page for query_posts():
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
You can also use get_posts() which is similar.
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Both functions accept a number of arguments that are explained on the query_posts function reference page. The arguments shown above are only examples. The list of available args is long.
A third method available to you is to instantiate another instance of the WordPress Query object (WP's main query method). Query_posts and get_posts both run a second call to the database after WordPress runs the default wp_query. If you are super concerned about performance or reducing db hits, I suggest learning how you can interact with wp_query to modify the default query before it is run. The wp_query class provides a number of simple methods for you to modify the query.
Good Luck!
It is possible that WordPress does not start a loop for you because you use a static page. But if this static page is defined in your theme (since you include the PHP code to display the loop, I assume it is), you can always start a new loop there yourself. Just call query_posts yourself, and your code should start working.

custom wordpress page

I'd like to implement a custom post retrieval page in wordpress. Basically, I'm using AJAX to call this page that will be passed a post ID and retrieve certain data from that post.
Note: please don't mistake this as a template question. I do not want a template for one single page -- I am looking to make this page query multiple different posts based on postID and return certain data from that post.
So I tried creating a page
<?php
$args=array(
'p'=>'77'
);
$friends = new WP_Query($args);
?>
<?php if ($friends->have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php else: ?>
<p>Sorry, no posts are available.</p>
<?php endif; ?>
But this does not work since it is not loading in the wp functions to handle the query.
Thanks in advance for any help!
You have to include the wp-blog-header.php file. If the page you are creating is in your theme's folder then you would put something like this at the top of your code
<?php require_once ('../../../wp-blog-header.php');?>
I think I guess what you are trying to do, and it sounds like you are going about it the wrong way. Do not make a 'new page' in the admin interface. What you want to do is serve up a file (JSON, XHTML fragment, whatever) to your Javascript and include in it WP data, right? I know that problem, having used it in some of my plugins.
There are two techniques:
(1) This is what you need to do: make a new plugin (just a loose php file in wp-plugins with the same format header as the other plugins in there). Write your function along these lines:
function mydatapage(){
if (...$_SERVER['REQUEST_URI'] == the one I am using ...) {
$args=array(
'p'=>'77'
);
$friends = new WP_Query($args);
if ($friends->have_posts()) :
the_post();
the_title();
the_content();
else:>?
<p>Sorry, no posts are available.</p>
<?php endif;
die();
} //else do nothing and leave WP to serve the page normally
}
//Crucially:
add_action('init', 'mydatapage');
What that does is do a lookup when pages are loaded to see if the url matches the one you want to hijack and use to send your custom data. If it is, you send the data/file/whatever you feel like and exit (die).
Give a shout if you want more detailed syntax. It is a bit messy, but works well.
(2) Directly call your plugin file. WP will only handle files that do not already exist, and leave the rest to Apache. That means you can make a plugin file and call that directly using the .../wp-plugin/myfile.php url. You would need to include some of the WP core files to get things like WP_Query to work. It is marginally more fragile a method.

Wordpress: Removing posts in "The Loop" using filters

After reading the Wordpress documentation, I realized you can remove posts from the index using filters inside "The Loop", e.g.:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- The following tests if the current post is in category 3. -->
<?php if (in_category('3')) continue; ?>
<!-- display normal post -->
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
What I'm wondering is if there is a filter/hook to filter posts in have_posts() without modifying the template. So far, I found options to change the results, but not remove results form the result set.
You can hook into 'pre_get_posts'.More info here: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts. As the article describes:
This hook is called after the query variable object is created, but before the actual query is run.
Using query_posts(), you can override the query variables and exclude any posts. The link has some decent examples on how to do this.
So you want to remove it from displaying but still have it 'there' incase you decide to show it later? Not really clear on what you want to do.
As one example, in the past I've used Query Posts to keep categories off of my homepage: http://codex.wordpress.org/Template_Tags/query_posts#Exclude_Categories_From_Your_Home_Page

Resources