Forums

if custom field != null (9 posts)

  1. fksr86
    Member
    Posted 4 years ago #

    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

  2. Ivovic
    Member
    Posted 4 years ago #

    $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');
  3. Joshua Goodwin
    Member
    Posted 3 years ago #

    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.'" />';
    }
    ?>
  4. boonika
    Member
    Posted 3 years ago #

    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.

  5. martyz
    Member
    Posted 3 years ago #

    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]; />';
    }
    ?>
  6. martyz
    Member
    Posted 3 years ago #

    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!

  7. taghaboy
    Member
    Posted 3 years ago #

  8. David Angel
    Member
    Posted 3 years ago #

    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?

  9. renato_s
    Member
    Posted 3 years ago #

    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.

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

Topic Closed

This topic has been closed to new replies.

About this Topic