WordPress.org

Ready to get started?Download WordPress

Forums

help with meta_value (11 posts)

  1. kenkelm
    Member
    Posted 4 years ago #

    Hi everyone,

    I'm trying to define a function based on a custom field. My custom field is named 'newsletter' and I'm just trying to create a function that gives 'true' if the page has the custom field and 'false' otherwise.

    Here's the function I've written so far:

    function is_newsletter() {
    global $post;
    if ('post_type=page&meta_value=newsletter') {
    return true;
    } else {
    return false;
    };
    };

    It's not working. I'm pretty new at this, so sorry about this.

    Can anyone help me with this?

    Thanks a lot!

    -Mary

  2. MichaelH
    Member
    Posted 4 years ago #

    The if ($link) tests if the field was returned.

    <?php
    $link = get_post_meta($post->ID, 'link_url', true);
    $text = get_post_meta($post->ID, 'link_text', true);
    if ($link){
    echo 'Credit: <a href="'.$link .'">'.$text.'</a>';
    }
    ?>

    Could also do

    if (get_post_meta($post->ID, 'link_url', true)) {
  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    There's already a function for doing that.
    http://codex.wordpress.org/Function_Reference/get_post_meta

    False on fail
    Value on success

    A check to see if the field if present could look like.

    $my_meta = get_post_meta( $post->ID , 'fieldname' , true );
    if( $my_meta ) {
    // It's true
    }
    else {
    // It's false
    }
  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    I really should refresh the page before hitting submit.

  5. kenkelm
    Member
    Posted 4 years ago #

    Thanks so much for the responses!

    I'm sorry, but I'm pretty dense about this stuff!

    I've changed my function to:

    function is_newsletter() {
    $news_meta = get_post_meta( $post->ID , 'newsletter' , true );
    if ($news_meta) {
    return true;
    } else {
    return false;
    };
    };

    Am I'm on the right track? The results look like they're always coming up 'false'. Thoughts?

    Thank you again so much for your help!

    -Mary

  6. MichaelH
    Member
    Posted 4 years ago #

    What about your global $post;

  7. kenkelm
    Member
    Posted 4 years ago #

    Thanks again for the reply.

    After your reply, I did searches on 'global $post' and it looks like it's needed to look at the info about the current page. So, I changed my function to :

    function is_newsletter() {
    	global $post;
    	$news_meta = get_post_meta( $post->ID , 'newsletter' , true );
            if ($news_meta) {
                   return true;
            } else {
                   return false;
            };
    };

    Unfortunately, it looks like the function is still always outputting a 'false'.

    I then changed it to:

    function is_newsletter() {
    	global $post;
    	$news_meta = get_post_meta( $post->ID , 'newsletter' , true );
            if ($news_meta = 'yes') {
                   return true;
            } else {
                   return false;
            };
    };

    and now I'm always getting a true!

    I'm clearly missing some basic understanding here. Can anyone help?

    Thanks again!

    -Mary

  8. MichaelH
    Member
    Posted 4 years ago #

    Using the WordPress Default theme, in functions.php I put:

    function is_newsletter($custom_field) {
    	global $post;
    	if (get_post_meta( $post->ID , $custom_field , true )) {
                   return true;
            } else {
                   return false;
            }
    }

    And in the loop, in the index.php for the Default theme I put:

    <?php
    the_meta();
    if (is_newsletter('news letter')) {
    echo 'this is news letter';
    }else{
    echo 'this is not a news letter';
    }
    ?>

    [edit - fixed]

  9. kenkelm
    Member
    Posted 4 years ago #

    MichaelH,

    It's finally working! Thank you so much. I am sincerely grateful for yours and everyone else's help.

    Thank you again!

    -Mary

  10. t31os
    Member
    Posted 4 years ago #

    Semi-colon after the closing curly bracket ... is that valid?

    };

    Sorry, i couldn't help myself... :)

  11. MichaelH
    Member
    Posted 4 years ago #

    Nope---so I fixed. Couldn't help myself either. Thanks for being yourself ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags