Support » Themes and Templates » Trying to recall a Custom Field with PHP

  • Resolved Trimmode


    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);


    <?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. “”)
    (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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Yuck. Try this instead.

    $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;

    @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.


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trying to recall a Custom Field with PHP’ is closed to new replies.