[Support request] Use WP Show Posts to Replace Blog Archive Page

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Use WP Show Posts to Replace Blog Archive Page

Tagged: 

Viewing 10 posts - 16 through 25 (of 25 total)
  • Author
    Posts
  • #29355
    Kar Yung
    Participant

    Elvin, actually as I try to do this again, I wonder… Is it just better to try to CSS style the archives instead of trying to mess with any archive.php?

    `<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’ );

    $cat = get_category( get_query_var( ‘cat’ ) );
    $cat_slug = $cat->slug;
    if ( function_exists( ‘wpsp_display’ ) )
    wpsp_display( 46456, ‘tax_term=”‘ . $cat_slug . ‘”‘
    );

    /**
    * generate_after_main_content hook.
    *
    * @since 0.1
    */
    do_action( ‘generate_after_main_content’ );
    ?>
    </main>
    </div>`

    I put this in my child theme but then my author archive disappeared so I imagine that needs a different modication?

    #29366
    Kar Yung
    Participant

    Actually, I think I might have confused myself in getting WP Show Posts. Right now it’s live on my main page.

    https://magic.facetofacegames.com

    But now I’m starting to think that I can probably just style the homepage without it. Used the in-built WordPress latest posts. Or am I missing an advantage?

    #29392
    elvin
    Moderator

    This topic was created prior to GP Premium 2.0.

    Consider trying GP Premium 2.0’s update regarding editing your archive template using the Block element. It’s great! 😀
    https://generatepress.com/introducing-the-gp-theme-builder/

    #32122
    Nicolas
    Participant

    Hello,

    I found the snippet provided hereabove by Elvin works well to show posts on a category archive page using WPSP: https://gist.github.com/ejcabquina/60f052a3667da05d479749cb3be3daad

    How would I have to change this snippet for it to work with a custom taxonomy?

    Another point: add_filter( 'generate_has_default_loop', '__return_false' ); works well on my GPP theme to remove the default post loop query, but it is not the case if I add conditionals, for example if(is_archive()){ add_filter( 'generate_has_default_loop', '__return_false' );} so I am confused.

    Thanks for your help

    • This reply was modified 6 days, 21 hours ago by Nicolas.
    #32169
    elvin
    Moderator

    Hi Nicolas,

    the snippet provided should’ve worked on CPT archive pages as the scope of the condition is broad using is_archive().

    But to be sure, you can be more specific and try is_post_type_archive()

    Example:

    if( is_post_type_archive('your_CPT') ){
    //do something.
    }

    Note: any snippet won’t work if the CPT setting doesn’t have CPT archive enabled. (I believe CPT UI can an option to disable/enable archive. Make sure it’s enabled)

    • This reply was modified 5 days, 8 hours ago by elvin.
    #32191
    Nicolas
    Participant

    Thank you Elvin,

    I am not sure I understand. I use the following code:

    add_action( 'generate_before_main_content', function(){
    	if( is_archive() ){
    		$cat = get_category( get_query_var( 'cat' ) );
    		$cat_slug = $cat->slug;
    		if ( function_exists( 'wpsp_display' ) ) 
    		   wpsp_display( 77992, 'tax_term="' . $cat_slug . '"' 
    		);
    	}
    });

    It correctly uses the 77992 WPSP list on the archive page of categories as seen here: https://hautlescours.fr/racinetest10/category/cours-danse/classique-contemporaine-modern/

    But if I go to a custom taxonomy archive page (for example the taxonomy ‘pratiques’) I get the message “Sorry, no posts were found.”: http://hautlescours.fr/racinetest10/pratiques/barre-au-sol/ (but under this message I still see the regular archive posts list as generated by the theme).

    I was under the impression that $cat = get_category( get_query_var( 'cat' ) ); refers only to categories and would necessarily have to be changed for it to work with custom taxonomies.

    Best.

    #32197
    elvin
    Moderator

    What happens if you change this part of code?

    Change if( is_archive() ) to if( is_archive() || is_post_type_archive() )

    Let us know how it goes.

    #32203
    Nicolas
    Participant

    Sorry it doesn’t work.

    But ‘pratiques’ is not a custom post type, it is a custom taxonomy.

    I registered it this way:

    add_action( 'init', 'create_pratiques_taxonomy' );
    function create_pratiques_taxonomy() {
    	register_taxonomy(
    		'pratiques', 'post',
    		array(
    			'label' => 'Activités',
    			'hierarchical' => true,
    			'public' => true,
    			'show_in_nav_menus' => true,
    			'show_tagcloud' => true,
    			'show_admin_column' => true,
    			'show_in_rest' => true,
    		)
    	);
    }
    #32239
    elvin
    Moderator

    ah that makes sense then why it wasn’t working.

    That code will definitely not work because you were getting the terms under the taxonomy ‘category’ while pratique is a taxonomy of its own. (and I assume it has its own terms)

    I believe the query args for that should be different.

    You can set a separate query for that. Example:

    if( is_tax('pratiques') ){    
        $terms = get_the_terms( get_the_ID(), 'category' );
    
        $settings = array(
            'taxonomy' => 'pratiques',
            'tax_term' => array( $term->slug ),
        );
        if ( function_exists( 'wpsp_display' ) ) {
            wpsp_display( 77992, $settings );
        }	   
    }
    #32269
    Nicolas
    Participant

    Thank you very much Elvin,

    I could make the code work changing ‘category’ to ‘pratiques’ and adding foreach( $terms as $term ){ before $settings:

    add_action( 'generate_before_main_content', function(){
    if( is_tax('pratiques') ){    
        $terms = get_the_terms( get_the_ID(), 'pratiques' );
        foreach( $terms as $term ){
        $settings = array(
            'taxonomy' => 'pratiques',
            'tax_term' => array( $term->slug ),
        );
    	}
        if ( function_exists( 'wpsp_display' ) ) {
            wpsp_display( 77992, $settings );
        }	   
    }
    }
    );
Viewing 10 posts - 16 through 25 (of 25 total)
  • You must be logged in to reply to this topic.