- This topic has 36 replies, 4 voices, and was last updated 2 years, 8 months ago by David.
-
AuthorPosts
-
August 11, 2021 at 5:22 pm #1892708nomadiceman
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?
August 11, 2021 at 5:37 pm #1892719nomadicemanI’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
August 11, 2021 at 5:46 pm #1892721ElvinStaffCustomer SupportHi 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.
August 11, 2021 at 5:59 pm #1892725nomadicemanThat is awesome!!!!
That was my concern!
Great. I will give that a try
August 11, 2021 at 6:24 pm #1892735nomadicemanOne question, what hook would you recommend?
August 11, 2021 at 9:13 pm #1892818ElvinStaffCustomer SupportIt 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/
August 12, 2021 at 4:02 pm #1893991nomadicemanHow would I go about making this only show on page 1 of the archive?
August 12, 2021 at 8:43 pm #1894109ElvinStaffCustomer SupportYou’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 );
August 17, 2021 at 9:15 pm #1899558nomadicemanHi 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
August 17, 2021 at 9:34 pm #1899577ElvinStaffCustomer SupportIf 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.August 18, 2021 at 4:17 am #1899909nomadicemanthis worked perfectly.
Thank you again! Such a big help for my learning
August 18, 2021 at 4:22 am #1899912nomadicemanoops. 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
August 18, 2021 at 4:26 am #1899922nomadicemanIm 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
August 18, 2021 at 8:18 am #1900376DavidStaffCustomer SupportHi 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.August 18, 2021 at 2:44 pm #1900663nomadicemanOk great I’ll try that
To confirm. Does this replace Elvins code?
-
AuthorPosts
- You must be logged in to reply to this topic.