Adding a div class or id inside the_content()

I’m trying to create an advertisement block to be placed on a single post (single.php) such that the div class or id is left aligned and the post content wrapped around it. I checked the single.php and this is the only piece of code I notice is what displays the post.

<div class="entry entry-content">
                <?php the_content(); ?>
                <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'themejunkie' ), 'after' => '</div>' ) ); ?>

I tried adding the code both above the ‘entry-content’ div and also after the_content, but with both methods it either displays the div either at the beginning or at the end of the post content.

Read More

Can someone tell me which files to look into in order to add this?

Related posts

Leave a Reply

3 comments

  1. You have to put your advertisement block just before <?php the_content(); ?> in a separate div-layer and add some css to it. E.g.

    single.php

    <div class="entry entry-content">
        <div class="advertisement">
            <p>Your advertisement</p>
        </div>
    
        <?php the_content(); ?>
    </div>
    

    CSS

    div.advertisement {
        float: left;
        width: 150px;
        padding: 0px 10px 10px 0px;
    }
    
  2. Why not just hook into the the_content filter hook, and append your code to the_content()? e.g.:

    function mytheme_content_ad( $content ) {
        $myadcode = '<div class="someclas">';
        $myadcode .= 'some string with the ad code';
        $myadcode .= '</div>';
    
        $filteredcontent = $myadcode . $content;
    
        return $filteredcontent;
    }
    add_filter( 'the_content', 'mytheme_content_ad' );
    

    You may need to make the function a bit fancier than this, but this should convey the general idea.

  3. Using the example to add classes to p tag & h2 tag.

    <?php
    function replace_content($text_content)
    {
        if (is_page()) {
            $text = array(
                '<p>' => '<p class="text-danger">',
                '<h2>' => '<h2 class="h2">',
            );
        }
        $text_content = str_ireplace(array_keys($text), $text, $text_content);
        return $text_content;
    }
    add_filter('the_content', 'replace_content');
    ?>