Support » Plugins » Links from Custom Fields

  • I am trying to pull out meta data to use in my navigation – basically I want a list of all my posts, but with an anchor text I define myself.
    I’ve tried something like this:

    <li> <a href="<?php the_permalink(); ?>"><?php the_meta(); ?></a> </li>
    …But here I get both the key and the value – and I only want the value…

    Any ideas, hints and solutions are most welcome.

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

    (@otto42) Admin

    If you can see the key/value you want in the output from the_meta, then you should be able to do:
    $value = post_custom('keyname');
    to get the value from it. If there’s more than one value, it returns an array of values. get_post_custom_values(‘keyname’) does the same thing, but always returns an array, which may be more useful or easier to use for you.

    Ok, but do where do I use that string?
    Using it like so doesn’t really work:

    <a href="<?php the_permalink(); ?>"><?php the_meta($value = post_custom('keyname')); ?></a>

    i don’t know how to do it with the_meta like that

    So I just used the Get Custom Fields plugin, which allows this:

    <a href="<?php echo c2c_get_custom('Links to mp3'); ?>">mp3</a>

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Pheick: Umm… Yeah, that code doesn’t work because it doesn’t make any actual sense or anything. Sorry, from your original post I thought you understood PHP. It would be good to clarify in the future that you don’t, so people will explain in greater detail.

    So try this instead:
    <a href="<?php the_permalink(); ?>"><?php echo post_custom('keyname'); ?></a>

    You need to put the key you want the value for in place of ‘keyname’.

    the_meta() is mainly a debugging tool. It just spits out all the meta information. It doesn’t have any other way to use it.

    Hi Otto – you’ve seen through my clever disguise… French and php are the same to me… I appreciate your help, though.

    Tried your suggestion, but I only get the permalink w/o anchor text (and I did change the ‘keyname’).

    But I might as well move on to my ultimate goal: the long list of posts with custom labels in the sidebar.

    Hope I don’t knock anyone off the chair here:

    $posts = get_posts('numberposts=100');
    foreach($posts as $post) :
    <li><a href="<?php the_permalink(); ?>"><?php echo post_custom('keyname'); ?></a></li>
    <?php endforeach; ?>

    I’ve tried this on my site, but as I said, I only get a list of permalinks in the code. Any suggestions/corrections?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Right after your foreach line, add this line:

    That function reads the post and sets a whole bunch of relevant global variables. That line being in the loop is what makes it “The Loop”. Or at least, “A Loop”. 🙂

    Cool! Works like a charm! 🙂 Thanks a lot.

    …well almost works 🙁

    – if i use the code snippet in the bottom of the code under the content, the magic works.

    – if I use the code snippet in the top of the code (above the_content), the permalinks/custom links works, but the content returns the latest post on all permalinks…

    I seems I need to tell the_content to behave ‘normally’ if I want to/have to use the snippet above the_content.

    What to do?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    I don’t really understand what you’re talking about there. This is a separate Loop that you’ve made, it should not be in your main Loop itself. That would be very bad.

    Well, I have several blogs with different designs. Only thing they have in common is the need for customized links to the single posts. These links thus needs to be placed differently on the different blogs according to the actual design.

    In the templates where I can use the snippet in the sidebar and call the sidebar after the_content, everything is just fine. The problem, as mentioned above, appears when I use the snippet before the_content – which apparently then conflicts with the main Loop.

    So, if I can’t have any little loops in the main loop – is there any other way to get the result that I want?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    No, you can have that bit of code before the main loop. Just not *in* the main loop.

    You could put a loop inside the loop like that though, but you have to setup_postdata() on the actual main loop’s post before you do the the_content() call or any other call that you want to hit the post from the main loop and not the post from the little loop. Without knowing exactly what your code looks like, it’s hard to be specific.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Links from Custom Fields’ is closed to new replies.