Site logo

Archived topic

Making 3-4 posts be at the top or archive, but different ones depending

36 replies · Started by nomadiceman on August 11, 2021

Viewing posts 1–15 of 37

Is there a way to select say 2-4 blog posts and have them stick at the top of a category.

But be able to also select different blog posts for the other categories and also the tags and author archives?

I'm thinking its probably best to create a element hook with WPSP in and select the posts I want

But was asking in case there is a better way

That is awesome!!!!

That was my concern!

Great. I will give that a try

One question, what hook would you recommend?

How would I go about making this only show on page 1 of the archive?

You'll have to filter the display of the hooked element.

Say, for example, you have a hook element, you can use generate_header_element_display.

Example: (this assumes this hook element is used only on archive pages w/ pagination)

add_filter( 'generate_header_element_display', function( $display, $element_id ) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ( 123 === $element_id ) {
        if( 1 == $paged ){
            $display = true;
        }  else {
            $display = false;
        }
    } 

    return $display;
}, 10, 2 );

Hi Elvin,

So everything seems to work great apart from getting the Block Element Hook to only show on the first page of the relevant archive

Ive sent you the URL of the Block element hook with the WPSP in.

Please can you advise me on what I need to add or change to the above code to turn off that element for the pages 2 onwards

If it's a block element, use generate_block_element_display filter instead.

Example:

add_filter( 'generate_block_element_display', function( $display, $element_id ) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ( 123 === $element_id ) {
        if( 1 == $paged ){
            $display = true;
        }  else {
            $display = false;
        }
    } 
    return $display;
}, 10, 2 );

Change 123 to the block element's ID.

this worked perfectly.

Thank you again! Such a big help for my learning

oops. I spoke to soon.

It now no longer shows on that tags archive page 2 onwards, but its now showing on all other pages

It should only be shown on the tag archive for the term Visa

The element is set to only display on that tag archive so I am lost why its showing on other spots now

Im not a developer so I am unsure, but I think you may need to know that I am using this snippet also given from one of your team

add_filter( 'generate_block_element_display', function( $display, $element_id ) {
    if ( 11587 === $element_id && !is_paged() && is_tag() ) { 
        $display = true;
    } 

    if ( 11587 === $element_id && is_paged() && is_tag() ) { 
        $display = false;
    }
    if ( 11953 === $element_id && !is_paged() && is_tag() ) { 
        $display = false;
    } 

    if ( 11953 === $element_id && is_paged() && is_tag() ) { 
        $display = true;
    }

    return $display;
}, 10, 2 );

I added that to the site so that pages 2 onwards of the tag pages have a different header.

I am guessing that's what's causing a conflict or something

Let me know your thoughts

Hi there,

to confirm - you have the Block Element, and you only want it displayed on Page 1 of Tag Archive Term you have specified in the Element Display Rules ?

If so you should only need to do:

add_filter( 'generate_element_display', function( $display, $element_id ) {
    if ( 12345 === $element_id && is_paged() ) { 
        $display = false;
    } 

    return $display;
}, 10, 2 );

Change the 12345 for the Element ID.
And this will simply tell it not to display on a paged view.

Ok great I’ll try that

To confirm. Does this replace Elvins code?

This archived topic is closed to new replies.