1

Basicly what I'm trying to do (with very basic knowledge of PHP):

If Custom Field $randomname exists for that particular post, show the content of that custom field.
If Custom Field $randomname doesn't exist for that particular post, show something else - for example: <div class="name">content</div>

Any help is highly appreciated.

4

Inside the loop, you can check the custom field with get_post_meta. Like this.

If custom field randomname exists then it will show it's value otherwise it will print <div class="name">content</div>

<?php

    if ( get_post_meta( $post->ID, 'randomname', true ) ) {

        echo get_post_meta( $post->ID, 'randomname', true );

    } else {

        echo '<div class="name">content</div>';

    }

?>

OR you can use below which is shorter version of above using Ternary Operator

    echo get_post_meta( $post->ID, 'randomname', true ) ?  get_post_meta( $post->ID, 'randomname', true ) :  '<div class="name">content</div>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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