• 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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Thread Starter mayb101

    (@mayb101)

    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.

    This is what I do to test for null values:

    <?php if ( !empty( $smurl ) ) : ?>
    ...do something, like <div> etc...
    <?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Help creating functions & shortcodes’ is closed to new replies.