WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] If is custom post type archive page (4 posts)

  1. Clicknathan
    Member
    Posted 2 years ago #

    Does anyone know how I can check whether I'm on a specific custom post types main archive page?

    For example, I have a custom post type cpt_images with a rewrite slug of images and I want to check if I'm on the http://website.com/images/ page (ie, the cpt_images archive page)?

    Any ideas?

  2. vtxyzzy
    Member
    Posted 2 years ago #

    In the Twenty Ten theme archive.php, the first post is queued up so it can be tested. Here is the code used:

    /* Queue the first post, that way we know
     * what date we're dealing with (if that is the case).
     *
     * We reset this later so we can run the loop
     * properly with a call to rewind_posts().
     */
    if ( have_posts() )
       the_post();

    You can use something similar. Queue the first post, test the post type and call rewind_posts() later.

    if ( have_posts() ) {
       the_post();
       if ($post->post_type == 'images') {
          echo "<h2>In Images Archive</h2>";
       }
       rewind_posts();
    }

    Leave out the rewind_posts() call if it is made later on before the actual Loop.

  3. In the source code, if your theme uses the body_class() in the header, you can see what classes are generated. I find that's a useful way to know what template is used.

  4. Clicknathan
    Member
    Posted 2 years ago #

    Thanks vtxyzzy, just what I needed.

    Thanks to you, too, Christine, but I needed a way to do a PHP call based on the post type being shown, not just a CSS mod.

Topic Closed

This topic has been closed to new replies.

About this Topic