-
-
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; | |
} |
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. 😄
I don't see anything obviously wrong with it.