سؤال

I'm working on a shortcode function that returns a category image, a link to the category, the last three posts in that category, and another link to the category. See my code below:

    add_shortcode('caticons_listing','bm_caticons_listing');
    function bm_caticons_listing($atts) {
        extract( shortcode_atts( array(
            'include' => '',
            'exclude' => '',
            'small' => 'true'
        ), $atts ) );
        if ($atts['include']) $include = "&include=".$atts['include'];
        if ($atts['exclude']) $exclude = "&exclude=".$atts['exclude'];
        if ($atts['small'] == 'false') $small = "&small=false";
        $listing_code .= '<table class="cat-nav">';
        foreach(get_categories("orderby=name&order=ASC&hide_empty=0".$include.$exclude) as $category) {
            if (category_description($category->cat_ID)) { $desc = category_description($category->cat_ID); } else { $desc = "Coming soon!";}
            if ($category->count > 0) {
                $seemore = '<br /><a href="'.get_category_link( $category->term_id ).'">See more.</a>';} 
                else {$seemore = '';}
           query_posts('cat='.$category->term_id.'&showposts=3');
           $listing_code .= '<tr><td>'.get_cat_icon("echo=false".$small."&class=caticon&cat=".$category->cat_ID).'</td><td valign="top" style="padding: 5px;"><a href="'.get_category_link( $category->term_id ).'"><h2 style="font-size: 130%;">'.$category->cat_name.'</h2></a>'.category_description($category->cat_ID).'<br />';
//$listing_code .= "I think I need a loop here.";
$listing_code .= $seemore.'</td></tr>';
        }
        $listing_code .= '</table>';
        return $listing_code;
    }

My issue is, with the way the WP loop examples are written, I can't figure out a good way to output the three latest posts as links in a manner that can be stored as a variable (to be returned as the shortcode output). Any help would be appreciated. Thanks!

هل كانت مفيدة؟

المحلول

First, need to lose query_posts() - it should never be used for secondary loops.

Try something like this:

$posts = get_posts( array(
    'cat' => $category->term_id,
    'numberposts' => 3,
    ));

foreach( $posts as $post ) {

    $listing_code .= get_permalink( $post->ID ); //or whatever
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top