Display Posts Shortcode

» Download the Plugin

Comments are closed. Please post all questions on the Support Forum.

The Display Posts Shortcode was written to allow users to easily display listings of posts without knowing PHP or editing template files.

Add the shortcode in a post or page, and use the arguments to query based on tag, category, post type, and many other possibilities.

For full documentation and examples, see the Display Posts Shortcode Wiki. I also have some code snippets that extend this plugin, which are good examples if you’re looking to do something similar.

chat25 Comments

  1. says

    I just wanted to say thank you for this code. It took some searching, but luckily I landed on your page. Nicely done.

  2. says

    Okay, so I don’t claim to be any great PHP coder. In fact I know very little. But I jsut added the excerpt to the listing. He is how I modified it:
    =====================

    <?php
    /**
    * Plugin Name: Display Posts Shortcode
    * Plugin URI: http://www.billerickson.net/shortcode-to-display-posts/
    * Description: Display a listing of posts using the [display-posts] shortcode
    * Version: 0.1.3
    * Author: Bill Erickson
    * Author URI: http://www.billerickson.net
    *
    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
    * General Public License version 2, as published by the Free Software Foundation. You may NOT assume
    * that you can use any other version of the GPL.
    *
    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    *
    * @package Display Posts
    * @version 0.1.3
    * @author Bill Erickson
    * @copyright Copyright (c) 2011, Bill Erickson
    * @link http://www.billerickson.net/shortcode-to-display-posts/
    * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    */

    // Create the shortcode
    add_shortcode(‘display-posts’, ‘be_display_posts_shortcode’);
    function be_display_posts_shortcode($atts) {

    extract( shortcode_atts( array(
    ‘tag’ => ”,
    ‘category’ => ”,
    ‘posts_per_page’ => ’10′,
    ‘include_date’ => ”,
    ‘order’ => ‘DESC’,
    ‘orderby’ => ‘date’,
    ‘image_size’ => ”,
    ‘include_excerpt’ => ”
    ), $atts ) );

    $args = array(
    ‘tag’ => $tag,
    ‘category_name’ => $category,
    ‘posts_per_page’ => $posts_per_page,
    ‘order’ => $order,
    ‘orderby’ => $orderby
    );

    $return = ”;
    $listing = new WP_Query($args);
    if ( $listing->have_posts() ):
    $return .= ”;
    while ( $listing->have_posts() ): $listing->the_post(); global $post;
    $return .= ”;
    if ($image_size) $return .= ‘‘. get_the_post_thumbnail($post->ID, $image_size).’ ‘;
    $return .= ‘‘. get_the_title() .’‘;
    if ($include_excerpt) $return .=”. get_the_excerpt() .”;
    if ($include_date) $return .= ‘ (‘. get_the_date(‘n/j/Y’) .’)’;
    $return .= ”;
    endwhile;

    $return .= ”;
    endif; wp_reset_query();

    if (!empty($return)) return $return;
    }
    ?>

      • Paula says

        Have you added the excerpt option to the plugin as yet? If yes, how do you actually add the excerpt. I have typed in “include_excerpt=” as part of my shortcode but not sure what to put after the equals sign.

  3. Clay says

    Bill:

    I see in the Codex where it should be possible to ‘AND’ multiple tags.

    $query = new WP_Query( ‘tag=bread+baking+recipe’ );

    Is this supported in the shortcode?

    TIA,
    :: Clay

    • Bill Erickson says

      I just tested it and it works. You can use + as an AND operator and , as an OR operator.

      • Clay says

        Thanks for the prompt reply. Not sure why it wasn’t working for me earlier, it now does. Perhaps a cache issue – something I do need to check for more diligently.

        Overloading this thread with some new questions …

        1) Where is the CSS for formatting the return? Because I am displaying thumbnails, I don’t need the bullet in the unordered lists and the post title is bottom aligned with the thumbnail?

        2) Is there a (simple) way of specifying the max number of characters returned in the excerpt?

        3) Then inserting a line break after the post title to start the truncated excerpt on the next line.

        TIA,
        :: Clay

        • Bill Erickson says

          1. There currently isn’t any CSS, but you can write your own using the classes that are applied to every element.

          2. You can modify the excerpt length site-wide by using the excerpt_length filter. Or you could shorten it just here by filtering the whole output and changing $excerpt in some way (see the code in further customizations).

          3. You could do this with CSS by setting .display-posts-listing .title {display:block;} or you could use the output filter to manually add a line break to the output.

          • Clay says

            As I am not adept at PHP (and I do have a client for this and it would probably cost them more for me to fumble around with this until I figured it out), what would a ballpark charge be to make a custom output filter that would let me tailor excerpt length by changing a variable, etc?

          • Clay says

            Bill – That’s very gracious of you. Thank you.

            One final set of questions as this pushes the limit of my present understanding. I have downloaded the code from github but don’t immediately see how to incorporate it into the original shortcode code. I see where a filter is referenced in the original file but am I copying this code into that file, linking to the filter which is stored in the same directory as the original shortcode?

            It also looks like the “50″ in the following

            be_truncate_phrase( get_the_excerpt(), 50 )

            Is the magic number I use to determine the amount of text displayed.

            Again, TIA,
            :: Clay

        • Bill Erickson says

          Sorry I wasn’t clearer. That code can be added to your theme’s functions.php file. The filter allows you to modify the plugin without actually editing the plugin files. That way when the plugin is updated in the future your changes don’t get overwritten.

          There’s two functions in there. The function that actually modifies the shortcode, and the function to truncate the phrase. I actually copied this function out of Genesis, so if you’re using Genesis you don’t need to add the second function and can modify the first to use genesis_truncate_phrase() instead of be_truncate_phrase().

          Yes, 50 is the magic number.

  4. Clay says

    Bill -
    Traveling back from Dallas yesterday. Cut and paste the filter code on github into functions.php and get the following error:

    Fatal error: Call to undefined function add_filter() in /…path…/wp-includes/functions.php on line 4567

    • Bill Erickson says

      Are you running the latest version of the plugin? I just added the filter a few days ago.

      • Clay says

        Running v 1.5 according to the installed plugins dashboard. File dates on the unzipped archive are 8/27 @ 8:22PM

        • Clay says

          Bill –

          Thanks for all your behind-the-scenes patience and good will. My mistakes, not being fully aware of the folder/file structure of WP.

          Works like a charm.

          :: Clay

  5. says

    First off, thanks for the plugin! Was able to get everything working except for sorting. I am trying to display a list of pages ordered by menu order, order ASC. Here is the shortcode I am using…

    [display-posts post_type="page" taxonomy="attraction-type" tax_term="Lighthouses" image_size="Attraction List" orderby="menu_order" order="ASC" ]

    Am I missing something? I just can’t get them to sort properly.

    • Bill Erickson says

      Everything looks right to me. I’ll have to do some testing to see if there’s a conflict with taxonomy queries and sorting. Does it sort correctly if you leave out the taxonomy and tax_term arguments?

      • says

        Thanks for the reply Bill. I went ahead and removed the custom taxonomy and tax_term arguments as requested and it sorted properly. [display-posts order="ASC" orderby="menu_order"]

        I’ve come across another hang-up as it seems to mess with the site template I’m using. I had to disable all plugins to identify which plugin was the culprit, unfortunately Display Posts Shortcode was it. The specific conflict i am having with the template is the home page feature banner. The theme has an option to feature pages or posts. I’ve selected pages but after activating this plugin it displays both, pages and post. The post have images not formatted to 1400 x 300 so it makes the entire homepage look like garbage.

        Sorry to spring the second issue on you. I’ve tried looking for other solutions but this seems to be the only plugin that will display both post types using custom taxonomies in a fashion that works for us.

        Thanks again

  6. Nurdan says

    Hello :) I was wondering if there was a way to add custom fields. I am using ADV custom fields plugin and would like to add 3 custom fields below the title to output for each post listed. I am not a programmer so therefore i would be ever so grateful if you could help. Thank you very much in advance!

    This is the code of php I have to insert below the title of the post. But I don’t know where to place it for it to show.

    So it would go:
    Thumbnail
    Title
    Location
    Type
    Price

    • Bill Erickson says

      Yes, this is what the “further customizations” section shows how to do. Use the filter to modify the output of the shortcode.

      Here’s code that shows specifically how to do what you’re looking for: https://gist.github.com/1209601

      Things you might need to change:
      - Name of custom fields (I used location, type and price)
      - HTML wrapping each custom field (I used span’s)
      - Order in which everything is output

      If you need help with further customization I recommend you hire a developer.