[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

This topic contains 7 replies, has 2 voices, and was last updated by  Tom 1 week, 3 days ago.

Viewing 8 posts - 1 through 8 (of 8 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 3 weeks, 2 days 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 2 weeks, 1 day 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.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.