• I am using a conditional statement to check if a custom field has a value with a verve meta box plugin using a check box.

    I am using this but it isnt echoing my ‘widerColumn’ class.

    <?php if (get_post_meta($post->ID, 'wider_text_column', true) ) : ?>
    <?php echo 'widerColumn'; ?>
    <?php endif; ?>"

    I have checked the name of custom field and it is correct. Hep appreciated

Viewing 3 replies - 1 through 3 (of 3 total)
  • Have you tried printing get_post_meta($post->ID, 'wider_text_column', true); to confirm you actually have a value for that custom field for the post?

    Thread Starter wireframe74

    (@wireframe74)

    I got this working with is page…

    <div class="faq <?php
    
    if ( is_page (array('privacy-policy','index',)) ){    
    
        $margin= 'widerColumn"'; 
    
    echo $margin; 
    
    } 
    
    ?>

    but ideally I need this to be determined by custom field so that its content managed and not hardcoded.

    Thread Starter wireframe74

    (@wireframe74)

    Actually I found a handy link here that sorted this all very easily by defining a new function:

    https://www.nathanrice.net/blog/an-easy-way-to-get-the-contents-of-a-custom-field/

    Just added this to functions.php

    function get_custom_field($key, $echo = FALSE) {
    	global $post;
    	$custom_field = get_post_meta($post->ID, $key, true);
    	if ($echo == FALSE) return $custom_field;
    	echo $custom_field;
    }

    and this to my template where I needed this applied.

    <?php if(get_custom_field('dark_background')) { ?>
        class="darkerBackground"
    	<?php }  ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditonal Gget_post_meta’ is closed to new replies.