WordPress.org

Ready to get started?Download WordPress

Forums

Getting current Post ID ? (out of The Loop) (8 posts)

  1. richardlee
    Member
    Posted 9 years ago #

    Hi again,

    I've been using a similar function to that used by the Blix theme (BX_get_recent_posts) which lists the most recent posts in the blog. Problem is, like the Blix function it relies on the post id 'p=1' --passed by clicking on the post title when in Non-Permalinks mode-- to hide the current active Post from the list.

    Anyone know of a sure method for getting the current post id to a function in the sidebar?

    Thanks in advance :)

  2. masquerade
    Member
    Posted 9 years ago #

    It will work just fine with mod_rewrite permalinks, all mod_rewrite does is map the fancy permalinks to query strings.

  3. Kafkaesqui

    Posted 9 years ago #

    Try:

    $post = $wp_query->post;

    Then pass the function:

    $post->ID

  4. Kafkaesqui

    Posted 9 years ago #

    masquerade, with custom permalinks, unless using %post_id% as part of your structure you don't have the ID available through the GET query (just the post name through '?name=name-of-post').

  5. richardlee
    Member
    Posted 9 years ago #

    Thats neat Kaf ;) thanks a bunch. I'll try it out and if all is well mark this as resolved.

    Oh and masquerade thanks for your assistance, but like Kaf has said activating permalinks changes the entire structure which is why I posted this question :).

    Just for anyone else reading this, keep in mind this is a small issue with the Blix theme. Although not fatal, the recent posts generation partly relies on non-permalink structure to deduce the current Post.

  6. richardlee
    Member
    Posted 9 years ago #

    As expected it works. Thanks again.

  7. richardlee
    Member
    Posted 9 years ago #

    Actually Kaf, can you explian to me where $post = $wp_query->post; comes from?

  8. Kafkaesqui

    Posted 9 years ago #

    The post data is already sort of, to use a technical term, floating around as a query object on a single post page. Assigning $post the current post arrary through $wp_query is but one way to access it.

    As the scope of $post in a page is global, another that works in many (though not all) cases is to use $post without "claiming" it first. When it doesn't, you could try setting it to global:

    <?php
    global $post;
    echo $post->ID;
    ?>

    Also note this single line of the previous code works just as well:

    $wp_query->post->ID;

    Though it reads a bit geek.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.