Forums

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

Home Forums Back End WordPress – If Statement

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #38791
    chrisburton
    Participant

    I am using a custom meta box to post a description however, it outputs

    when there is no content. How can I write an if statement so if there is content, it will post, otherwise output nothing?

    // usually needed
    global $custom_metabox;
    // get value directly
    $custom_metabox->the_value('description');
    ?>

    #105489
    SgtLegend
    Member

    Try this

    
    
    global $custom_metabox;

    if (($value = $custom_metabox->the_value('description')) !== '') {
    echo '

    ' . $value . '

    ';
    }

    ?>
    #105490
    chrisburton
    Participant

    @SgtLegend It still outputs

    #105491
    SgtLegend
    Member

    See what the output value is by using the following as it sounds like white space may be present in the value.

    echo 'Value: ' . $custom_metabox->the_value('description');
    #105492
    chrisburton
    Participant

    @SgtLegend That outputs the following

    Value: 
    #105493
    SgtLegend
    Member

    Sorry, forgot to wrap it in a function.

    echo 'Value: ' . strlen($custom_metabox->the_value('description'));
    #105494
    chrisburton
    Participant

    @SgtLegend

    Value: 0
    #105496
    chrisburton
    Participant

    @ChrisxClash, Ah I think that worked but how would I incorporate:

    #105501
    chrisburton
    Participant

    @ChrisxClas Exactly although it’s not working.

    #105504
    chrisburton
    Participant

    @ChrisxClash Nothing. It just outputs the text without the markup

    #105506
    chrisburton
    Participant

    @ChrisxClash Correct.

    #105511
    AntonNiklasson
    Participant

    Try this instead:

    
    $desc = $custom_metabox->the_value('description');

    if (!empty($desc)): ?>
    ".$desc."

    "; ?>
    #105513
    chrisburton
    Participant

    @AntonNiklasson That didn’t work either, unfortunately.

    #105532
    TheDoc
    Member

    If the one that Chis posted isn’t outputting the markup then I have a feeling like you’re updating the wrong file. Or perhaps your server is caching.

    #105546
    chrisburton
    Participant

    @ChrisxClash, @Jamy_za tried helping me out to no success at this time. I sent him my entire theme and what I’m using for my custom meta box (WPAlchemy).


    @TheDoc
    , I thought it was my cache also but I completely cleared it several times.

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