Created
September 11, 2011 13:46
-
-
Save billerickson/1209601 to your computer and use it in GitHub Desktop.
Add custom fields to Display Posts Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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. 😄
*Hi! I'm running into a little issue on something related to this topic.
I'm really in a time sensitive place and am hoping someone sees this. Song
of our people. :) *
//* DISPLAY POSTS SHORTCODE CUSTOMIZATIONS *//
add_filter( 'display_posts_shortcode_output',
'be_display_posts_custom_fields', 10, 11 );
function be_display_posts_custom_fields( $output, $original_atts, $image,
$title, $date, $excerpt, $inner_wrapper, $content, $class, $author,
$category_display_text ) {
// Get our custom fields
global $post;
$date_start = get_post_meta( $post->ID, 'seminar_date_time', true ) ;
$date_end = get_post_meta( $post->ID, 'seminar_end_time', true ) ;
$synopsis = get_post_meta( $post->ID, 'seminar_synopsis', true ) ;
$name = get_post_meta( $post->ID, 'seminar_name', true ) ;
$host = get_post_meta( $post->ID, 'seminar_host_name', true ) ;
$bio = get_post_meta( $post->ID, 'seminar_host_bio', true ) ;
$headshot = get_post_meta( $post->ID, 'seminar_host_headshot', true ) ;
$cost = get_post_meta( $post->ID, 'seminar_cost', true ) ;
$location = get_post_meta( $post->ID, 'seminar_location', true ) ;
$sponsor = get_post_meta( $post->ID, 'seminar_sponsor', true ) ;
$eventbrite = get_post_meta( $post->ID, 'seminar_eventbrite_link', 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( $date_start ) ) $date_start = '<div class="date">' . $date_start
. ' —' . $date_end . '</div> ';
if( isset( $name ) ) $name = '<h4>' . $name . '</h4> ';
if( isset( $synopsis ) ) $synopsis = '<div class="synopsis">' . $synopsis .
'</div> ';
if( isset( $headshot ) ) $headshot = '<img src="' . $headshot
.'class="alignleft headshot" />';
if( isset( $host ) ) $host = '<h6>' . $host . '</h6>';
if( isset( $bio ) ) $bio = '<div class="bio">'. $bio . '</div> ';
if( isset( $location ) ) $location = '<div class="location">' . $location .
'</div> ';
if( isset( $cost ) ) $cost = '<div class="cost">' . $cost . '</div> ';
if( isset( $eventbrite ) ) $eventbrite = '<a href="' . $eventbrite . '"
class="button eventbrite-link">Reserve Your Spot</a>';
// Now let's rebuild the output.
$output = '<li>' . $date_start . $excerpt . $name . $synopsis . $headshot .
$host . $bio . $cost . $location . $eventbrite .'</li>';
// Finally we'll return the modified output
return $output;
}
*This is all great, except it's not rendering my links or my images -
despite me requesting a link and an image link.And my dates are rendering
like this: 2022-08-05 08:30:00 —2022-08-05 10:00:00*
*Is there a way to make those display the custom display I set in ACF?*
*Thank you!*
…On Tue, Oct 20, 2020 at 12:21 PM Bill Erickson ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
You could customize the $category_display_text using the output filter
above (it's the last parameter in that filter.
Or you could add a custom class to the listing item using the
display_posts_shortcode_post_class filter (examples
<https://displayposts.com/dps_filter/display_posts_shortcode_post_class/>
).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/1209601#gistcomment-3496969>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGBNGOXSOSTWJL754HKKRE3SLXPK7ANCNFSM4RSOYXNQ>
.[image: Web Bug from
https://github.com/notifications/beacon/AGBNGOUPVPY5L2M5H33WJYDSLXPK7A5CNFSM4RSOYXN2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGVOAS.gif]
[ { ***@***.***": "http://schema.org", ***@***.***": "EmailMessage",
"potentialAction": { ***@***.***": "ViewAction", "target": "
https://gist.github.com/1209601#gistcomment-3496969", "url": "
https://gist.github.com/1209601#gistcomment-3496969", "name": "View Gist"
}, "description": "View this Gist on GitHub", "publisher": { ***@***.***":
"Organization", "name": "GitHub", "url": "https://github.com" } } ]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Isset is checking if that variable actually exists, regardless of whether it's empty or not. You probably want
if( ! empty( $headshot ) )