1

I am trying to display the post title if the custom field exists and has a value of apple...

<?php if ( isset ( get_post_meta($post->ID, 'mycustomvalue', true ) ) ) && if ( get_post_meta($post->ID, 'mycustomvalue', true ) == 'apple' ) : ?>

<?php the_title(); ?>

I am getting a white screen, anyone spot where I am going wrong?

0

2 Answers 2

1

You can hook into the_title() to alter the output.

In functions.php paste this:

function my_change_title( $title, $post_id ) {
    $custom_title = get_post_meta($post_id, 'mycustomvalue', true);

    if( isset($custom_title) && $custom_title == 'apple' )
        return $custom_title;

    return $title;
}
add_filter('the_title', 'my_change_title', 10, 2);

In your template, you can just use <?php the_title(); ?>


To use your custom value in your template:

<?php $custom_value = get_post_meta(get_the_ID(), 'mycustomvalue', true); ?>
<?php if( isset($custom_value) && $custom_value == "apple" ) : ?>
    Do something
<?php endif; ?>
4
  • the_title was just being used as an example of some content being returned, I am not actually using the_title May 21, 2013 at 15:31
  • Edited my answer, hope this is what you need. May 21, 2013 at 15:36
  • Hi rob, with the first line, if mycustomvalue doesn't exist then won't it display a notice to that effect? May 21, 2013 at 15:43
  • Nope =) get_post_meta returns an empty string or empty array when nothing found and in the if statement you check if the var isset May 21, 2013 at 15:50
0

Because, in your code you didn't close the IF condition.

I have modified your code but NOT tested.

<?PHP
$post_meta = get_post_meta(get_the_ID(), 'mycustomvalue', true);
if( !empty( $post_meta ) && $post_meta == 'apple'){
    the_title();
}
?>

NOTE: if you don't want pass the third parameter, you will get the result as an array and you have to change your code from !empty( $post_meta ) to count( $post_meta ) > 0

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