WordPress.org

Ready to get started?Download WordPress

Forums

Display Posts Shortcode
Ability to display custom field? (3 posts)

  1. cbarnett
    Member
    Posted 5 months ago #

    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://wordpress.org/plugins/display-posts-shortcode/

  2. geekhero
    Member
    Posted 5 months ago #

    This might set you in the right direction...

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

  3. cbarnett
    Member
    Posted 4 months ago #

    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 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;
    $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!

Reply

You must log in to post.

About this Plugin

About this Topic