Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created September 11, 2011 13:46
Show Gist options
  • Save billerickson/1209601 to your computer and use it in GitHub Desktop.
Save billerickson/1209601 to your computer and use it in GitHub Desktop.
Add custom fields to Display Posts Shortcode
<?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;
}
@billerickson
Copy link
Author

Isset is checking if that variable actually exists, regardless of whether it's empty or not. You probably want if( ! empty( $headshot ) )

@joellereeder
Copy link

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. 😄

@joellereeder
Copy link

joellereeder commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment