WordPress.org

Ready to get started?Download WordPress

Forums

Function for inside the loop to determine if a post is a "page" type? (9 posts)

  1. Kaleb.G
    Member
    Posted 4 years ago #

    I've searched all over trying to find a simple function that will let me know if one of the posts returned from inside the loop is either a WordPress "page" or a "post". is_page() doesn't work, because that only works if the entire page you're viewing is the content of a WordPress "page".

    What I'm trying to do exactly is this: My search results page displays both "pages" and "posts", which is good. However, each of the "pages" shows that it's posted in the "Uncategorized" category. Instead of saying that, I want a function to determine if it's a "page" type, and have it show some different text rather than the category list.

    Can someone please help me? Thanks in advance.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    If it's inside the Loop, try:

    <?php if($post->post_type == 'post') :?>
    [ do stuff ]
    <?php endif;?>
  3. Kaleb.G
    Member
    Posted 4 years ago #

    Ha, that worked! I think this would be good information to add to the Codex. Or if it's in there, make it easier to find. Anyway, this is exactly what I needed. Thank you!

  4. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

  5. Kaleb.G
    Member
    Posted 4 years ago #

    alchymyth, I already knew what the database schema is. I just didn't know I could access the field by using $post->field_name. I haven't seen that explained anywhere in the Codex.

  6. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    you are right - the codex is great if one knows what to look for.

    it is not the best instruction manual.

  7. Kaleb.G
    Member
    Posted 4 years ago #

    Agreed. Hopefully the people who have access to work on it can improve on this aspect.

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

  9. Kaleb.G
    Member
    Posted 4 years ago #

    I'm pretty sure I had tried that function and it didn't work for me.

    Then again I may not even been searching the Codex for "post type". I was searching for stuff like "page or post".

Topic Closed

This topic has been closed to new replies.

About this Topic