[Resolved] Archive for Custom Post Types

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Archive for Custom Post Types

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #9666
    Dee
    Participant

    I have successfully used the following to use WPSP for my standard archives.

    $cat = get_category( get_query_var( 'cat' ) );
    
    $cat_slug = $cat->slug;
    $list = get_page_by_title( 'Simple', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );

    I would like to use something similar for my custom post archives, but I cannot get it to display for my CPT archive. This is what I have so far:

    if ( is_post_type_archive('event') ) :
    	
    	$cat = get_the_terms( $post->ID, 'event_category', get_query_var( 'event_category' ) );
    else :
    
    	$cat = get_category( get_query_var( 'cat' ) );
    endif;
    	$cat_slug = $cat->slug;
    	$list = get_page_by_title( 'Simple', 'OBJECT', 'wp_show_posts' );
    	wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
    
    

    The standard post archives display as desired, but the CPT show nothing.

    • This topic was modified 1 year, 6 months ago by Dee. Reason: misspelling
    #9727
    Tom
    Keymaster

    What if instead of this:

    get_the_terms( $post->ID, 'event_category', get_query_var( 'event_category' ) );

    You do this:

    get_term( get_query_var( 'event_category' ), 'event_category' );

    Let me know 🙂

    #9746
    Dee
    Participant

    For those looking for a solution to use WPShowposts as the display of the archive pages, including with custom post types, here is what I did:

    I have 2 custom post types, each with their own custom taxonomies which I created in PODS. I set the archive to true for each.
    CPT ‘event’ custom taxonomy ‘event_category’
    CPT ‘client’ custom taxonomy ‘source’

    1. Create a WP Show Posts list for each post type:
    Post type: post Taxonomy: category
    Post type: event Taxonomy: event_category
    Post type: client Taxonomy: source

    2. Copy the archive.php file to the generatepress-child folder

    3. Replace the coding between do_action( ‘generate_archive_title’ ); and generate_content_nav( ‘nav-below’ ); with the following

    $cat = get_queried_object();
    $cat_slug = $cat->slug;
    $cat_tax = $cat->taxonomy;
    
    if ($cat_tax=='category'):  $list = get_page_by_title( 'Simple', 'OBJECT', 'wp_show_posts' );
    elseif ($cat_tax=='event_category'): $list = get_page_by_title( 'Simple-Event', 'OBJECT', 'wp_show_posts' );
    elseif ($cat_tax=='source'): $list = get_page_by_title( 'Simple-Client', 'OBJECT', 'wp_show_posts' );
    endif;
    wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );

    Issues resolved:
    1. I originally used is_post_type_archive() for testing if an archive of a custom post type, but it only evaluates as true if the archive is of the custom post type, and not for the associated taxonomies

    2. get_category() returns an object, so you can get the slug value with the associated object or post, but for custom post types you use get_term() or get_the_terms() which returns an array, so you cannot get the slug value. I changed the code to get_queried_object() and it works for both custom post types and regular posts.

    3. You need a different WP Show Post list for each different post type, which I missed for a while, even though it is obvious.

    To minimize the coding further, you could remove the $list=get_page_by_title…. And just use the WPSP ID for each list, but I like being able to see which list is being used.

    There is still a display issue with WPSP in archives with repeating post thumbnails which I will cover in another post.

    #9763
    Tom
    Keymaster

    Very cool – thanks for sharing everything here 🙂

    #9823
    Dee
    Participant

    I thought this was working correctly as I had my blog redirected to a page, but for my generic “blog” archive, I am not getting the correct WPshowposts. I belive it is the “else” below, but I am not sure how to fix this.
    Here is what I have in the content section of the archive.php file”

    			if ( have_posts() ) :
    
    				/**
    				 * generate_archive_title hook.
    				 *
    				 * @since 0.1
    				 *
    				 * @hooked generate_archive_title - 10
    				 */
    				do_action( 'generate_archive_title' );
    
    						$cat = get_queried_object();
    						$cat_slug = $cat->slug;
    						$cat_tax = $cat->taxonomy;
    
    							if ($cat_tax=='category'):  $list = get_page_by_title( 'Simple', 'OBJECT', 'wp_show_posts' );
    
    							elseif ($cat_tax=='event_category'): $list = get_page_by_title( 'Simple-Event', 'OBJECT', 'wp_show_posts' );
    
    							elseif ($cat_tax=='source'): $list = get_page_by_title( 'Simple-Client', 'OBJECT', 'wp_show_posts' );
    							
    							endif;
    	
    						wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
    	
    				generate_content_nav( 'nav-below' );
    
    			else :
    
    				get_template_part( 'no-results', 'archive' );
    
    			endif;

    I am getting the generic post format instead of the wpsp_display format.
    I have a page named “Blog”, and my Homepage settings are set to “Posts”.
    Blog displays like I want, but when I switch my home page settings to “Blog” instead of “Posts” it reverts to the generic layout.
    I have the archive.php saved in the GP-child theme.

    • This reply was modified 1 year, 5 months ago by Dee. Reason: More info added
    #9856
    Tom
    Keymaster

    Just to confirm, it works when you have your front page set as the “Posts Page”, but breaks when you set the posts page as a static page?

    #9859
    Dee
    Participant

    So this site is live now, so perhaps some links will help. I disabled my redirect, so you can see both pages.

    I have 2 pages and both are identical /posts and /blog with content
    [wp_show_posts name=”Simple”]

    I currently have /posts/ set as my static blog page, and am redirecting to blog to get the format I desire with the above shortcode.
    If a switch the static page to point to /blog, then the default format (from where?) is shown, and not the wpshowposts shortcode version.

    The above coding works on the category and CPT archives, just not the main archive.

    https://www.deezunkerphotography.com/blog/
    https://www.deezunkerphotography.com/posts/

    #9866
    Tom
    Keymaster

    Is there a need to set the “Posts page” in “Settings > Reading”? Or is it possible to just leave it blank? WordPress will overwrite the content of that page completely if it’s set as the posts page.

    #17611
    Jon
    Participant

    Hey there, was attempting to use WPSP within my CPT archive built with Toolset here:https://matchlessweb.com/project/

    I created the CPT and archive with Toolset. I would like to not have to use Toolset to design the loop layout or “view” as they call it. I much prefer the styling an ease of use of WPSP.

    Toolset doesn’t seem to allow you to delete the loop from the page. But I suppose I could leave it empty and just drop the WPSP shortcode below that. So there’d be an empty loop function but at least the shortcode would show up. At least I think.

    Since I don’t want the loop function of the Toolset archive page, would I be better of creating a static page called https://matchlessweb.com/projects <—-with an “s” (on the end of the slug) and just treat that static page as my pseudo-archive for this CPT?

    Currently, the single CPT posts have this link structure: https://matchlessweb.com/project/ms-mane-co/ (but I might be able to change that in the Toolset settings, and maybe even the archive page title if it would help)

    But I guest the CPT posts themselves wouldn’t actually be children of the CPT because the relationship between the archive page and the posts would be severed. Tell me if I’m wrong. I’m just guessing here.

    Would that be a bad idea? Any negative impact from a site structure or SEO standpoint or anything else I’m not considering?

    #17627
    Tom
    Keymaster

    It may be better to create a custom page template with the wpsp_display() function: https://generatepress.com/forums/topic/create-custom-category-php-template-wp-show-posts-wpsp/

    #17630
    Jon
    Participant

    Thanks for pointing me in the right direction, Tom. That’s what I needed. You the real MVP!

    #17643
    Jon
    Participant

    Ok I’m feeling pretty dense. I feel like I’ve followed the instructions from the post you linked above, but I’m not having any luck getting the posts to display on the front end of the site. Maybe I’ve got the syntax wrong. I’ve got the following code in a my child theme’s category-projects.php file with the understanding it should allow the archive of my categories to be displayed by WPSP at the following link https://matchlessweb.com/category-projects

    <?php
    /**
     * The template for displaying Archive pages.
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    
    	<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
    		<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
    			<?php
    			/**
    			 * generate_before_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_before_main_content' );
    
    <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 1746 ); ?>  }
    			/**
    			 * generate_after_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_after_main_content' );
    			?>
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    	<?php
    	/**
    	 * generate_after_primary_content_area hook.
    	 *
    	 * @since 2.0
    	 */
    	do_action( 'generate_after_primary_content_area' );
    
    	generate_construct_sidebars();
    
    get_footer();

    Does that look right? or is there an error in here I’m missing?

    The function from the WPSP page is: <?php if ( function_exists( ‘wpsp_display’ ) ) wpsp_display( 1746 ); ?>

    And the shortcode is: [wp_show_posts id=”1746″]

    #17655
    Tom
    Keymaster

    Hi there,

    Instead of this: <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 1746 ); ?> }

    Try this:

    if ( function_exists( 'wpsp_display' ) ) {
        wpsp_display( 1746 );
    }

    Also, category-projects.php will work when you visit this URL: /category/projects/

    If your category is actually category-projects, you’d need this file name: category-category-projects.php

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.