• swiftmed@me.com

    (@swiftmedmecom)


    Hey guys,

    I have created two custom fields in my admin area. They look like this…

    -> Service Used
    -> Service URL

    I am wanting to publish the text from field 1 (service used) to my template, inside of a link from field 2. In a normal HTML page, that would look like this:

    <a href="Field 2 (Service URL)">Field 1 (Service Used)</a>

    Could you please give me the correct piece of code to achieve the above that I would post into my template file? I’ve been trying for over an hour and It’s not working for me.

    Many thanks.
    Andrew

Viewing 15 replies - 1 through 15 (of 15 total)
  • Ctrl-C

    (@ctrl-c)

    Use get_post_meta function, some examples could be seen there too.

    Thread Starter swiftmed@me.com

    (@swiftmedmecom)

    Thank you for the reply. To be honest, I’ve already looked at that post, and I don’t really understand what Im looking at, or how I would use the examples. I don’t have a coding background, other than very basic html. Hence, I was hoping someone could show me how / what I need to use.

    Ctrl-C

    (@ctrl-c)

    I don’t know where you are trying to insert those fields. Also, I don’t know IDs of your fields (Service Used or Service URL are not correct ones, they shouldn’t be separated by spaces).

    Thread Starter swiftmed@me.com

    (@swiftmedmecom)

    Hey,

    Im trying to insert the things into my single.php file, within the loop. The field names are as follows: ‘service_used’ and ‘service_url’

    Thanks again. πŸ™‚

    Ctrl-C

    (@ctrl-c)

    Safe example of code:

    $service_used = get_post_meta(get_the_ID(), 'service_used_field', true);
    $service_url = get_post_meta(get_the_ID(), 'service_url_field', true);
    if($service_used && $service_url) :
        echo '<a href="' . $service_url . '">' . $service_used . '</a>';
    endif;
    Ctrl-C

    (@ctrl-c)

    Oh, I guessed your fields πŸ™‚
    Try that code and report if it works, please.

    Thread Starter swiftmed@me.com

    (@swiftmedmecom)

    OK I entered that into my post, but nothings showing up. (Emptied my cache, tried with Firefox, Safari and Chrome).

    Heres the code Im using.

    single.php:

    <? $service_used = get_post_meta(get_the_ID(), 'service_used_field', true);
    $service_url = get_post_meta(get_the_ID(), 'service_url_field', true);
    if($service_used && $service_url) :
        echo '<a href="' . $service_url . '">' . $service_used . '</a>';
    endif;?>

    In the portfolio.php custom post type page that came with my theme…

    'service_used' => Array('type' => 'text', 'description' => __('Service', 'themeton'), 'title' => __('Service', 'themeton')),
    
    	'service_url' => Array('type' => 'text', 'description' => __('Service URL', 'themeton'), 'title' => __('Service URL', 'themeton')),

    Can you see anything wrong with that?

    Ctrl-C

    (@ctrl-c)

    Sorry, my bad:

    $service_used = get_post_meta(get_the_ID(), 'service_used', true);
    $service_url = get_post_meta(get_the_ID(), 'service_url', true);
    if($service_used && $service_url) :
        echo '<a href="' . $service_url . '">' . $service_used . '</a>';
    endif;
    Thread Starter swiftmed@me.com

    (@swiftmedmecom)

    There must be something weird going on with my theme or something. No matter what I do, it’s just not showing up. I know Im editing the right page, as other changes I make to it show up just fine. But nothings showing up with regards to the custom fields.

    It ‘should’ be showing up above the Date on the right hand column at the following URL….

    http://metalpotato.com/portfolio/techno-chimp/

    Ctrl-C

    (@ctrl-c)

    Try this code (it is not safe, but should output fields if they do exist):

    $service_used = get_post_meta(get_the_ID(), 'service_used', true);
    $service_url = get_post_meta(get_the_ID(), 'service_url', true);
    echo '<a href="' . $service_url . '">' . $service_used . '</a>';
    Thread Starter swiftmed@me.com

    (@swiftmedmecom)

    Still nothing. I honestly don’t get it, as the custom fields show up in the admin, and are filled out (see pic). But the output is not showing up on the front end.

    http://postimg.org/image/43dv3pvi3/

    Ctrl-C

    (@ctrl-c)

    Probably, your custom fields have some other names. You will need to get them.

    Thread Starter swiftmed@me.com

    (@swiftmedmecom)

    Hey, I asked the person who created the theme about it, he made a small modification to the code you gave me, and it worked perfectly.

    This is what worked:

    $service_used = get_field("service_used");
    $service_url = get_field("service_url");
    echo '<a href="' . $service_url . '">' . $service_used . '</a>';
    Ctrl-C

    (@ctrl-c)

    Seems like the developer of your theme is using 3rd party framework or plugin as get_field() is not an in-built WordPress function. Great to hear that everything has worked out.

    Thread Starter swiftmed@me.com

    (@swiftmedmecom)

    Ahh that now makes sense. Im not that great with code, so I was stabbing in the dark. Either way, thanks very much for your help. It was very much appreciated. πŸ™‚

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Posting and Linking 2 Custom Fields in Template – Please Help’ is closed to new replies.