Support » Plugins » Conditional comments for the length of a post

  • I would like to include extra content alongside each post based on the length of the post, i.e. if a post is under 500 characters, show a short banner. If it’s over 500, show a medium size banner. And if it’s over 1000 characters, show a long banner, etc.

    My site is . I plan to put this additional content in the <dl> to the left of each post – so it will be in the Loop. I’m okay with conditional comments for pages, sidebars etc, i.e. <?php if(is_single())?> etc, but I don’t know how to make conditional comments work via queries against the length of a post.

    I think this would make quite a handy plugin if it had a simple interface for choosing the character increments (i.e. 500 words) and the content rendered at each increment, especially if it’s implemented in the traditional sidebar via a widget…or maybe this plugin exists and I just don’t know about it!?!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Orin


    On my WordPress site, I’ve borrowed some code from The Engineered Boulderer’s Word Count Plugin. Define this function once anywhere before you want to use it:

    function word_count($display = false) {
        global $wpdb, $id;
    	$post = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
    	$post_content = apply_filters('the_content', $post);}
    	$words = trim(strip_tags($post_content));
    	if( $words == '' ) {
    		$wordcount = 0;
    	} else {
    		$words = explode(' ', $words);
    		$wordcount = count($words);
    	if( $display ) {
    		echo $wordcount; }
    	else {
    	return $wordcount; }
    } ?>

    I use it to customize the size of the font, depending on how long the post is. Shorter posts get a lot more balance that way. I’m sure you could adapt my code for your purposes:

    <?php if(function_exists('word_count')) {
    	$words = word_count();
    	if (200<$words && $words<=240) echo 'style="font-size:98%"';
    	elseif (240<$words && $words<=285) echo 'style="font-size:97%"';
    	elseif (285<$words && $words<=360) echo 'style="font-size:95%"';
    	elseif (360<$words) echo 'style="font-size:94%"';

    @orin: thanks! Very clear explanation and code.



    Wow. Neat idea.

    I would like to try making different sidebars depending on length of post. That would be pretty cool to help prevent a short post — long sidebar appearance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional comments for the length of a post’ is closed to new replies.