Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a website which has a static front page and a blog page where all the blogs are displayed.

I am using my own theme and have created some custom meta boxes that display content in a sidebar based on the return value from $post->ID. The interesting behaviour I am getting is that $post->ID gives me the ID of the first blog not the id of the blog page itself. I am using $post outside the loop and have declared it global but to no avail. I have also tried using $wp_query->post->ID but that gives me the ID of the last post.

The relevant code is where I use $post is below this piece of code is located in footer.php:

<?php require_once('wp-content/plugins/markdown.php'); ?>
    <aside class="left-column">
        <?php
            global $post;
            $leftSidebar = get_post_meta( $post->ID, '_my_meta', true );
            // Convert markdown to HTML and then convert smilies
            if ( isset( $leftSidebar['leftContent'] ) ) {
                echo convert_smilies( markdown( $leftSidebar['leftContent'] ) );
            }
        ?>
    </aside>

The code that is used for the loop is below and is placed in index.php:

get_header(); ?>

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="post-date"><?php the_time(get_option( 'date_format' )); ?></div>
        </header>
        <?php the_excerpt(); ?>
        <footer class="post-footer">
            <div class="categories">Posted in: <?php the_category(', '); ?></div>
            <div class="tags">Tags: <?php the_tags(); ?></div>
        </footer>
    </article>
   <?php endwhile; ?>
<?php else : ?>
    <article>
        <h3>Not Found</h3>
        <p>Sorry, but you are looking for something that is not available</p>
    </article>
<?php endif; ?>

Please do let me know if any further information is required. If there is a way to find the ID of the blog page programatically and make the blog page recognize itself as the blog page and not the first post that would solve me problem I think.

Thanks in advance.

share|improve this question
    
not the best contribution playing search engine helper, however, this might apply: $postspage_id = get_option('page_for_posts'); from: blog.highub.com/cms/wordpress/… –  Michael Feb 25 '12 at 15:13
    
@Michael - post this as an answer? –  Stephen Harris Feb 25 '12 at 15:32
    
moved the suggestion into an answer. –  Michael Feb 25 '12 at 18:27

2 Answers 2

up vote 6 down vote accepted

consider to use:

$postspage_id = get_option('page_for_posts'); 

and then change the rspective line in your code to:

$leftSidebar = get_post_meta( $postspage_id, '_my_meta', true ); 

from: http://www.blog.highub.com/cms/wordpress/wordpress-front-page-posts-page-id/

share|improve this answer
    
Thanks the above it works for me. I just have to do a check if the current page I am viewing is the blog page. This can be done by if (is_home($postpage_id)) which will evaluate to true if the page viewed is the blog/posts page. Using is_home to check was a vital piece of the puzzle because is_page etc. do not work for the blog page. I found out about this at this post. I am marking this as an answer. –  navanitachora Feb 26 '12 at 1:52

If you are using query_posts (which has some drawbacks since it alters the main loop), as the Codex says,

If you must use query_posts(), make sure you call wp_reset_query() after you're done.

(See codex wp_reset_query())for For example

// The Query
query_posts( $args ); //your $args

// The Loop
while ( have_posts() ) : the_post();
      //Display title, content here etc.
endwhile;

// Reset Query afterwards
wp_reset_query();

If you are not using query_posts you may still need to use wp_reset_postdata() which...

After looping through a separate query, this function restores the $post global to the current post in the main query.

share|improve this answer
    
I am not using query_posts and I have tried both wp_reset_query() and wp_reset_postdata() but to no avail. Are there any alternatives that will make my blog page recognize itself as the blog page. –  navanitachora Feb 25 '12 at 14:27
    
You'll have to update your question and provide the code you are using for the Loop, hard to say what's wrong otherwise. –  Stephen Harris Feb 25 '12 at 14:54
    
I have updated the question with the code for the loop. –  navanitachora Feb 25 '12 at 15:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.