• Resolved chrisanthropic

    (@chrisanthropic)


    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?

Viewing 8 replies - 1 through 8 (of 8 total)
  • 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.

    Thread Starter chrisanthropic

    (@chrisanthropic)

    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.

    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
    https://codex.www.ads-software.com/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

    Thread Starter chrisanthropic

    (@chrisanthropic)

    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. https://pastebin.com/kcUwHh2P

    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:
    https://wp.tutsplus.com/tutorials/theme-development/a-beginners-guide-to-the-wordpress-loop/

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

    They should help you out.

    Thread Starter chrisanthropic

    (@chrisanthropic)

    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.

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

    Thread Starter chrisanthropic

    (@chrisanthropic)

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

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘single.php | how to insert custom field data into a URL’ is closed to new replies.