Support » Themes and Templates » retrieving and displaying post meta as list

  • Resolved Marj Wyatt

    (@marjwyatt)


    I am working on a custom theme project. After reviewing several recipe plugins, I chose to develop code that stored the recipe information as custom fields assigned to that post type only. Here is a link to the code that is working to do this:
    http://pastebin.com/2bE6akPb

    The next hurdle to cross is displaying the recipe ingredients, instructions, at a minimum, as lists. I have a limping custom single template that is rendering the meta for each custom field. I can see through reviewing the database postmeta table that the ingredients and instructions are being stored on separate lines. Here is a screenshot with those fields highlighted to show you what I’m looking at:
    http://screencast.com/t/B03qrv9ykWXY

    This segment of code produces the metadata for the ingredients field but I’m at a loss as to how to write the code that will change the string to a list.

    <?php if ( get_post_meta($postid, '_recipeingred', true) ) :
                    $unorderedList = get_post_custom_values('_recipeingred',$postid);
                    foreach ( $unorderedList as $key => $value ) {
                        echo "$key  => $value<br />";
                    }
                    //$text = '_recipeingred';
                    //format_ingredients($text);
                    //echo $imarkup; ?>
                    <p>The Recipe Ingredients: <?php echo get_post_meta($postid, "_recipeingred", true); ?></p>

    The output wrapped in paragraph html produces a string with spaces between the ingredients. The output of the $unorderedList variable politely puts a 0=> in front of the same list. Here is a screen shot showing what the above embedded snippet outputs:
    http://screencast.com/t/J5hCYsv4OJIw

    I’m beginning to wonder if I need to alter the meta box save function to include the necessary html and, if this is the case, I don’t know that I can imagine where to begin…

    I went on this mission to begin with because all the recipe plugins that I tested did not store the data in ways that it could be accessed for customized output and/or was not retrievable for editing.

    I hope there is someone out there to offer advice. Thanks in advance for your considerations. WordPress rocks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • wpismypuppet

    (@wordpressismypuppet)

    I can offer assistance! Normally I would point you to a tutorial, but it looks like you already have a good grasp of what you are doing.

    Your foreach loop is the way to go! However you don’t need to echo out $key, as that is only the index of the array element. What you want is to simply echo out the unordered list html elements with your $value:

    <?php
        if ( get_post_meta($postid, '_recipeingred', true) ) :
            $unorderedList = get_post_custom_values('_recipeingred',$postid);
            echo '<p>The Recipe Ingredients:</p>' . '<ul>';
            foreach ( $unorderedList as $value ) {
                echo '<li>$value</li>';
            }
            echo '</ul>';
        endif;
    ?>
    wpismypuppet

    (@wordpressismypuppet)

    Oh wait… I misread your data… it looks as though the data is being stored as one long string, but it looks as though it has line breaks stored with it. In this case:

    <?php
        if ( get_post_meta($postid, '_recipeingred', true) ) :
            echo '<p>The Recipe Ingredients:</p>';
            apply_filters( 'the_content', get_post_meta( $postid, "_recipeingred", true ) );
        endif;
    ?>
    Thread Starter Marj Wyatt

    (@marjwyatt)

    Hi @wpismypuppet

    Thanks for your ideas and for recognizing that I had already read many tutorials before posting here.

    The code suggestion in your first post brought back $value, not the contents of the meta.

    The 2nd code suggestion returned nothing but the words enclosed in the paragraph tags.

    I do want a list. Any other ideas?

    wpismypuppet

    (@wordpressismypuppet)

    I apologize… it’s late and I’m a little tired :). My second code is what you want, but you’ll want to echo out the apply_filters…

    <?php
        if ( get_post_meta($postid, '_recipeingred', true) ) :
            echo '<p>The Recipe Ingredients:</p>';
            echo apply_filters( 'the_content', get_post_meta( $postid, "_recipeingred", true ) );
        endif;
    ?>

    However, all this will do is echo out the _recipeingred as it was typed into the field. If you truly want to expand that into a physical ordered/unordered list, let me know and I can help you with some PHP str_ireplace functionality.

    Thread Starter Marj Wyatt

    (@marjwyatt)

    Awesome! I have the items showing on separate lines now, thanks to your help. Thank you so much!!!!

    Looking at the page source, I can see
    tags. I would appreciate your advice/assistance with the str_replace function code, if it isn’t too much trouble. 🙂

    wpismypuppet

    (@wordpressismypuppet)

    Ok… so I am guessing you want _recipeingred to be an actual unordered list… so something like:

    • Item one
    • Item two
    • Item three

    If this is the case, we first want to get that string saved into a variable instead of just echoing it out:

    $ingredients = get_post_meta( $postid, "_recipeingred", true );

    Then we’ll want to break the string apart using the line breaks as a divider:

    $ingredients = explode( "\n", $ingredients );

    Now $ingredients is an array! We can go back to your previous foreach loop and create an unordered list:

    echo '<ul>';
    foreach( $ingredients as $ingredient ) {
    	echo '<li>' . $ingredient . '</li>';
    }
    echo '</ul>';

    So the whole thing should look something like this:

    <?php
    	if ( get_post_meta($postid, '_recipeingred', true) ) :
    		// Get the variable from the database as a string
    		$ingredients = get_post_meta( $postid, "_recipeingred", true );
    		// Break the string up by using the line breaks (carriage returns)
    		$ingredients = explode( "\n", $ingredients );
    		// Echo out the title
    		echo '<p>The Recipe Ingredients:</p>';
    		// Start the unordered list tag
    		echo '<ul>';
    		// Loop through each ingredient since it's now an array thanks to the explode() function
    		foreach( $ingredients as $ingredient ) {
    			// Add the list item open and close tag around each array element
    			echo '<li>' . $ingredient . '</li>';
    		}
    		// Once the loop finishes, close out the unordered list tag
    		echo '</ul>';
    	endif;
    ?>

    This isn’t tested, but it should work. If there are any bugs, let me know and I’ll be able to correct them.

    Thread Starter Marj Wyatt

    (@marjwyatt)

    You are absolutely my savior tonight, @wpismypuppet! I’ve implemented the code in my custom single script template and it works like a charm!

    Thank you so much for getting me what I needed tonight. I have a meeting with the client tomorrow morning and, even though a lot has been accomplished without this formatted output, it will be a much happier meeting for me with this functioning code in place.

    Thank you! Thank you! Thank you!

    wpismypuppet

    (@wordpressismypuppet)

    You are most welcome. Good luck with the client! Also, you should mark this thread as resolved so others know. Have a nice night.

    Thread Starter Marj Wyatt

    (@marjwyatt)

    I have indicated resolution.

    Get some rest! 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘retrieving and displaying post meta as list’ is closed to new replies.