Site logo

Archived topic

Add Featured Posts Section to All Archives

20 replies · Started by Lewis on July 8, 2022

Viewing posts 1–15 of 21

I want to add a featured post section to each category archive but those posts should only come from the current category and that have 'current-featured' tag assigned.

I have WP Show Posts installed and know I could go through and generate a shortcode individually for each category but I was hoping there would be a way to set up one element which would work for all category pages.

Hi there,

thats quite complicated to do, and i am not sure we have a solution for that today.
We just released GenerateBlocks 1.5, that has the new Query Loop block, and in the next main update of GP Premium we will be adding a new hook that will allow you to build your archive pages with GenerateBlocks.

Do you have an example of the design your looking for ? In case its less complicated then i think.

I don’t have an example to be honest. Essentially, (for now) I just want to pull in 5 post titles fr the current category using WP show posts (I can style it later).

Ideally I want to be able to limit those 5 posts to those with a certain tag applied too.

For example, let’s say I have a site about crafts and I have a category about candle making. During Christmas, I want to feature a few Christmas candle making posts at the top of the archive so I apply a tag to those posts of “featured”.

I then want to apply this to all category pages on the site. But I want the list of posts to only show posts from the current category.

The long workaround would be for me to create individual short codes for each and every category page on my site but was hoping there would be a speedy workaround

Ok, its not easy and i am not sure if this will work:

1. Create your WPSP list, don't assign any categories or other filter settings.
Make a note of its ID from the list editor.

2. Add the WPSP list Shortcode to your Category Archives using a Hook Element

e.g the generate_after_header hook.
Display Rules > Location: Category Archives > All Archives.

3. Add this PHP Snippet to filter the list to display only posts in the current category and that have the featured tag.


add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
    $category = get_queried_object();
    $cat_id = $category->term_id;	
    if ( 99 === $settings['list_id'] ) {
        $args['category__and'] = $cat_id;
        $args['tag__in'] = 33;
    }

    return $args;
}, 10, 2 );

The 99 is the WPSP List ID. And the 33 is the Tag ID. Change those to match your list and featured tag.

4. Now add this PHP Snippet to Exclude the posts with featured tag from the current category archive. So you don't have duplicate posts.


function excludeTagId($query) {
  $tag_Ids = array( 33 );

  if ($query->is_main_query() && ! is_admin() && $query->is_category() ) {
    $query->set( 'tag__not_in', $tag_Ids );
  }
}
add_action('pre_get_posts', 'excludeTagId');

I added this my functions.php file (child theme):

<?php 
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
    $category = get_queried_object();
    $cat_id = $category->term_id;	
    if ( 2125 === $settings['list_id'] ) {
        $args['category__and'] = $cat_id;
        $args['tag__in'] = 20;
    }

    return $args;
}, 10, 2 ); ?> 

But I received the following error:

Your PHP code changes were rolled back due to an error on line 124 of file wp-content/themes/generatepress/functions.php. Please fix and try saving again.

Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in wp-includes/class-wp-hook.php on line 307 and exactly 2 expected in wp-content/themes/generatepress/functions.php:124
Stack trace:
#0 wp-includes/class-wp-hook.php(307): {closure}()
#1 wp-includes/plugin.php(191): WP_Hook->apply_filters()
#2 wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters()
#3 wp-content/plugins/wp-show-posts/wp-show-posts.php(569): wpsp_display()
#4 wp-includes/shortcodes.php(356): wpsp_shortcode_function()
#5 [internal function]: do_shortcode_tag()
#6 wp-include

I have got this code working:

<?php
if ( is_archive() ) {
    $cats =  get_the_category();
    $cat = $cats[0];
} else {
    $cat = get_category( get_query_var( 'cat' ) );
}

$cat_slug = $cat->slug;
$list = get_page_by_title( 'trending category main', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
?>

This filters the WP Show Post for the current category. Do you know if there is a way for me to tweak this code to also add a filter for the 'Featured' tag I have set up?

I did add it without the <?PHP ?> previously and got an error. Have tried again in functions.php and the same error is occurring:

Your PHP code changes were rolled back due to an error on line 9 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again.

Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in wp-includes/class-wp-hook.php on line 307 and exactly 2 expected in wp-content/themes/generatepress_child/functions.php:9
Stack trace:
#0 wp-includes/class-wp-hook.php(307): {closure}()
#1 wp-includes/plugin.php(191): WP_Hook->apply_filters()
#2 wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters()
#3 wp-content/plugins/wp-show-posts/wp-show-posts.php(569): wpsp_display()
#4 wp-includes/shortcodes.php(356): wpsp_shortcode_function()
#5 [internal function]: do_shortcode_tag()
#6 wp-inc

Weird, is there any other code in the functions.php ?

Nope, it's completely empty

Which version of WP Show Posts are you running ?

I did have a snippet plugin which added code just not via the functions.php so I have successfully moved that over so my functions.php file is now:

<?php
/**
 * GeneratePress child theme functions and definitions.
 *
 * Add your custom PHP in this file.
 * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
 */

add_filter( 'generate_hero_thumbnail_id_size', function() {
    return 'large';
} );

add_shortcode( 'excerpt', function() {
    return get_the_excerpt();
} );

This updated fine but it's the same error when adding your code.

WP Show Post Version: 1.1.3
Generate Press Version: 3.1.3
Wordpress Version: 6.0

Can you double check the password - as its being rejected for me :(

New password, I'm an idiot and missed a capital letter

Hi Lewis,

Thank you for sharing your credentials!

I modified something in your plugin file. Then, I added the code in your functions.php file.

Kindly check, and let us know if it’s working as expected.

This archived topic is closed to new replies.