-
-
Save billerickson/1209601 to your computer and use it in GitHub Desktop.
| <?php | |
| /** | |
| * Add custom fields to Display Posts Shortcode | |
| * @author Bill Erickson | |
| * @link http://wordpress.org/extend/plugins/display-posts-shortcode/ | |
| * @link http://www.billerickson.net/shortcode-to-display-posts/comment-page-1/#comment-4565 | |
| * | |
| * @param $output string, the original markup for an individual post | |
| * @param $atts array, all the attributes passed to the shortcode | |
| * @param $image string, the image part of the output | |
| * @param $title string, the title part of the output | |
| * @param $date string, the date part of the output | |
| * @param $excerpt string, the excerpt part of the output | |
| * @return $output string, the modified markup for an individual post | |
| */ | |
| add_filter( 'display_posts_shortcode_output', 'be_display_posts_custom_fields', 10, 6 ); | |
| function be_display_posts_custom_fields( $output, $atts, $image, $title, $date, $excerpt ) { | |
| // Get our custom fields | |
| global $post; | |
| $location = esc_attr( get_post_meta( $post->ID, 'location', true ) ); | |
| $type = esc_attr( get_post_meta( $post->ID, 'type', true ) ); | |
| $price = esc_attr( get_post_meta( $post->ID, 'price', true ) ); | |
| // If there's a value for the custom field, let's wrap them with <span>'s so you can control them with CSS | |
| if( isset( $location ) ) $location = '<span class="location">' . $location . '</span> '; | |
| if( isset( $type ) ) $type = '<span class="type">' . $type . '</span> '; | |
| if( isset( $price ) ) $price = '<span class="price">' . $price . '</span> '; | |
| // Now let's rebuild the output. | |
| $output = '<li>' . $image . $title . $location . $type . $price . $date . $excerpt . '</li>'; | |
| // Finally we'll return the modified output | |
| return $output; | |
| } |
I managed to get my dates to display properly using the actual field names instead of $date or $date_start. I just called the field in the conditionals area, which doesn't seem to be doing any conditionals, it shows everything whether it's set or not.
But, my biggest issue right now are the links. The last "eventbrite" item. That is happening in 90 minutes and I can't get the link to display. I've tried it without anything, just naked. I've tried it wrapped in html like I have here. I've tried making it an ACF text field instead of a URL, nothing seems to work. NOTHING displays in the "url" portion of it. It either pastes nothing, or it displays a broken button. I keep looking at my code and see no issues, so if anyone has a second pair of eyes, I'd appreciate it. :) Thanks!
Oh! Hi! sorry, I missed your reply. I got everything, but the $eventbrite link is my biggest issue right now. Sorry about not being more clear. :)
It's currently an ACF text field with a link it. I can't get it to give me anything.
I don't see anything obviously wrong with it.
Thanks, Bill. Yes, that's the kicker. Me either. π Thanks for your help. I appreciate your time.
AH! So... this is what's not working. :)
if( isset( $headshot ) ) $headshot = '<img src="' . $headshot .'class="alignleft headshot" />';
Same with the link. It's because I'm trying to wrap it in HTML, I guess. Truth be told, I'm not sure how to fix that. The others seem to work fine because we're closing them, but this breaks down on the first double quote. Do I escape it or what should I do there, if you have a recommendation? If not, thank you anyway. :)
You're missing the closing double quote. It should be
if( isset( $headshot ) ) $headshot = '<img src="' . $headshot .'" class="alignleft headshot" />';
Oh my god, thank you. Leave it to the olβ missing quotes. I stared at that for literally hours. Thank you so much.
That worked, thanks! Though I guess I had corrected that at some point - but maybe I don't clearly understand isset. Is that meant to be like a conditional?
I assumed it was saying "if this is not null, show this", but that's not what's happening. it's rendering the html regardless of whether or not the field is empty. Thanks again, I appreciate your help.
Isset is checking if that variable actually exists, regardless of whether it's empty or not. You probably want if( ! empty( $headshot ) )
OOOOOOOOOOOK. I was wondering about that. I was like "how does it know?" I feel silly because I write those conditionals all the time. Derp. π
In your rebuilding of the output at the end, you're not including the $image, and I'm not sure what link you want.
Your date is displaying as it is in the database since you're calling it directly. you can either:
a) Format your date, like
date( 'F j Y, $date_start )orb) Use
get_field()instead ofget_post_meta()so ACF formats it.