WordPress.org

Ready to get started?Download WordPress

Forums

Links from Custom Fields (12 posts)

  1. Pheick
    Member
    Posted 8 years ago #

    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.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    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.

  3. Pheick
    Member
    Posted 8 years ago #

    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>

  4. Dgold
    Member
    Posted 8 years ago #

    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>

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    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.

  6. Pheick
    Member
    Posted 8 years ago #

    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:

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

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

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    Right after your foreach line, add this line:
    setup_postdata($post);

    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". :)

  8. Pheick
    Member
    Posted 8 years ago #

    Cool! Works like a charm! :-) Thanks a lot.

  9. Pheick
    Member
    Posted 8 years ago #

    ...well almost works :-(

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

    But
    - 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?

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    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.

  11. Pheick
    Member
    Posted 8 years ago #

    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?

  12. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags