[Support request] Wants to Add different Blocks on Different Category

Home Forums Support [Support request] Wants to Add different Blocks on Different Category

Home Forums Support Wants to Add different Blocks on Different Category

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2262851
    Kartik

    Hi,
    I am working on https://shivasportsnews.com/ website

    Wants 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 block

    how to do it

    #2262866
    Fernando
    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.

    #2262879
    Kartik

    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

    #2262886
    Fernando
    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!

    #2262990
    Kartik

    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.

    #2262999
    Fernando
    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/jkuXLwJO

    Then, use a custom hook called middle_hook as such: https://share.getcloudapp.com/yAu1DEor

    Hope this helps!

    #2263241
    Kartik

    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)

    #2263403
    David
    Staff
    Customer Support

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

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