ID for posts/blogs page

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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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.