• Hi! First off, your Display Posts Shortcode plugin is great. Thank you so much for developing it. I’m using it on my client’s site for articles, publications, and more.

    I wanted to know if there is anyway to display meta data from a custom field in the displayed posts for the post lists. I tried looking at the plugin in the plugin editor but couldn’t figure out where this would be / how to do it. Basically I just want to have a custom field that could be displayed under the excerpt that could read “Published by: Sassafrass” etc. where the custom part “Sassafrass” would change according to what is input to the custom field. Does that make sense?

    Is there anyway I can do this with your plugin? Is it easy for me to add some php into the plugin myself to make it communicate with custom fields?

    I would really appreciate your help with this. Thank you.

    Sincerely,
    Candace

    https://www.ads-software.com/plugins/display-posts-shortcode/

Viewing 2 replies - 1 through 2 (of 2 total)
  • This might set you in the right direction…

    https://gist.github.com/billerickson/1209601

    Thread Starter cbarnett

    (@cbarnett)

    Hi geekhero,
    Thank you for sharing this link and sorry for the slow reply.

    Where do I add this filter? I tried editing the custom field to match my custom field (which is “publication”) instead of location, etc, and added this code directly to the php of the main plugin and it forced it to deactivate the plugin because of a syntax error so I am obviously doing something wrong. I’m not a developer so I am not well versed in php.

    Do I need to instead upload it directly to the FTP server under that plugin as an additional php file?

    How do I then with the shortcode call out the custom fields to be displayed? Will they be displayed by default or do I need to do a special parameter ie category=”articles” or something of that nature?

    Basically the name of my custom field is “publication” so would the correct code then be:

    <?php
    /**
    * Add custom fields to Display Posts Shortcode
    * @author Bill Erickson
    * @link https://www.ads-software.com/extend/plugins/display-posts-shortcode/
    * @link https://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;
    $publication = esc_attr( get_post_meta( $post->ID, 'publication', 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( $publication ) ) $publication = '<span class="location">' . $publication . '</span> ';
    
    // Now let's rebuild the output.
    $output = '<li>' . $image . $title . $publication . $excerpt . '</li>';
    
    // Finally we'll return the modified output
    return $output;
    }

    Thanks for any additional help you could give. I appreciate it!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ability to display custom field?’ is closed to new replies.