• Is there a plugin that will allow you to insert custom HTML based in post category? I want to insert a block of text to groups of posts, say a “Tip” or a “Idea” or “Warning.” But I don’t want to insert a shortcode in EACH post. I want to write the custom text for each category and have WordPress add it automatically to all posts in that category.

    I’ve searched for plugins but can’t find any that does this. 🙁

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter wpgator

    (@wpgator)

    Oh, and since I already have custom post footer text (text that appears at the end of each post), I want this other custom text to appear elsewhere. Specifically, I want it to appear IN the post, like maybe after 1-2 paragraphs.

    Thread Starter wpgator

    (@wpgator)

    Hmm. Looks like I found something that works. Anybody want to comment? I don’t know what half of it means. I copied and modified the code from this page http://www.wpbeginner.com/wp-tutorials/how-to-insert-ads-within-your-post-content-in-wordpress/

    Seems to work for me initially.

    //Makes the next part work.
    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 );
    }
    
    //Insert ads after second paragraph of single post content.
    
    add_filter( 'the_content', 'prefix_insert_post_custom' );
    function prefix_insert_post_custom( $content ) {
     $ad_code = '<div>Custom text here</div>';
    if ( in_category('0') && is_single() ){
    return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }
    return $content;
    }

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Inserting custom text into posts by category?’ is closed to new replies.