• hey, everyone

    i’m using custom fields to add a name of a image.

    but some of my posts don’t have this custom field.

    i’d like to know if i can create a function, something like

    if ( custom_field != null ) :

    i tried some things that haven’t worked… maybe because the custom field even exist on some posts

Viewing 8 replies - 1 through 8 (of 8 total)
  • $value = get_post_meta($post_id, 'keyname', true);
    if ($value) dostuff('here');

    this is perhaps nicer… but not much:

    $value = get_post_meta($post_id, 'keyname', true);
    if (!empty($value)) dostuff('here');

    Ivovic’s solution has really helped me, so thank you!

    I’ve ended up using this, if it helps anyone:

    <?php $image = get_post_meta($post->ID, 'image', true); ?>
    <?php
    if (!empty($image)) {
    echo '<img alt="" src="'.$image.'" />';
    }
    ?>

    What about this? I want to use custom fields for displaying thumbs. But also, I want to have a default image placed somewhere inside my theme’s images folder which will be displayed when no image is attached to the post. How do I do it? I already posted this questions on more than few pages but there was no answer. Thanks in advance.

    I am trying to something a little similar and JoshuaGoodwin’s code didn’t seem to be doing it for me — here is what I’m trying to do.

    I have a custom field called “Author” associated to posts (because on my blog, sometimes we have submissions sent in, which we post and want to attribute to the author) — but other times we just want to show who the actual WP author is — here is what I have and I’m getting an error with my syntax and I can’t seem to lick what the issue is — here’s the code:

    <?php $author = get_the_author(); ?>;
    <?php $values2 = get_post_custom_values("Author");
    echo "By " . $values2[0];
    <?php if (!empty($values2)) {
    echo "By " . $author[0]; />';
    }
    ?>

    I figured out what I needed to do — thanks, me!

    Obviously, when you create a post in the WP Dashboard, underneath the editor there’s a section for Custom Fields. You want to add a new key called “Author” (sans quotes) and the value will be whatever the name of the author is.

    Now within your theme (in your single.php or index.php, just below ‘the_title’ you want to add the following:

    <span>
    <?php
    $author2 = get_the_author();
    $values2 = get_post_custom_values("Author");
    if (empty ($values2)) {
    echo "By " . $author2;
    } else {
    echo "By " . $values2[0];
    }
    ?></span>

    What the above code does is display “By Author Name” (the author’s name from the Custom Field that you input.) If there is nothing in there, then it will just use the actual WP admin’s name of who posted the article.

    Hope that helps!

    I would love to test to see if a key value is set across all posts (or at least within a category). I have a custom field named State (for U.S. State) and I want to query whether or not any posts with the state TN exist. How’s the best way to go about this?

    Use a SQL query directly on your database.

    In your PHPMyAdmin (I access it through CPanel) use something like this:
    SELECT * FROM wp_postmeta WHERE metakey=’State’ and meta_value=’TN’

    You will be able to see the IDs of the posts.

    https://www.w3schools.com/sql/sql_where.asp
    https://www.w3schools.com/sql/sql_and_or.asp

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘if custom field != null’ is closed to new replies.