WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Trying to recall a Custom Field with PHP (3 posts)

  1. Trimmode
    Member
    Posted 5 years ago #

    I am pretty close on this, but I must have a , or a ' in the wrong place - if someone wouldn't mind giving me a minute or two of their time:

    I have 2 custom fields: Author Name (Submitter) and Author Webpage. And here are the 2 sections I am trying to get to work:

    $submitter = get_post_meta($post->ID, 'Author Name', $single = true);

    and

    <?php if (get_post_meta($post->ID, 'Author Name', true)) {echo "Submitter: "; } else { echo '';} ?>
                                       <?php if (get_post_meta($post->ID, 'Author Webpage', true)) {echo '<a href="', get_post_meta($post->ID, "Author Webpage", true), 'target="_blank">'; echo $submitter;'</a>';} else { echo get_post_meta($post->ID, 'Author Name', true);} ?>

    I am trying to make it look to see if the post has a name in the custom field "Author Name." If it does AND if the Author has a webpage url in another custom field "Author Webpage", to display in the post "Submitter: [Author Name]" (and the [Author Name] is a link to their webpage url. If they don't have a Webpage listed, then just print their name without the clickable link.

    I am really close, but the last line of code above is messing up. I obviously have the syntax typed in wrong because:
    (1) It is taking me to a site that has "target=" at the end so I get a "can't open page error". (i.e. "www.yahoo.comtarget=")
    (2) It is not "closing" the <a> because my next element sitting next to this "Submitter: " element thinks it is something to be clicked on too and it's not.

    Can someone look over that last line and tell me what I did wrong.

    Thank you! Trimmode

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Yuck. Try this instead.

    <?php
    $author_name = get_post_meta($post->ID, 'Author Name', true);
    $author_url = get_post_meta($post->ID, 'Author Webpage', true);
    if (!empty($author_name)) {
    echo 'Submitter: ';
    if (!empty($author_url)) {
    echo '<a href="'.$author_url.'" target="_blank">';
    echo $author_name;
    echo '</a>';
    } else {
    echo $author_name;
    }
    }
    ?>
  3. Trimmode
    Member
    Posted 5 years ago #

    @Otto42 ~

    Wow. That not only works but works much better than mine would have should it have been fixed! But I guess you knew that already. :)

    I learned a lot from that. Thank you.

    Trimmode

Topic Closed

This topic has been closed to new replies.

About this Topic