Recently I developed a WordPress site that uses a custom post type for Events. This is a typical event page – note the More Info section in the left sidebar.
The client needed to be able to insert a list of links under More Info, and the number of links will change for every event. I needed a way to make this super-simple for the client, so in the Events custom post editor, the links are entered one per line like so:
1 2 3 4 5 |
<a href="http://www.supportourwarriors.org/">Event Website</a> <a href="http://www.supportourwarriors.org/scheduleofevents.html">Schedule of Events</a> <a href="http://www.supportourwarriors.org/keynotespeaker.html">Keynote Speaker</a> <a href="http://www.supportourwarriors.org/honoredguests.html">Honored Guests</a> <a href="http://www.supportourwarriors.org/golftournament.html">Golf Tournament</a> |
Then, I needed to convert that line break-delimited list into an unordered list for formatting… I had no idea how to do that but after some hunting around I found this post that completely answered my questions.
Then in my single-events.php template, I have this section including the wonderful code snippet from wordpressismypuppet that converts the entries into a standard unordered list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<div id="links"> <h3>More Info</h3> <?php if ( get_post_meta($post->ID, 'wpcf-event-info-links', true) ) : // Get the variable from the database as a string $links = get_post_meta( $post->ID, "wpcf-event-info-links", true ); // Break the string up by using the line breaks (carriage returns) $links = explode( "\n", $links ); // Start the unordered list tag echo '<ul class="link-list">'; // Loop through each ingredient since it's now an array thanks to the explode() function foreach( $links as $link ) { // Add the list item open and close tag around each array element echo '<li>' . $link . '</li>'; } // Once the loop finishes, close out the unordered list tag echo '</ul>'; endif; ?> </div> |
So now the result is a ul under ‘More Info.’ formatted to match the other sidebar widgets. The client has a bare minimum of HTML to contend with and has control over what appears in the list – everyone’s happy!