• Resolved mercurylime

    (@mercurylime)


    I have a custom key on some of my posts – the key is ‘slink’ and the value is a link to files. What is the correct syntax for making a link using that value from inside the loop?

    I’m trying:/a href = “/?php get_post_custom_values(‘$slink’); ?\”\Listen<\a>

    (replaced <‘s and >’s with /’s)

    …but it isn’t working. Any suggestions?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Wouldn’t you want to use the_meta()?

    https://codex.www.ads-software.com/Template_Tags/the_meta

    Thread Starter mercurylime

    (@mercurylime)

    When I use that, it puts up the meta information of the template page I’m using, not of the current post. When I use something like the_time(), it gives me the right time of the post, but the_meta() gives me info of the template page…

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    What info, exactly, does the_meta give you? Template pages don’t have metadata, I thought. Maybe I’m confused.

    get_post_custom_values(‘slink’) (no dollar sign!) should return whatever value is associated with the slink key on that post. This must be used in the loop, of course.

    Thread Starter mercurylime

    (@mercurylime)

    it inserts the text “_wp_page_template: ml_music.php”.

    I took out the dollar sign and it still didn’t work. I have this in the code:

    /?php $posts = get_posts(‘category=42’); foreach($posts as $post) : ?/

    I guess that doesn’t count as the Loop… but how else would I do it?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Yes, get_posts does not a proper loop make, unless you do a setup_postdata() in the loop. Even then it’s not quite the same, but it’s usually good enough.

    Try this:
    <?php $posts = get_posts('category=42');
    foreach($posts as $post) :
    setup_postdata($post)
    ... rest of your loop ...
    ?>

    And use backtick marks (below the ~ key) to post code on these forums. One before the code, one after.

    Thread Starter mercurylime

    (@mercurylime)

    Thanks! That fixed it. Any idea how to pull the specific value from the_meta to use in a link? I’m working on it now..

    the_meta won’t work because it includes more than just the value..

    get_post_meta($post->ID, 'slink', $single=true) doesn’t work, I’m not sure why…

    get_post_meta($post->ID, 'slink', true); also doesn’t work..

    get_post_custom_values('slink') doesn’t work either, it brings back the link for the current page..

    ..and I can’t find any other tags that return plain strings.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    the_meta() just spits out the key/value pairs. More useful for debugging than anything else.

    But get_post_custom_values(‘key’) returns the value of that key. It uses the exact same method as the_meta() does to get those key/value pairs. All these functions are in template-functions-post.php if you want to see how they work.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Actually, I just noticed that get_post_custom_values(‘key’) returns an array of values and not the value itself. If you know there’s only one value, then post_custom(‘key’) will return just that one value, and the array of values if it’s got more than one value.

    Thread Starter mercurylime

    (@mercurylime)

    Hm… It’s still not working. I checked the source html, and it’s not inserting anything into the “” of the <a href="">.

    https://mercurylime.net/blog/?page_id=26 is the link to the page I’m working on. I’m trying to get the ‘slink’ value to be the url of each post’s “Listen” link.

    Thread Starter mercurylime

    (@mercurylime)

    I fixed it! I just had to add “echo” before the get_post_meta.

    Thanks Otto!

    so what was your final solution, with the correct syntax and formatting? I’m doing a similar thing. Each post in a specific category needs a link added to it. I have a seperate single.php for this category with a nice custom box to contain this link, and I’d like to add the call to the custom field in that box. When writing the post, I just add the “key” and then the URL to the link. Not certain on how to go about it though, and this sounds like the winner but what was your exact solution?

    Nevermind on my last post, we have figured it out. For anyone else in the same situation:

    In the post, mark your custom key (for this it will be “link”) at the end of your post, and then in the value type the FULL URL to the link (otherwise WP combines the link (for example https://www.google.com) onto your own URL so it looks like https://www.mysite.com/www.google.com).

    In the template, which was what I needed to put the code into, I simply added the following code:

    <a href="<?php echo get_post_meta($post->ID, 'link', $single=true) ?>">Here's the link to the article</a>

    The link text won’t change so it needs to be generalized and I plopped that between my style tags and it works like a charm.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘using custom fields as links – help!’ is closed to new replies.