Yury Zvekov
Yury Zvekov
JAMstack developer

Add custom block to Single.php in Wordpress

28 September 2020

function calc_in_single( $content ) {
    if( !is_single() )
    return $content;
    $paragraphAfter = 1; // Number of paragraph
    $content = explode ( "</p>", $content );
    for ( $i = 0; $i < count ($content)-1; $i ++ ) {
        if ( $i == $paragraphAfter ) {
          ?>
         
         <?php echo do_shortcode( '[post_calculator]' ); ?>

    <?php
    }
    echo $content[ $i ] . "</p>";
    }
}
add_filter( 'the_content', 'calc_in_single' );

/* comment line
 the_content();
*/
<?php 
    $paragraphAfter = 1; // Number of paragraph
    $content = explode ( "</p>", $content );
    for ( $i = 0; $i < count ($content)-1; $i ++ ) {
        if ( $i == $paragraphAfter ) {
          ?>
         
         <?php echo do_shortcode( '[post_calculator]' ); ?>

    <?php
    }
    echo $content[ $i ] . "</p>";
    }
 // end loop
Share