• Resolved Hedley

    (@hedley)


    OK, I’ve been trying to work this out myself through reading help pages and experimentation, but I’m growing frustrated, so I’ll ask for help.

    I am rebuilding a site for someone in which a small number of editors post articles from a large number of different writers. Most of those writers are NOT registered users, but a few are. As I want the real author of the article to show up on the post, not necessarily the creator of the post, I use custom fields to specify the writer (name=”author”, value=author’s actual name). Then, in my theme files, I replaced:

    <?php the_author_posts_link(); ?>

    with

    <?php if ( get_post_meta($post->ID, 'author', true) ) { ?><?php echo get_post_meta($post->ID, "author", $single = true); ?><?php } ?>

    So far so good. In any post that has a custom “author” field specified, the pseudo author is displayed instead of the post creator. However, in posts in which a pseudo author has not been specified, no author shows up on the post at all. This is understandable, since there is no code to call it, and I have not used a “then” statement of any sort to specify what happens “if” there us no custom key set.

    I could just populate the custom field on every post, but since it interests me, I’d like to learn how to do satisfy the if/then condition for the sake of learning it.

    I’ve tried to use this bit of code, but it seems not to work:

    <span>
    <?php
    $author2 = get_the_author();
    $values2 = get_post_custom_values("Author");
    if (empty ($values2)) {
    echo "By " . $author2;
    } else {
    echo "By " . $values2[0];
    }
    ?></span>

    Anyone have any useful feedback?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Untested but why not:

    <?php
    $author = get_post_meta($post->ID, "author", $single = true);
    if ( $author ) {
    echo $author;
    } else {
    the_author_posts_link();
    }
    ?>

    Thread Starter Hedley

    (@hedley)

    Thanks very much for the reply, MH, that seems to have done the job.

    With each struggle comes more enlightenment…

    HB

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘If/then condition in custom field usage’ is closed to new replies.