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

  1. Clicknathan
    Posted 4 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
    Posted 4 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() )

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

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

    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
    Posted 4 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