WordPress.org

Ready to get started?Download WordPress

Forums

Help creating functions & shortcodes (4 posts)

  1. mayb101
    Member
    Posted 3 years ago #

    Hi!

    Thank you in advance for checking out my post.

    I am wondering if anyone can help me with creating a function and shortcode.

    I set up a custom field in my posts called smurl.

    This is what I would like to display in my loop.

    <div class="smurl_div"><input type="text" class="smurl_box" value="<?php $key="smurl"; echo get_post_meta($post->ID, $key, true); ?>"></div>

    However, I would like it to only display if the field has a value.
    If it is null, I would not like it to display.
    At present, if it is null, the div and input still displays (obviously).

    I would also like to be able to have code for the functions.php of my theme so that I can place the value of smurl in the loop using:

    <?php get_smurl();?>
    and a shortcode[smurl] for within posts

    And so I can place the code

    <div class="smurl_div"><input type="text" class="smurl_box" value="<?php $key="smurl"; echo get_post_meta($post->ID, $key, true); ?>"></div>

    in the loop using:

    <?php get_smurl_div();?>
    and a shortcode[smurl_box] for within posts

    Is this possible? I am still learning so any help is greatly appreciated.

    Thank you.

  2. superyetkin
    Member
    Posted 3 years ago #

    Use this:

    <div class="smurl_div"><input type="text" class="smurl_box" value="<?php $key="smurl"; $res = get_post_meta($post->ID, $key, true); if ($res != "") echo $res; ?>"></div>

    Donate if this helps you.

  3. mayb101
    Member
    Posted 3 years ago #

    Thanks for replying. That may help. I'll see what I can do with it.

    But for the second instance, it's the whole DIV itself that needs to be wrapped in an IF statement of some kind e.g.

    if smurl is not null
    display <div>...</div>
    else
    don't display anything

    No problem donating something if I can get this to work.

  4. thefatguy
    Member
    Posted 3 years ago #

    This is what I do to test for null values:

    <?php if ( !empty( $smurl ) ) : ?>
    ...do something, like <div> etc...
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic