Home › Forums › Pro Support › Use WP Show Posts to Replace Blog Archive Page
Tagged: Blog Archive
- This topic has 32 replies, 6 voices, and was last updated 10 months, 4 weeks ago by
elvin.
-
AuthorPosts
-
April 16, 2021 at 2:56 pm #29355
Kar Yung
ParticipantElvin, 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?
April 16, 2021 at 7:56 pm #29366Kar Yung
ParticipantActually, 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?
April 18, 2021 at 6:47 pm #29392elvin
ModeratorThis 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/July 17, 2021 at 3:53 am #32122Nicolas
ParticipantHello,
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 exampleif(is_archive()){ add_filter( 'generate_has_default_loop', '__return_false' );}
so I am confused.Thanks for your help
July 18, 2021 at 5:42 pm #32169elvin
ModeratorHi 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)
July 18, 2021 at 11:53 pm #32191Nicolas
ParticipantThank 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.
July 19, 2021 at 12:39 am #32197elvin
ModeratorWhat happens if you change this part of code?
Change
if( is_archive() )
toif( is_archive() || is_post_type_archive() )
Let us know how it goes.
July 19, 2021 at 12:58 am #32203Nicolas
ParticipantSorry 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, ) ); }
July 19, 2021 at 7:38 pm #32239elvin
Moderatorah 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 ); } }
July 20, 2021 at 2:16 am #32269Nicolas
ParticipantThank 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 ); } } } );
July 26, 2021 at 11:48 pm #32434Nicolas
ParticipantSorry Elvin, I get back to you as my code does not work:
add_action( 'generate_before_main_content', function(){ if( is_tax('lieu') ){ $terms = get_the_terms( get_the_ID(), 'lieu' ); foreach( $terms as $term ){ $settings = array( 'taxonomy' => 'lieu', 'tax_term' => array( $term->slug ), ); } if ( function_exists( 'wpsp_display' ) ) { wpsp_display( 78685, $settings ); } } } );
I think adding
foreach( $terms as $term )
as I tried it was not a good idea.Here is what happens, let’s say I go to the archive page for the term “__Normandie” of the taxonomy “Lieu”: https://hautlescours.fr/lieu/__normandie/. There is only one post with the term “__Normandie” on this page (the last one in the list). This post also has the term “Paris lieu variable” from the taxonomy “Lieu”. What seems to happen is that this archive page will give me the list of all the posts with “Paris lieu variable” posts and not those with “__Normandie”.
Would you have any idea on how to make this work? (my goal is to use WPSP on archive pages of custom taxonomies).
Thanks,
Nicolas
July 27, 2021 at 2:33 am #32440elvin
ModeratorTry this:
add_action( 'generate_before_main_content', function(){ if( is_tax('lieu') ){ $terms = get_the_terms( get_the_ID(), 'lieu' ); foreach( $terms as $term ){ if( is_archive($term) ){ $settings = array( 'taxonomy' => 'lieu', 'tax_term' => $term, ); if ( function_exists( 'wpsp_display' ) ) { wpsp_display( 78685, $settings ); } } } } });
Let us know how it goes.
July 27, 2021 at 5:12 am #32451Nicolas
ParticipantThank you Elvin,
Your code doesn’t seem to work as is but it produces something if I replace
'tax_term' => $term,
with'tax_term' => array( $term->slug )
The result is a bit better (now on my staging site): https://hautlescours.fr/racinetest10/lieu/__normandie/
It displays two WPSP lists (and the regular archive list at the bottom). The first one (a small thumbnail due to CSS settings) works well, this is the only post with “__normandie” term which is what I want.
The second WPSP list is the faulty one displaying posts with “Paris lieu variable” term.
And the third list is the regular archives results produced by the theme (one post as expected).
So this is getting closer, except for the fact that the code generates two wpsp lists.
Best.
July 28, 2021 at 11:03 pm #32499elvin
Moderatorah right yeah that makes sense. It’ll make 2 or more WPSP list because it’s in a for loop of terms.
Let’s move it out.
Try this:
add_action( 'generate_before_main_content', function(){ if( is_tax('lieu') ){ $terms = get_the_terms( get_the_ID(), 'lieu' ); foreach( $terms as $term ){ if( is_archive($term) ){ $settings = array( 'taxonomy' => 'lieu', 'tax_term' => $term->slug, ); } } if ( function_exists( 'wpsp_display' ) ) { wpsp_display( 78685, $settings ); } } });
July 29, 2021 at 1:51 am #32507Nicolas
ParticipantThank you Elvin but we are back at the situation of reply 32434.
Here is what happens:
- If the first post of the archive page has only one term for the taxonomy ‘lieu’ (the slug of the archive page, for example paris-05) there is no problem. For example https://hautlescours.fr/racinetest10/lieu/paris-05/, WPSP and the theme archive module give the same result
- If the first post of the archive has several terms for the taxonomy ‘lieu’ but that the term of the archive page is the last term of the post for this taxonomy, it also works: for example https://hautlescours.fr/racinetest10/lieu/paris-07/
- But if there are several terms for the first post, WPSP is going to display posts having the last term of the first post: https://hautlescours.fr/racinetest10/lieu/paris-06/. For example here, the post « L’improvisation, c’est du théâtre ! » has ‘paris-06’ as a term and ‘paris-13’ as a last one so WPSP displays ‘paris-13’ posts, and not ‘paris-06’ as expected.
-
AuthorPosts
- You must be logged in to reply to this topic.