WordPress.org

Forums

[resolved] get_the_author_meta conditional display (3 posts)

  1. crookeddesigns
    Member
    Posted 2 years ago #

    I've run into a bit of a snag with a theme I'm developing. I've created a custom user profile field called "occupation" (using the tutorial located on Justin Tadlock's blog), when the user comments on a post I want to display the following specific information about the user commenting:

    If the person's occupation is set to "alpha" display:
    I am an alpha, my job consists of A, B & C.

    If the person's occupation is set to "beta" display:
    I am a beta, my job consists of D, E & F.

    If the person's occupation is set to "gamma" display:
    I am a gamma, my job consists of G, H & I.

    If the person is none of these, display nothing.

    I can get the users occupation to display by simply using:
    <?php get_the_author_meta('occupation'); ?>

    If I try to create a conditional statement, nothing shows up. Here's what I tried:

    <?php $occupation = get_the_author_meta('occupation');
    if($occupation == 'alpha') { ?>
    I am an alpha, my job consists of A, B & C
    <?php } elseif($occupation == 'beta') { ?>
    I am a beta, my job consists of D, E & F
    <?php } elseif($occupation == 'gamma') { ?>
    I am a gamma, my job consists of G, H & I
    <?php } else { ?>
    <?php } ?>

    The only way I can get anything to display is by changing
    if($occupation !== 'value')
    but by doing that I end up with all of the values showing up for every occupation type so after every comment it spits out: I am an alpha... I am a beta... I a gamma... one after the other.

    Obviously there has to be a better way of doing this. Any suggestions and help would be greatly appreciated!

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    are you definitively sure what is in your get_the_author_meta('occupation');
    i.e. capitalisation, spelling, spaces?

    maybe if you apply a trim() and strtolower() ...

    I can get the users occupation to display by simply using:
    <?php get_the_author_meta('occupation'); ?>

    i would have assumed you need to use an echo in that code (?)

  3. crookeddesigns
    Member
    Posted 2 years ago #

    Thanks for your reply! I wasn't able to get it to work using that method, however this worked just fine:

    <?php $occupation = get_the_author_meta('occupation');
    if($occupation == 'alpha') { ?>
    I am an alpha, my job consists of A, B & C
    <?php } wp_reset_query(); if($occupation == 'beta') { ?>
    I am a beta, my job consists of D, E & F
    <?php } wp_reset_query(); if($occupation == 'gamma') { ?>
    I am a gamma, my job consists of G, H & I
    <?php } wp_reset_query(); else { ?>
    <?php } ?>

    I'm sure there's a cleaner way of doing it without multiple query resets, but that worked like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic