Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom fields in two columns (through Functions.php)
- This topic has 6 replies, 3 voices, and was last updated 10 years, 9 months ago by ameeker.
-
AuthorPosts
-
July 15, 2013 at 9:54 pm #50912ameekerMember
http://pastebin.com/Kh0iAvXr <-- in case it doesn't paste properly below.
I'm trying to display some custom fields in two columns. The fields themselves are displaying fine, but I don't think I have this written right yet to get them in two columns. I'd like everything that I currently have with the class "location" in the first column, and everything with the class "touchpoints" in the second column. Ideally, I'd be able to use the built in column classes. Can anyone help me straighten this up?
add_action( 'genesis_entry_content', 'address' ); function address() { if(is_single() ) { if(get_field('Address_1')) { echo '<div class="location">' . get_field('Address_1') . '</div>'; } if(get_field('street_address_2')) { echo '<div class="location">' . get_field('street_address_2') . '</div>'; } if(get_field('po_box')) { echo '<div class="location">' . get_field('po_box') . '</div>'; } if(get_field('city')) { echo '<div class="location">' . get_field('city') . '<div>'; } if(get_field('state')) { echo '<div class="location">' . get_field('state') . '</div>'; } if(get_field('zip')) { echo '<div class="location">' . get_field('zip') . '</div>'; } if(get_field('phone')) { echo '<div class="touchpoints">' . get_field('phone') . '</div>'; } if(get_field('website')) { echo '<div class="touchpoints">' . get_field('website') . '</div>'; } } }
http://worthington.angiemeekerdesigns.com/attractions/new-attraction/ is the site I'm working on but it's nowhere near finished.
Looking for Genesis website design backed by strategy? http://www.angiemeekerdesigns.com
July 15, 2013 at 10:40 pm #50918Robert NeuMemberAngie,
You can use column classes for this so you don't need to write any extra CSS for the columns themselves. You should probably run a check to make sure you're not outputting empty divs when there isn't any data. Something like this should work:
https://gist.github.com/fatmedia/6005802
I haven't tested that, but it should work. I can't remember if ACF automatically sanitizes the output when you use get_field but it's generally good practice to use esc_attr() when echoing out the data. You probably also don't need so many divs in there unless you're planning on styling each line individually, in which case you could use a <span> for some of them.
July 16, 2013 at 12:58 am #50923Gary JonesMemberInstead of all of the echoes, I'd assign the field divs to a variable, then just check that variable to see if it contains anything, before adding a wrapping column div. If that result then contains anything, then output. Same end result as Rob (not echoing empty wrappers), but avoids having to specify that big conditional on line 14.
Don't echo until the last possible moment. Build all of the required data first.
WordPress Engineer, and key contributor the Genesis Framework | @GaryJ
July 16, 2013 at 11:05 am #50958ameekerMemberHere's the result of Rob's:
http://worthington.angiemeekerdesigns.com/attractions/new-attraction/Not yet what I was expecting, but I don't know why.
Looking for Genesis website design backed by strategy? http://www.angiemeekerdesigns.com
July 16, 2013 at 12:34 pm #50977Robert NeuMemberIt looks like there's an unclosed div tag in there. It's causing the second div to render inside the first.
I updated my code based on Gary's suggestions. I'm pretty sure this is what he meant:
https://gist.github.com/fatmedia/6005802
Again, that hasn't been tested so I'm not sure if it will work. I also escaped the data for you. Let me know if that does the trick. You probably don't even need the spans in there, but I dropped them in in case you need them for styling.
July 16, 2013 at 1:06 pm #50982Robert NeuMemberI guess it would probably make more sense to run through that first set with a foreach loop.
Something like this:
https://gist.github.com/fatmedia/26faf43ea62f5153c6be
I'm not totally sure if I'm setting that up right though, there's a good chance that won't work and I'm not in front of my main dev machine so I can't really test it. Gary, can you escape the data that way? Did I screw anything else up on that?
July 16, 2013 at 2:57 pm #50994ameekerMemberThanks guys, the second paste is what I used: http://pastebin.com/VKzfWM2j
Looking for Genesis website design backed by strategy? http://www.angiemeekerdesigns.com
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.