• Resolved waxpop

    (@waxpop)


    Hi, I’m using this tutorial to try to list multiple values of one custom key under each post. I feel like I’m pretty close but I’m not quite there.

    It seems to be working fine on posts that have just one value for each key. See some posts on this page of my site for an example of where things are working and not working. This entry with only one value is fine but this one, which is supposed to have 3 values listed, only has one listed, plus the CSS formatting which I used to hide the date of the post, the category and the author is now not working for that post only. You can see below the notes that “March 22, 2011Uncategorizedfamily, Joan, movies, travelThe Diarist” is showing there. Only the tags should show under each post, as they do with all other posts that only have one value.

    Here’s the code I’ve used:

    <?php
    $my_notes = get_post_meta($post->ID, "Notes", false);
    if ($my_notes[0]=="") { ?>
    
    <!-- If there are no custom fields, show nothing -->
    
    <?php } else { ?>
    
    <div class="MyNotes">
    	<h3>Notes:</h3>
    
    	<?php foreach($my_notes as $my_notes) {
    	echo '<blockquote><p>'.$my_notes.'</p></blockquote>';
    	} ?>
    
    </div>
    
    <?php } ?>

    Notes is my key. $my_notes is what I’ve chosen as the variable that will store the value of each custom field, as he says in the tutorial. Can I really just choose anything for that? This code is within its own div between the entry-content and the entry-meta sections (not within them). I’m using a child theme of the Twenty Thirteen theme.

    Can anyone see what I’m doing wrong here? Any help would be greatly appreciated. Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • I think this is due to a typo. You have the same variable used as the array ($my_notes) and the single entry ($my_notes) in your foreach loop.

    Try changing this:

    <?php foreach($my_notes as $my_notes) {
       echo '<blockquote><p>'.$my_notes.'</p></blockquote>';
    } ?>

    to this:

    <?php foreach($my_notes as $one_note) {
       echo '<blockquote><p>'.$one_note.'</p></blockquote>';
    } ?>
    Thread Starter waxpop

    (@waxpop)

    Hmm….made that change and it looks the same as before.

    Time for a little debugging. Please insert a line to dump $my_notes, like this:

    $my_notes = get_post_meta($post->ID, "Notes", false);
    print_r('<pre>MY NOTES:');print_r($my_notes);print_r('</pre>');
    Thread Starter waxpop

    (@waxpop)

    Any particular place I should put that code?

    I meant for you to insert it into the code you showed earlier. Change this:

    $my_notes = get_post_meta($post->ID, "Notes", false);

    to this:

    $my_notes = get_post_meta($post->ID, "Notes", false);
    print_r('<pre>MY NOTES:');print_r($my_notes);print_r('</pre>');
    Thread Starter waxpop

    (@waxpop)

    Ok, thanks! You can see results here.

    That dump shows that only one Custom Field was found. Maybe the others have a slightly different meta_key?

    I think you might have a missing quote in the new line. Please check it closely to be sure it matches the line above, particularly the quotes.

    OK looks like the quote problem is solved, and you now have 3 Custom Fields. At the moment, the second one is blank.

    Thread Starter waxpop

    (@waxpop)

    Sorry, it didn’t look like it had saved some changes that I made yesterday – all three notes were in one key. I’ve edited it to separate them just now. There should be 3 notes: Ridgemont Theatre, Dreams & Three Strange Loves (aka Thirst). Can you check that link again?

    Here’s a link to a screenshot of what I have going on in my Dashboard.

    I appreciate all your help!

    Thread Starter waxpop

    (@waxpop)

    Ah! That did it. I had an errant < somewhere in there so the second one wasn’t showing up. Thank you so much!

    The second entry has an extra < in front of Dreams. That is causing an HTML error so it does not show the link.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Trouble displaying multiple values of a custom field key’ is closed to new replies.