• So I ventured into the custom field function and it was easier than I thought!

    I made a custom field to post an image in the sidebar of posts. I put this code in my sidebar

    <img src="<?php echo get_post_meta($post->ID, 'side-image', true); ?>" #<?php the_ID(); ?>" />

    It works but for example in a post/page where I don’t upload an image, I get the default image broken/unavailable icon.

    Is there a code I can add to tell not to display anything if there is no input in the custom-field?

Viewing 11 replies - 1 through 11 (of 11 total)
  • You just need to create a condition for the data…

    Something like…

    <?php $i = get_post_meta( $post->ID , 'side-image' , true ); ?>
    <?php if( $i ) : ?>
    <img src="<?php echo $i; ?>" #<?php the_ID(); ?>" />
    <?php endif; ?>

    Thread Starter virgild

    (@virgild)

    Thanks for the code! Would this be correct?

    <a href="<?php the_permalink() ?>" rel="bookmark">
    <?php $i = echo $theme_image2; ?>&h=100&w=293&zc=1" alt="" width="293" height="100" />
    <?php if( $i ) : ?>
    <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $theme_image2; ?>&h=100&w=293&zc=1" alt="" width="293" height="100" /></a>

    The original code without conditions is

    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $theme_image2; ?>&h=100&w=293&zc=1" alt="" width="293" height="100" /></a>

    Could you further explain what you’re trying to modify into using a custom field (assuming that’s what you’re doing), rather then showing me your attempts at intergrating it (not meaning to sound rude there), this way i can give you a better tailored example (which will essentially just be me modifying/fixing what you give me).

    I assume what you want.. is to remove the code’s dependancy on the data stored in $theme_image2 (whatever that is), and replace it with data pulled from a custom field…. would that be correct?

    Thread Starter virgild

    (@virgild)

    Sorry for not explaining. You’re not rude at all! I’m not even sure what I’m really doing but it works.

    The code is for a custom theme option I’m using to display images in my blog.

    It calls this function.

    array(  "name" => "Box image",
    	"desc" => "Enter image path for the text box",
    	"id" => $shortname."_image2",
    	"std" => "",
    	"type" => "text"),

    so I’m placing this

    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $theme_image2; ?>&h=100&w=293&zc=1" alt="" width="293" height="100" /></a>

    in my page template where I want the image to appear. But when I don;t upload an image I get the broken image icon. so I’m tryin to use a sort of

    <?php else: ?>
    <?php endif; ?>

    php code to display nothing if there is no image path but I don;t know how to implement that.. I’m very new to php.

    I’d still need to see more of the associated code..

    The code you’ve referred to as a function is actually just data in an array, which on it’s own wouldn’t do anything, unless you’re saying that’s what’s in the $theme_image2 variable, which could make sense.

    Paste your complete template code in a temporary pastebin …

    https://wordpress.pastebin.ca/
    (and post a link back here for it)

    As far i can tell it’s just a case of not quite grasping the PHP side .. i’ll show you how to adopt my previous example once i see more of the code.

    Thread Starter virgild

    (@virgild)

    Thank you so much for trying to help me! I shouldn’t probably play with php until I learn the basics.. but it’s fun! ??

    here is the page template on which I have the codes. I’m trying to make my own theme with theme options.

    https://wordpress.pastebin.ca/1771031

    I have 3 text blocks on the bottom page similar to the ones in this theme

    https://www.curtziegler.com/sitedemo/portfolio/

    just that I have images under the titles.. and when there is no image uploaded I get that broken image icon..

    Thread Starter virgild

    (@virgild)

    I tried this.. I think I’m getting closer.. or going adrift lol

    <?php if ($_image1=="") {?>
    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $theme_image1; ?>&h=100&w=293&zc=1" alt="" width="293" height="100" /></a>
    <?php else : ?>
    none
    <?php endif; ?>

    but it doesn’t work ??

    What’s in these variables?

    $theme_image1;
    $theme_image2;
    $theme_image3;

    You need to check they have data before using them… (see basic example in pastebin code below).

    Now i’m slightly confused, before i was under the impression you wanted to replace the $theme_image calls with a value pulled from a custom field.

    Anyhow i’ve reformatted the code, and moved around a few bits… , see if this brings you closer to what you want to do, then we’ll work from there, one step at a time ?? …

    https://wordpress.pastebin.ca/1771940

    The only issue i have is that i can’t see what’s in your variables, so it will be some guess work on my part as to what bits you want to use/replace/change, the more specifics the better…

    Thread Starter virgild

    (@virgild)

    Thank you for the new code! To be honest I don’t understand most of it ?? I think I have to learn some basic php. May I ask one question tho?

    I don’t understand why you globalised the variables. I don’t have that code but the theme works. What does that do?

    If a variable is declared outside the scope of where you are calling it then you globalise the variable to get at the data… it’s not globalising in the same manner as “register_globals” which has been cause to alot of php security problems, but i’ve seen the connection made before, so thought i’d point it out.

    $a = 2;
    function example() {
    	echo $a;
    }

    The result would be empty, because the $a variable is outside the scope of the function..

    $a = 2;
    function example() {
    	global $a;
    	echo $a;
    }

    Now the result is 2, because the function has access to the variable..

    Perhaps not the best of examples, but it should illustrate the point.. ??

    Thread Starter virgild

    (@virgild)

    Thank you for teaching me that! Php seems pretty complex but it’s worth learning it! ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘I made a custom field! :-D but just one question..’ is closed to new replies.