WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] If is custom post type archive page

[Resolved] If is custom post type archive page

  • 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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    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.

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] If is custom post type archive page’ is closed to new replies.