WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get all custom fields (name + value)? (9 posts)

  1. hberger
    Member
    Posted 4 years ago #

    Does anyone know how to show all custom fields (name AND value) for one post?

    I would like to create a FAQ, and just put the Q&A:s into the custom fields of one post:

    CUSTOM FIELDS
    NAME.......................................VALUE
    Question 1...............................Answer 1
    Question 2...............................Answer 2
    Question 3...............................Answer 3

    ______________________________________________

    I am using a jquery faq plugin, so I want to have full control of the data. Please help..

  2. esmi
    Forum Moderator
    Posted 4 years ago #

  3. hberger
    Member
    Posted 4 years ago #

    Emsi,

    That doesn´t give me both fields. Only one of them + the ID :-(

  4. Justin Tadlock
    Member
    Posted 4 years ago #

    It works perfectly fine. get_post_custom() returns a multidimensional array of ALL custom fields (with keys and values) for a post.

  5. hberger
    Member
    Posted 4 years ago #

    Greenshady,

    OK, I´m sorry. But I can´t get it to work.
    Could you please give me the code for echoing "name" and "value"?

  6. hberger
    Member
    Posted 4 years ago #

    Someone please help me.

    I have my custom fields like this:

    Custom field "name" Custom field "value"
    Q: How do I contact you? A: You can use our contact form here
    Q: Which way is the....... A: Try this one........

    And so on. Everything I am able to get is:

    0 => You can use our contact form here
    1 => Try this one........

    ID and the value field. Not the "name" field.

  7. Justin Tadlock
    Member
    Posted 4 years ago #

    This bit of code should work:

    <?php
    $custom_fields = get_post_custom();
    
    foreach ( $custom_fields as $field_key => $field_values ) {
    	foreach ( $field_values as $key => $value )
    		echo $field_key . ' - ' . $value . '<br />';
    }
    ?>
  8. hberger
    Member
    Posted 4 years ago #

    Thank you so much, greenshady!

    But I get this as well, in the beginning of the custom fields:

    _edit_lock - 1274798334
    _edit_last - 1

    Is it possible to filter that away somehow?

  9. hberger
    Member
    Posted 4 years ago #

    I got it, thanks to a friend:

    $custom_fields = get_post_custom(9);
    
    foreach ( $custom_fields as $field_key => $field_values ) {
    	if(!isset($field_values[0])) continue;
    	if(in_array($field_key,array("_edit_lock","_edit_last"))) continue;
    echo $field_key . '=>' . $field_values[0];
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.