• Resolved s0l1dsnak3123

    (@s0l1dsnak3123)


    Hi there, I want to ad advertisements to posts on my wordpress blog, but only if they are on the index, and only if they are larger than a certain number of chars. What is the best way to do this?

    currently I have this code:

    <div class="entry">
       <?php
          advman_ad('Medium Rectangle');
          the_content('Read the rest of this entry &raquo;')
       ?>
    </div>

    I have attempted to use PHP’s “strlen” on the_content, but it gave me a result of 0 and outputted the post twice.

    Your help would be strongly appreciated.

    Thanks in advance,
    John.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Print the strlen of the content…

    <?php
    $cont = strlen(get_the_content());
    echo $cont;
    ?>

    Would print the string length of the post.. Remember this will include any special characters, so a link <a href="https://www.example.com">Example</a> such as this would be a total of 44 characters…

    Howver you could count the characters minus the tags… like so..

    <?php
    $cont = strip_tags(get_the_content());
    $countchars = strlen($cont);
    echo $countchars;
    ?>

    Such code needs to be inside the loop of course…

    Then do switch based on string length..

    <?php if($countchars > 300) { ?>
     CODE HERE
    <?php } else { ?>
     CODE HERE
    <?php } ?>

    You can obviously remove the echo $countchars part, that’s just so you can see the count on the page while you test it.

    Of course test and adjust 300 to the desired number…
    > beside the 300 means “more than” , you can just as easily replace that for < which is “less than”…

    Hope that helps…

    Thread Starter s0l1dsnak3123

    (@s0l1dsnak3123)

    Thanks alot, I had figured out the first part after I wrote the original post, but I never considered the tags adding extra chars. Thanks for the heads up ??

    I didn’t think of it to begin with, but i printed the count onto the page, and something seemed off… then the ball dropped and i realised what i missed, lol…

    Happy to help.. ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘find the number of chars in a post?’ is closed to new replies.