$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Why is my Blog Page ID == First Post ID?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Why is my Blog Page ID == First Post ID?

matteradmin9PV0评论

It is possible that I'm doing something wrong. Currently I'm using index.php as the main blog page. I have a page setup for the Blog and set it up in my Settings. When I try to get the Blog Page ID though it always shows the first posts ID instead. I'm not running any additional queries anywhere besides the normal Loop.

Why is my code showing the Post ID versus the Page ID?

Index.php Code:

<?php get_header(); ?>

<?php get_sidebar(); ?>

<?php
    echo $post->ID;  // First Post ID
    echo get_queried_object_id(); // First Post ID
?>

<div id="content" class="blog">
<?php if(have_posts()) : ?>
                <?php while(have_posts()) : the_post(); ?>
                    <div <?php post_class('blog-entry') ?> id="post-<?php the_ID(); ?>">
                        <h1>
                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h1>
                        <small>
                            by Company <?php the_time('F jS, Y') ?> / 
                            <a href="<?php the_permalink(); ?>/#comments">Share Comment</a>
                        </small>

                        <div class="entry">
                            <?php the_excerpt(); ?>
                        </div>

                        <p class="postmetadata">
                            <span style="text-align: left; float: left;">
                                <a href="<?php the_permalink(); ?>" title="Read More">Continue Reading &raquo;</a>
                            </span>
                        </p>
                        <div class="clear"></div>
                    </div>
                <?php endwhile; ?>
                <div id="blogNav">
                    <span class="prev"><?php previous_posts_link('&laquo; Previous Page'); ?></span>
                    <span class="next"><?php next_posts_link('Next Page &raquo;'); ?></span>
                </div>
            <?php endif; ?>
</div>
<?php get_footer(); ?>

It is possible that I'm doing something wrong. Currently I'm using index.php as the main blog page. I have a page setup for the Blog and set it up in my Settings. When I try to get the Blog Page ID though it always shows the first posts ID instead. I'm not running any additional queries anywhere besides the normal Loop.

Why is my code showing the Post ID versus the Page ID?

Index.php Code:

<?php get_header(); ?>

<?php get_sidebar(); ?>

<?php
    echo $post->ID;  // First Post ID
    echo get_queried_object_id(); // First Post ID
?>

<div id="content" class="blog">
<?php if(have_posts()) : ?>
                <?php while(have_posts()) : the_post(); ?>
                    <div <?php post_class('blog-entry') ?> id="post-<?php the_ID(); ?>">
                        <h1>
                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h1>
                        <small>
                            by Company <?php the_time('F jS, Y') ?> / 
                            <a href="<?php the_permalink(); ?>/#comments">Share Comment</a>
                        </small>

                        <div class="entry">
                            <?php the_excerpt(); ?>
                        </div>

                        <p class="postmetadata">
                            <span style="text-align: left; float: left;">
                                <a href="<?php the_permalink(); ?>" title="Read More">Continue Reading &raquo;</a>
                            </span>
                        </p>
                        <div class="clear"></div>
                    </div>
                <?php endwhile; ?>
                <div id="blogNav">
                    <span class="prev"><?php previous_posts_link('&laquo; Previous Page'); ?></span>
                    <span class="next"><?php next_posts_link('Next Page &raquo;'); ?></span>
                </div>
            <?php endif; ?>
</div>
<?php get_footer(); ?>
Share Improve this question edited Aug 30, 2013 at 17:18 Howdy_McGee asked Aug 30, 2013 at 16:44 Howdy_McGeeHowdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges 2
  • We would have to see your code to tell you anything about it. – Charles Clarkson Commented Aug 30, 2013 at 17:06
  • Updated my question, it's all pretty standard though. – Howdy_McGee Commented Aug 30, 2013 at 17:18
Add a comment  | 

2 Answers 2

Reset to default 2

the 'posts page' ID when using a static front page and a different posts page is:

get_option( 'page_for_posts' )

in some context:

if( is_home() && get_option( 'page_for_posts' ) ) { 
  $page_ID = get_option( 'page_for_posts' ); 
}

Why don't just :

if( is_home() ) { 
  $page_ID = get_option( 'page_for_posts' ); 
}

?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far