admin管理员组

文章数量:1291102

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?

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?

Share Improve this question asked May 21, 2011 at 20:41 swordfish81swordfish81 5663 gold badges12 silver badges32 bronze badges 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. – swordfish81 Commented May 21, 2011 at 20:57
Add a comment  | 

3 Answers 3

Reset to default 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;
}

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.

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

本文标签: postsAdding a div class or id inside thecontent()