WordPress.org

Ready to get started?Download WordPress

Forums

variable google sidebar advert (3 posts)

  1. taglad
    Member
    Posted 6 years ago #

    I'm trying to make the sidebar advert on posts fit the content of the post. In other words, if the post is small, I might place one ad in the sidebar; or, if it's a big post, I would place two ads in the sidebar.

    Is there some way of doing this that's .... umm... simple ?

  2. haochi
    Member
    Posted 6 years ago #

    It doesn't seem like WordPress has such function out of the box that returns the length of a post (number of characters or words).

    You can use the Post Word Count plugin to get the number of words per post, but you will have to modified the plugin first.

    Open post-word-count.php, replace echo number_format($totalcount); with return $totalcount;

    Install and activate the plugin.

    Open your theme's sidebar.php, add the following code to wherever you want to second ad to show up.

    <?php
    if(mdv_post_word_count(true) >= 100){ //replace 100 with the desire number of words.
    ?>
    YOUR AD CODE HERE
    <?
    }
    <?>
  3. taglad
    Member
    Posted 6 years ago #

    Thanks for the suggestion Haochi, but I've solved it without using a plugin by using a Custom Field and evaluating that field within the post template.

    In the post, I have a custom field called "sidebarpost". If I think the post is smaller or bigger than my default ad, I give a value to this.

    In the template I use :

    <?php $sidebarpost=get_post_meta($post->ID, 'sidebarpost', $single = true);
    if ($sidebarpost == "small") {
    include("sidebaradvertsmall.php");
    } elseif ($sidebarpost == "button") {
    include("sidebaradvertbutton.php");
    } elseif ($sidebarpost == "none") {
    echo "";
    } else {include("sidebaradvertdefault.php");
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic