Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Expressing WP’s the_content as a function and checking isset

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #30630
    Eamonn
    Member

    Hi guys, to jump straight in, I am trying to work with WP’s the_content() tag, expressed as a function.



    The above expresses the_content as $content and outputs the text fine. What I want, however, is to check whether there is any actual text within the_content() and change the container div’s styling accordingly.

    I have tried to do this with the following…


    echo "

    ";
    } else {
    echo "

    ";
    }
    ?>

    … but it has the effect of returning the content without any wrapping div (just

    tags), and then opening and closing an empty div afterwards:

    the_content text here

    Any help is, as always, greatly appreciated.

    P.S I tried doing this using a PHP string word count, but it would only return ‘0’ as the number of words…

    #77598
    Eamonn
    Member

    Ahh. Here’s the answer:

    http://digwp.com/2009/07/putting-the_content-into-a-php-variable/

    The final code looks like this:

    the_content();
    $content = ob_get_clean(); ?>

    echo "

    ";
    } else {
    echo "

    ";
    }
    ?>

    This now lets me apply margins, padding, etc. to a container only if it will be actually filled with content. Otherwise, empty div! Thanks Chris!

    #70491
    dustin
    Member

    You could also use the get_the_content() function to return the content of the post to a variable instead of having to initialize a buffer.

    <?php

    $content = get_the_content();

    if ($content != '') {
    echo '<div>' . $content . '</div>';
    }

    ?>
    #47180
    Eamonn
    Member

    Just saw this now – nice one!

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.