- This topic has 7 replies, 3 voices, and was last updated 1 day, 14 hours ago by
David.
-
AuthorPosts
-
June 23, 2022 at 8:37 pm #2262851
Kartik
Hi,
I am working on https://shivasportsnews.com/ websiteWants to Add different Blocks on Different Category on content after 1 paragraph, middle paragraph and last after content
Likes for one category – Type 1 block
for second category – Type 2 blockhow to do it
June 23, 2022 at 9:17 pm #2262866Fernando Customer Support
Hi Kartik,
To clarify, are you wanting to add 3 different Block Elements to Posts of different Categories? For instance, this Post: https://shivasportsnews.com/australia-vs-bulgaria-mens-vnl-2022-live-stream-today-game/
Or, are you wanting to add 3 different Block Elements to the Category pages themselves? For instance, this page: https://shivasportsnews.com/volleyball/
Kindly let us know.
June 23, 2022 at 10:04 pm #2262879Kartik
this kinds of Post but its only for Volleyball category: https://shivasportsnews.com/australia-vs-bulgaria-mens-vnl-2022-live-stream-today-game/
Other three blocks for different category are different
June 23, 2022 at 10:39 pm #2262886Fernando Customer Support
I see. Thank you for clarifying.
If you add this PHP snippet, you’ll be able to have a hook available to hook onto after the first paragraph and after the content:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook_name']); return ob_get_clean(); }); add_filter( 'the_content', 'insert_my_element', 20 ); function insert_my_element( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_first_paragraph_and_content"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 1, $content ) . $inserted_hook; } return $content; } function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); } add_filter( 'generate_hooks_list', function($hooks){ $hooks['content']['hooks'][] = 'after_first_paragraph_and_content'; return $hooks; }, 99,1 );
Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets
Example: https://share.getcloudapp.com/YEukRE7w
The hook name would be after_first_paragraph_and_content
With regards to the middle part, you’ll need to add the shortcode manually through a Shortcode Block.
The shortcode is:
[portable_hook hook_name="after_first_paragraph_and_content"]
Example: https://share.getcloudapp.com/Koujd2xq
So, basically, you’ll need two Block Elements for the two categories.
Then set the display location as Post Category, and select the appropriate category: https://share.getcloudapp.com/04uQAwmy
Hope this clarifies!
June 24, 2022 at 2:02 am #2262990Kartik
Thanks for providing code but issue is in both After First Paragraph and Last content both place showing same thing i want different one for both.
June 24, 2022 at 2:25 am #2262999Fernando Customer Support
Oh I forgot about the 3 different Block Elements.
You may modify your code into something like this:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook_name']); return ob_get_clean(); }); add_filter( 'the_content', 'insert_my_element', 20 ); function insert_my_element( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_first_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 1, $content ); } return $content; } function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); } add_filter( 'generate_hooks_list', function($hooks){ $hooks['content']['hooks'][] = 'after_first_paragraph'; return $hooks; }, 99,1 );
This will add
after_first_paragraph
hook.Then for the end of the content, you can just use after_entry_content hook.
Lastly, for the middle, you’ll need to add this new PHP snippet for a new portable hook:
function your_shortcode($atts, $content = null) { ob_start(); do_action(‘middle_hook’); return ob_get_clean(); } add_shortcode(‘middle_shortcode’, 'your_shortcode');
You should be able to use
[middle_shortcode]
as such: https://share.getcloudapp.com/jkuXLwJOThen, use a custom hook called
middle_hook
as such: https://share.getcloudapp.com/yAu1DEorHope this helps!
June 24, 2022 at 7:26 am #2263241Kartik
Thanks Its working Fine.
Sorry to disturb you.. can any code to place item after first two paragraph
(We got code- after first paragraph on content can possible after two paragraph what’s coding for it)June 24, 2022 at 7:58 am #2263403David
StaffCustomer SupportHi there,
in the code that Fernando provided look for this line:
return prefix_insert_after_paragraph( $inserted_hook, 1, $content );
And change it to:
return prefix_insert_after_paragraph( $inserted_hook, 2, $content );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.