Support » Fixing WordPress » Custom Field Conditionals

  • Resolved cadencehart


    Hi! I hope you can help. I am using More Fields to setup custom fields for posts on a certain category. The first field outputs the image url and the second field outputs the amazon link.

    I added a conditional that if a particular custom field is empty, it should output nothing.

    My code is below:

    // Add Book Image to Books and Excerpts Page
    add_action( 'genesis_before_post_content', 'appendBookCover' );
    function appendBookCover() { ?>
    	<?php $bookcover = get_post_custom_values('book-cover'); ?>
    	<?php if (!empty($bookcover) && in_category('9')) {
    		echo '<img src="';
    		echo meta('book-cover');
    		echo '" style="float:left;padding:5px 15px 15px 0;" class="books" />';
        } else {
    		echo '';
        } ?>
    <?php }
    // Add Amazon Button to Books and Excerpts Page
    add_action( 'genesis_post_content', 'appendAmazon' );
    function appendAmazon() { ?>
    	<?php $amazon = get_post_custom_values('amazon-link'); ?>
    	<?php if (!empty($amazon) && in_category('9')) {
    		echo '<a href="';
    		echo meta('amazon-link');
    		echo '"><img src="" style="float:right;clear:both;" class="books" /></a>';
    	} else {
    		echo '';
        } ?>
    <?php }

    The code above works great if you haven’t set any value for a field yet. However, if you have already entered a value, saved the post, then later changed your mind and deleted the field value — it no longer works.

    It still outputs data as if you have specified a value to the field although you have already emptied it.

    What modifications should I do to the code to properly implement this? Thank you. Your help is appreciated.

  • The topic ‘Custom Field Conditionals’ is closed to new replies.