Support » Fixing WordPress » If and Else in custom field

  • Resolved taghaboy

    (@taghaboy)


    hi,
    is this lines true or false :

    <?php // if there's a url in my custom field
    if($value !== '') { ?>
    
    <p>
    <?php $values = get_post_custom_values("url"); echo $values[0]; ?>">
    </p>
    
    <?php } // end if statement
    
    // if there's no url
    else { echo '<b>sorry,no url</b>'; } ?>

    id like this to generat some things like this:
    if i write a url (any thing) in my custom field
    http://www.google.com
    if no url (data) in my custom field to print this
    sorry,no url(data)

    Thanks for advice.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter taghaboy

    (@taghaboy)

    hi all,
    any idea
    thanks

    Thread Starter taghaboy

    (@taghaboy)

    Hi all, I have the solution, so i share the love with you, even if the help her it’s so so hard šŸ™‚

    in this exemple i set a custom field with value “petit” who will print in my template the URL of my Thumbnail image, so the user forget to put the url in the custom field, the script will automatically print a URL to an image i set in the images folder named : no-img.jpg . Magic šŸ˜‰ in this way you saved the look of ur website and esigne too :

    <?php
    $values = get_post_custom_values("petit"); // set the img custom name
    if (isset($values[0])) { // if there's a img in my custom field
    	?>
    		<img src="<?php $values = get_post_custom_values("petit"); echo $values[0]; ?>" title="<?php the_title(); ?>"/>
    	<?php } // end if statement
    			// if there's no img do replace it with no-img
    	else { ?>
    		<img src="<?php bloginfo('stylesheet_directory'); ?>/images/no-img.jpg" title="<?php the_title(); ?>"/>
    	<?php } ?>

    Another way :

    <?php 	// set the img custom name + get the data of custom_values.
    if($value !== get_post_custom_values("petit")) {
    	?>
    			<!--Print the data of custom_values if exist, if not next line-->
    		<img src="<?php $values = get_post_custom_values("petit"); echo $values[0]; ?>" title="<?php the_title(); ?>"/>
    	<?php } // end if statement
    			// if there's no data in custom_values print this line code : url of no-img.jpg
    	else { ?>
    		<img src="<?php bloginfo('stylesheet_directory'); ?>/images/no-img.jpg" title="<?php the_title(); ?>"/>
    	<?php } ?>

    et Voila the latest all i have:

    <?php
    	$pattern = "/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/i";
    	preg_match_all($pattern, $post->post_content, $images);
    	if(!$images[1][0]) {?>
    		<img src='<?php bloginfo("template_url");?>/i/sorry-no-photo.png' alt='Sorry! No image was found.' width='75px' />
    	<?
    	}
    	else
    		echo "<img src='".$images[1][0]."' width='75px' />";
    ?>

    that’s all, i shared my lovely php code just for YOU.

    thx mate, your love helps me a lot.
    your code modified include the permalink at the image:

    <?php $values = get_post_custom_values("image"); // set the img custom name
    if (isset($values[0])) { // if there's a img in my custom field
    	?>
    		<a>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php $values = get_post_custom_values("image"); echo $values[0]; ?>" title="<?php the_title(); ?>"/></a>
    	<?php } // end if statement
    			// if there's no img do replace it with no-img
    	else { ?>
    		<a>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/no-img.gif" title="<?php the_title(); ?>"/></a>
    	<?php } ?>

    Thread Starter taghaboy

    (@taghaboy)

    Your welcome šŸ™‚

    what php does this go under and what line?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘If and Else in custom field’ is closed to new replies.