Support » Plugin: Custom Post Type UI » CPT Archive Question

  • Resolved D

    (@clocksaysnow)



    Hi,

    I used your excellent Custom Post Type UI plugin to easily create a custom post type. I also want my custom post types to show up in the tag archives along with my posts, so I inserted the following snippet into a custom plugin to create a tag archive that successfully merges my posts and custom post types:

    <?php
    /*
    Plugin Name: Site-Specific WordPress Plugin
    Description: Created per instructions found at http://docs.pluginize.com/article/17-post-types-in-category-tag-archives. The snippet below allows custom post types to appear in tag archives.
    */
    /* Start Adding Functions Below this Line */

    function my_cptui_add_post_types_to_archives( $query ) {
    // We do not want unintended consequences.
    if ( is_admin() || ! $query->is_main_query() ) {
    return;
    }

    if ( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );

    /* Stop Adding Functions Below this Line */
    ?>

    So far, so good. I just have one remaining problem: in the tag archive, the layout/styling of my custom post types doesn’t look the same as the layout of my posts. In hopes of fixing the problem, I created an archive-{posttype}.php file, filled it with all the same code found in my archive.php file, and inserted it into my child theme.

    But the layout/styling of the custom post types still differs from the layout/styling of my posts in the tag archive. Would you happen to have a snippet of code that could solve this problem for me, so that my custom post types will use the same layout as my posts do in the tag archive? (I’m not skilled at php.) And if so, where would I put the snippet?

    Many thanks!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    A little hard to say what exactly is going on, as a number of templates may be in play here.

    First up I’d confirm if you have a tag.php template file, which would be what’s used for tag archives, regardless of post types included in it.

    After that it’d be a case of checking template files used for any possible logic that dictates what else to possibly load, dependent on post type slug.

    I don’t have any sort of snippet available for you as it’s all dictated by the theme being used.

    D

    (@clocksaysnow)

    Hi Michael,

    No, there’s no tag.php template file. I just now checked via email with the theme maker, who said that the tag archive is controlled by the achieve.php file. The theme maker also wrote, “WordPress should pick it up automatically for your custom post types. If it doesn’t, make sure to pass the ‘has archive’ => true parameter to your register_post_type function call.” He didn’t say where or exactly how I should do that, but I assume it should be done in the functions.php file.

    Here’s the section of my theme’s functions.php that refers to the archive:

    if ( ! function_exists( ‘ttf_common_archives_title’ ) ) :
    /**
    * Adjust the archives page title to something sensible
    */
    function ttf_common_archives_title() {
    if ( is_category() ) { /* If this is a category archive */
    printf( __( ‘Posts from the ‘%s’ category’, ‘ttf_common’ ), single_cat_title( ”, false ) );
    } elseif ( is_tag() ) { /* If this is a tag archive */
    printf( __( ‘Posts tagged ‘%s’’, ‘ttf_common’ ), single_tag_title( ”, false ) );
    } elseif ( is_day() ) { /* If this is a daily archive */
    printf( __( ‘Archive for ‘%s’’, ‘ttf_common’ ), get_the_time( ‘F jS, Y’ ) );
    } elseif ( is_month() ) { /* If this is a monthly archive */
    printf( __( ‘Archive for ‘%s’’, ‘ttf_common’ ), get_the_time( ‘F, Y’ ) );
    } elseif ( is_year() ) { /* If this is a yearly archive */
    printf( __( ‘Archive for ‘%s’’, ‘ttf_common’ ), get_the_time( ‘Y’ ) );
    } elseif ( is_author() ) { /* If this is an author archive */
    printf( __( ‘Posts by %s’, ‘ttf_common’ ), get_the_author() );
    } elseif ( is_paged() ) { /* If this is a paged archive */
    _e( ‘Blog Archives’, ‘ttf_common’ );
    }
    }

    endif; // ttf_common_archives_title

    I’m not adept at php code. Would you know the proper way that I would “pass the ‘has archive’ => true parameter to your register_post_type function call” in the above code? And then after making that change, I would add the revised functions.php file to my child theme, correct?

    Thanks!

    Plugin Author Michael Beckwith

    (@tw2113)

    “has archive” details for the post type, in your case, would be part of the CPTUI settings for the post types in question. I know we have a spot for that topic.

    The code above that you have just determines what title the template displays. Nothing regarding the overall content.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘CPT Archive Question’ is closed to new replies.