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.
consider to use:
and then change the rspective line in your code to:
from: http://www.blog.highub.com/cms/wordpress/wordpress-front-page-posts-page-id/
If you are using
query_posts
(which has some drawbacks since it alters the main loop), as the Codex says,(See codex
wp_reset_query()
)for For exampleIf you are not using
query_posts
you may still need to usewp_reset_postdata()
which…