views:

1687

answers:

6

Hello,

I know there are functions to like is_single() that will return data about the page, but I'm looking for a way to get the following information outside of the loop:

The category of the single post.

AND

The title of the single post.

All I would really need is the post ID in question and I could get all the other information. I've looked through the functions reference in the codex, but I haven't found anything. Is this impossible because the script doesn't even get that information 'til the Loop runs?

(I would need this information in both the header and footer, so before and after the PHP script for the loop, if that is a problem.)

Hopefully someone can offer some insight.

EDIT: To clarify: I want the information to be from the post that is loaded in the loop on the "single" page. (AKA the post they are viewing.) So how would I get this ID in the first place? Basically, when viewing a post, I want to get its category or title, but not while the loop is going.

+1  A: 

You could execute your own query and return a $post object and then access it's attributes through something like

echo $post->title;

Read about $wpdb and Custom Queries on the Codex. Basically you could run a SQL query using the ID as a where filter through its $wpdb object.

Another option, more WP-like, would be to use a Custom Query Post, where you could define another loop, returning only your post using its id:

query_posts('p=2010'); // 2010 being the post's ID

Then you could run the alternative loop and use the similar $post object to display some information.

Yaraher
Okay, but the problem I'm seeing is... I want the information to be from the post that is loaded in the loop on the "single" page. (AKA the post they are viewing.) So how would I get this ID in the first place?Basically, when viewing a post, I want to get its category or title, but not while the loop is going.
Ian Storm Taylor
+1  A: 

This is what I use inside the loop to run an new query; I just tried it outside the loop and it works, on a single.php template page. This will give the title of the latest post in mycategory. I don't know how to pull the category ID, though.

<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><?php the_title(); ?><?php endwhile; ?>
songdogtech
Again the problem is this is making a new query. What I need is to access the post already queried on the is_single page. But I need it outside the loop on that page.
Ian Storm Taylor
The loop has already run, so you need to query or search the code of the completed page? I don't know what the aim is; I guess I'd rethink why you need to do that.
songdogtech
A: 

After the query has run, try running:

global $wp_query;
var_dump( $wp_query );

The information you are looking for is probably in $wp_query->query_vars or $wp_query->post

postpostmodern
A: 

The usual thing is to create "second loop" which would extract needed post, hence your needed data.

Eimantas
+3  A: 

This is actually quite simple.

// Gets an array of post objects.  
// If this is the single post page (single.php template), this should be an
// array of length 1.
$posts = get_posts();
// The post object contains most/all of the data that is accessible through the 
// normal functions like `the_ID()`, `the_author()`, etc
var_dump($posts[0]->ID);

This can be performed outside of and it doesn't affect the normal loop. For example, my single.php template looks like this (line numbers included):

1. <?php
2. get_header(); 
3. $posts = get_posts();
4. var_dump($posts[0]->ID);
5. ?>
6. <div id="post">
Justin Johnson
A: 

Couldn't you store the $post information in a separate variable on the page, then just echo the data you need out later when you need it (ie, outside the loop)?

This is predicated on needing the data after the initial loop has run.

PrairieHippo