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

Home Forums Support [Resolved] Making 3-4 posts be at the top or archive, but different ones depending

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

Viewing 15 posts - 1 through 15 (of 37 total)
  • Author
    Posts
  • #1892708
    nomadiceman

    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?

    #1892719
    nomadiceman

    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

    #1892721
    Elvin
    Staff
    Customer Support

    Hi there,

    This has 2 parts depending on the behavior you want it to have.

    The first part is the WPSP you mentioned.

    The second part is only necessary if you don’t want duplicate posts to display.

    It’s this PHP snippet – https://generatepress.com/forums/topic/dispatch-theme-posts-showing-up-twice-on-homepage/#post-1888598

    This code checks the page and removes duplicate posts on the list if the WPSP you’ve added has the same post.

    A wise man once said:
    "Have you cleared your cache?"

    #1892725
    nomadiceman

    That is awesome!!!!

    That was my concern!

    Great. I will give that a try

    #1892735
    nomadiceman

    One question, what hook would you recommend?

    #1892818
    Elvin
    Staff
    Customer Support

    It depends on where exactly you want it to be placed.

    But normally this is hooked on generate_before_main_content or generate_after_header.

    But if you’re unsure, check this reference – https://docs.generatepress.com/article/hooks-visual-guide/

    A wise man once said:
    "Have you cleared your cache?"

    #1893991
    nomadiceman

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

    #1894109
    Elvin
    Staff
    Customer Support

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

    A wise man once said:
    "Have you cleared your cache?"

    #1899558
    nomadiceman

    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

    #1899577
    Elvin
    Staff
    Customer Support

    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.

    A wise man once said:
    "Have you cleared your cache?"

    #1899909
    nomadiceman

    this worked perfectly.

    Thank you again! Such a big help for my learning

    #1899912
    nomadiceman

    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

    #1899922
    nomadiceman

    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

    #1900376
    David
    Staff
    Customer Support

    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.

    #1900663
    nomadiceman

    Ok great I’ll try that

    To confirm. Does this replace Elvins code?

Viewing 15 posts - 1 through 15 (of 37 total)
  • You must be logged in to reply to this topic.