4

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.

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

1
  • Figured it out. It was a problem with the share post plugin. Had to disable the automatically add to post and use the manual insert option. May 21, 2011 at 20:57

3 Answers 3

2

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;
}
5
  • Thanks roman. now I have a new problem. The div block shows left aligned at the beginning of the <p> tags in the post. Is there a way to push this block to say 200px below and have the wordpress post to wrap around this ad? If I use margin-top: 200px, it leaves a whitespace. May 21, 2011 at 21:11
  • Well this time, css isn't enough. If you want to put an advertisement inside your actual content, you have to a) include the ad inside your post during edit with a shortcode or b) add a filter for the_content() which will explode the content in parts, add your ad and output the new content.
    – Roman
    May 21, 2011 at 21:20
  • @Sledge81 A new problem doesn’t belong into a comment. Please use Ask Question for this. Otherwise nobody else will find the solution. Pure CSS questions belong to StackOverflow tough …
    – fuxia
    May 21, 2011 at 22:01
  • ünsche Prepend the div.advertisement with another div.spacing and add .spacing{float:left;width:1px;height:200px}. div.advertisement needs clear:left then. I hesitate to change your answer so much.
    – fuxia
    May 21, 2011 at 22:05
  • Thanks toscho.. Your code worked. Will bear that in mind next time onwards. May 22, 2011 at 7:00
8

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.

0

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');
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.