WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] single.php | how to insert custom field data into a URL (9 posts)

  1. chrisanthropic
    Member
    Posted 1 year ago #

    I'm learning to play around with custom fields and now I'm trying to take the data from a custom field and add it to a URL.

    Here's the code I'm using right now

    if ( get_post_meta($post->ID, 'amazon', true) ) : {
    echo '<a class="abutton medium orange" href="">Kindle</a>';
    }

    This will successfully show what I want but of course links to nothing.
    I've also tried a lot of other methods I found online but nothing seems to work.

    In short, I want the link to look like this
    href="$amazon"

    where $amazon is the name of my custom field.
    Any suggestions on how I could go about coding that withing my single.php file?

  2. Jay Hoffmann
    Member
    Posted 1 year ago #

    You can try:

    if ( get_post_meta($post->ID, 'amazon', true) ) : ?>
    <a class="abutton medium orange" href="<?php echo get_post_meta($post->ID, 'amazon', true) ?>">Kindle</a>
    <?php endif; ?>

    That should check to make sure there is a link, and add the custom field value inside the link. The trick is probably closing out the PHP and switching straight to HTML for the bulk of the code.

  3. chrisanthropic
    Member
    Posted 1 year ago #

    Thanks for the advice.

    If I try the code you suggested then the page give me a server error. In fact that happens any time I try to put any php inside the href"" section.

    The code is being nested after some other stuff that all follows this:

    <?php
    			if ( have_posts() ){
    				while (have_posts()){
    					the_post();

    If that makes any difference.

  4. Jay Hoffmann
    Member
    Posted 1 year ago #

    Yea that definitely makes a difference. Are you making sure to open and close your Loop properly?

    Your Loop should begin with:
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    and at the end of your single.php file should be:

    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Read more about it here to get acquainted, the error might be coming
    http://codex.wordpress.org/The_Loop

    Also, every time you go out of PHP code and into HTML you have to make sure to end it with ?> and every time you introduce more php code you put <?php

  5. chrisanthropic
    Member
    Posted 1 year ago #

    Ok, thanks again. I'm still new to php and I'm learning a lot, thanks for taking the time to help.

    After spending more time reading up, I'm still not getting it working. There may be a better way of going about it, but here's what I'm trying to accomplish: I have my content area split into two columns. The left side is a full sized thumb, the right side is some post_meta followed by the_content.

    The links I'm trying to create are directly under the thumbnail and thus (as far as I can tell) need to be within the loop.

    Here's the full single.php that I'm working with, if you're still willing to take a look. http://pastebin.com/kcUwHh2P

  6. Jay Hoffmann
    Member
    Posted 1 year ago #

    I think you would want to read up on how to properly construct this stuff, since there is a lot going on in your code.

    I would try reading over this:
    http://wp.tutsplus.com/tutorials/theme-development/a-beginners-guide-to-the-wordpress-loop/

    and then maybe this: http://wp.smashingmagazine.com/2010/04/29/extend-wordpress-with-custom-fields/

    They should help you out.

  7. chrisanthropic
    Member
    Posted 1 year ago #

    Yeah, I'm still learning and trying to adapt parts of a custom theme that I paid for a while back.

    Thanks for the links, I'm off to do some more reading.

  8. Jay Hoffmann
    Member
    Posted 1 year ago #

    If it's a premium theme you want to contact the theme creator for support if he is still active as well. Good luck.

  9. chrisanthropic
    Member
    Posted 1 year ago #

    I got it working using get_template_part and the code you recommended. Thanks a lot for the advice!

Topic Closed

This topic has been closed to new replies.

About this Topic