Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, <strong>don't use</strong> <code>query_posts</code>.<br> Please, check:</p> <ul> <li><a href="https://wordpress.stackexchange.com/q/1753/12615">When should you use WP_Query vs query_posts() vs get_posts()?</a></li> <li><a href="https://wordpress.stackexchange.com/q/50761/12615">When to use WP_query(), query_posts() and pre_get_posts</a></li> </ul> <p>In a shortcode case, I'd go with <a href="http://codex.wordpress.org/Template_Tags/get_posts" rel="nofollow noreferrer"><code>get_posts</code></a>. It will return what you need and won't mess with the loop.</p> <p>It is worth noting that you don't really need to extract the attributes, using <code>$atts['id']</code> does the work.</p> <p>Follows a working shortcode, see comments:</p> <pre><code>add_shortcode( 'slider', 'shortcode_slider' ); function shortcode_slider( $atts, $content=null ) { // Initialize variable and check for shortcode content $return = ''; if( $content ) { $return = $content; } // Shortcode attribute: title="Lorem Ipsum" if( isset( $atts['title'] ) ) { $return .= '&lt;br&gt;&lt;h2&gt;' . $atts['title'] . '&lt;/h2&gt;'; } // Get our custom posts // 'category' is the category ID or a comma separated list of ID numbers $sliders = get_posts( array( 'post_status' =&gt; 'publish', 'post_type' =&gt; 'slider', 'numberposts' =&gt; 10, 'order'=&gt; 'ASC', 'orderby' =&gt; 'title', 'category' =&gt; $atts['id'] ) ); // Auxiliary variable, don't print &lt;br&gt; in the first element $first = ''; // Iterate through the resulting array // and build the final output // Use $slide-&gt;post_author, -&gt;post_excerpt, as usual // $slide-&gt;ID can be used to call auxiliary functions // such as get_children or get_permalink foreach( $sliders as $slide ) { $link = get_permalink( $slide-&gt;ID ); $return .= $first . '&lt;a href="' . $link . '"&gt;' . $slide-&gt;post_title . '&lt;/a&gt; '; $first = '&lt;br&gt;'; } return $return; } </code></pre> <p><strong>Shortcode applied in a default post type:</strong> </p> <blockquote> <p><img src="https://i.stack.imgur.com/cvXeL.png" alt="shortcode"></p> </blockquote> <p><strong>Custom post types:</strong> </p> <blockquote> <p><img src="https://i.stack.imgur.com/VnwQV.png" alt="cpt"></p> </blockquote> <p><strong>Result:</strong> </p> <blockquote> <p><img src="https://i.stack.imgur.com/fPX1U.png" alt="cpt shortcode result"></p> </blockquote> <p><strong>Useful links:</strong> </p> <ul> <li><a href="http://wp.smashingmagazine.com/2012/05/01/wordpress-shortcodes-complete-guide/" rel="nofollow noreferrer">WordPress Shortcodes: A Complete Guide</a></li> <li><a href="http://wpmu.org/how-to-add-shortcodes-to-your-wordpress-plugin/" rel="nofollow noreferrer">How to Add Shortcodes to Your WordPress Plugin</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload