0

I'm trying to create a function to display either a theme option or custom images based on user input.

So basically, it's a custom meta box. The user selects either a display header image OR they can upload one they want to use.

I've created a custom function to call later on the single page template. This is what I have so far.

//El display image
function el_display(){
    global $post;
    $eldisplay = get_post_meta($post->ID, '_cmb_el_theme', true);
    $elcustomdisplay = get_post_meta($post->ID, '_cmb_custom_image', true);

        if(isset($eldisplay)){
            echo '<img src="' . $eldisplay . '" />';
                }elseif(isset($elcustomdisplay)){
                echo '<img src="' . $elcustomdisplay . '" />';
            }else{
                echo '<img src="' . EL_IMG . 'mobile-development.jpg' . '" />';
            }
}

When I call the function later (using el_display(); ), there is no display image showing. I'm a little stuck and have been trying to fix this for hours.

Any help would be much appreciated, thanks!


UPDATE Ok, thanks to all for helping me out and pointing me in the right direction. I do have some code clean up to do and a possible all out change in code but I'll use this for now.

I got it to work with the following code.

//El display image
function el_display(){
    global $post;
    $eldisplay = get_post_meta($post->ID, '_cmb_el_theme', true);
    $elcustomdisplay = get_post_meta($post->ID, '_cmb_custom_image', true);

        if(!empty($eldisplay)){
            echo '<img src="' . $eldisplay . '" />';
                }elseif(!empty($elcustomdisplay) && isset($elcustomdisplay)){
                echo '<img src="' . $elcustomdisplay . '" />';
            }else{
                echo '<img src="' . EL_IMG . 'mobile-development.jpg' . '" />';
            }
}

Thanks again!

7
  • Check the page source to see if an <img> tag is sent to the browser. Then check to see if the src attribute is set to a valid image path. Commented Aug 10, 2013 at 23:07
  • 1
    Relying on global $post is a bit risky in my opinion. (I'd pass the $post data into the function explicitly.) Is that variable set correctly?
    – s_ha_dum
    Commented Aug 10, 2013 at 23:18
  • I second the above. Also, you will want to use empty rather than isset in the conditional. Commented Aug 11, 2013 at 0:11
  • @CharlesClarkson - I just checked the source and an image tag is set however there is no source attribute or valid image path. I checked the database and the image path is there but not displaying in the source.
    – Jerry R.
    Commented Aug 11, 2013 at 3:19
  • @s_ha_dum - Sorry but I'm not sure I understand (sorry noob question). Do you mean to run a new WP query in the function?
    – Jerry R.
    Commented Aug 11, 2013 at 3:25

1 Answer 1

1

UPDATE Ok, thanks to all for helping me out and pointing me in the right direction. I do have some code clean up to do and a possible all out change in code but I'll use this for now.

I got it to work with the following code.

//El display image
function el_display(){
    global $post;
    $eldisplay = get_post_meta($post->ID, '_cmb_el_theme', true);
    $elcustomdisplay = get_post_meta($post->ID, '_cmb_custom_image', true);

        if(!empty($eldisplay)){
            echo '<img src="' . $eldisplay . '" />';
                }elseif(!empty($elcustomdisplay) && isset($elcustomdisplay)){
                echo '<img src="' . $elcustomdisplay . '" />';
            }else{
                echo '<img src="' . EL_IMG . 'mobile-development.jpg' . '" />';
            }
}

Thanks again!

2
  • Why did you check to see if $elcustomdisplay was set (isset($elcustomdisplay)), but not do the same for $eldisplay. I'm not saying either way is right or wrong. I'm just curious why you did it on one, but not on the other. Commented Aug 11, 2013 at 18:03
  • I ended up changing that, thanks for the suggestion.
    – Jerry R.
    Commented Sep 19, 2013 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.