Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@replaid

This comment has been minimized.

Copy link

replaid commented Jun 12, 2014

Here's info on how to make this conditional on shortcode attributes: http://wordpress.org/support/topic/plugin-display-posts-shortcode-custom-fields-clarification

@RonR-WebDesign

This comment has been minimized.

Copy link

RonR-WebDesign commented Mar 26, 2016

Just found this and have a question. Is this replacing or adding to the actual shortcode? Here I see $title - do I then not have title in the shortcode?

@ReddyRedisson

This comment has been minimized.

Copy link

ReddyRedisson commented Jan 27, 2017

Good day! Maybe you can help me. I did everything as written above, but one of my fields - is the picture and plugin displays html code instead of images.

@wisebydesign

This comment has been minimized.

Copy link

wisebydesign commented Feb 10, 2017

Hey Reddy, this is what you need to get the featured image of a post type: get_the_post_thumbnail( get_the_ID(), large )
In this example, I display a logo (set as featured image) as an image link, and make it click through to a partner website. This uses a custom post type in which I have a set featured image and a custom field made by ACF (advanced custom fields) for the website. This particular snippet is NOT on the functions page, but on my custom post type file "single-partners.php"
if ( has_post_thumbnail() ){ echo '<a class="partnerlogo" href="' . get_field('partner_website') . '">' . get_the_post_thumbnail( get_the_ID(), large ) . '</a>'; }
The "if has post thumbnail" function means if there is no image set, it won't display (broken info).

  1. The echo '<a class="partnerlogo" href="' is printing the beginning of the a link in html ,
  2. Then the get_field('partner_website') gets the ACF custom field called partner_website, where the user has inputted a url,
  3. then '">' closes the beginning of the tag. When you echo '', anything between the '' is printed as html.
  4. Then the get_the_post_thumbnail( get_the_ID(), large ) gets the featured image and the large sets the size. you can also use thumbnail or medium etc.
  5. The ''; closes the entire a tag. Don't forget your ; at the end of your line, otherwise it breaks the page.
  6. The } closes the function.

If the image is set via an ACF custom field, not as a featured image, you need to create the custom image field in ACF, then in its settings, choose a return value of either array, ID or URL then use ACF advice to determine how to display it: https://www.advancedcustomfields.com/resources/image/

good luck. :O)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.