WordPress.org

Forums

Non-linking posts (5 posts)

  1. alastc
    Member
    Posted 10 years ago #

    Is there anything that I can test / hook into to see if there is content in a post?

    That might sound a bit strange, but there are occasions when I just have an excerpt and a link to a permantent (non-wordpress) item of content. For example, on http://www.ukwindsurfing.com there is an item "South West Cup - FW Report", which links to a non-wordpress page.

    I'd like to do a "If (content == "") but that doesn't seem to work.

    Does anyone know of a way of checking for content?

  2. Michael Adams (mdawaffe)
    Member
    Posted 10 years ago #

    Try

    <?php
    if ( '' == $post->content ) {
    // do something special for blank content
    } else {
    the_content();
    }
    ?>

  3. alastc
    Member
    Posted 10 years ago #

    Hi mdawaffe,

    Thanks for replying, unfortunately I can't get that to work.

    Everything seems to match the 'have content' condition, although I've checked in the database, and a test post with no content shows none in the DB:
    mysql> select post_content from wp_posts where id='10';
    +--------------+
    | post_content |
    +--------------+
    | |
    +--------------+

    I'm not familiar with the -> notation, is that for selecting items from the database? In which case, wouldn't it be:
    if ( '' == $wp_post->post_content )

    That doesn't work either, so I guess not!

    Any other possibilites?

  4. Michael Adams (mdawaffe)
    Member
    Posted 10 years ago #

    Ah - that's because there's a typo.

    <?php
    if ( '' == $post->post_content ) {
    // do something special for blank content
    } else {
    the_content();
    }
    ?>

    The -> is PHP syntax. It tells the program to look at the post_content property of the $post object. See the PHP docs.

  5. alastc
    Member
    Posted 10 years ago #

    Ah-ha! That worked a treat :)

    Basically (for anyone else that wants to have posts with just excerpts and no link), you need something like this:

    <?php if ( '' == $post->post_content ) {
    // for posts without any content
    ?>
    <h2 id="post-<?php the_ID(); ?>">
    <?php the_title(); ?>
    </h2>
    <? } else {
    // For posts with content, and therefore a link
    ?>
    <h2 id="post-<?php the_ID(); ?>">" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></h2>

    <p class="date">Posted: <?php the_time('jS F Y') ?>, in <?php the_category(', ') ?><?php edit_post_link('Edit', ', (', ')'); ?>.<!-- by <?php the_author() ?> -->

    <?php }
    // Finish heading bit, excerpt would go after this.
    ?>`

    Thanks mdawaffe, I'll look into that syntax as well, very useful. (Just couldn't find it by putting "->" into a search engine).

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.