0

我正在尝试使用 query_posts 从类别中检索帖子,但它从未指定的类别中返回帖子,其他参数(例如排序和 showposts)可以正常工作。

extract(shortcode_atts(array(
            'categories' => 'Partners',
            'imageswidth' => '200px',
            'imagesheight' => '115px',
            'imageslink' => 'Web_Link',
            'partnerscount' => 'All',
            'columnscount' => 1,
            'imagescrop' => 'No',
            'imagesblackhovercolor' => 'No',
            'imagesopacity' => 100,
            'imagesgrayscale' => 'No',
            'imagescolorize' => '',
            'imagesnegative' => 'No',
            'imagessort' => 'Date ASC'
        ), $atts));

        //$args = "category_name=".categories;
        //$catIDs = get_cat_ID( $cat_name='Partners' );

        $args = "cat_name=".categories;
        $args .= strtolower($partnerscount) != "all" ? "&showposts=".$partnerscount : '';
        $args .= $imagessort == "Date_ASC" ? "&orderby=date&order=ASC" : '';
        $args .= $imagessort == "Date_DSC" ? "&orderby=date&order=DESC" : '';
        $args .= $imagessort == "Random" ? "&orderby=rand" : '';



        query_posts($args);

while ( have_posts() ) : the_post();
        {
$output = $output.get_the_title();
}
4

1 回答 1

1

根据wordpress文档(http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

为了显示某个类别的帖子,您可以使用下一个参数:

cat (int) - 使用类别 ID。

category_name(字符串)- 使用类别 slug(非名称)。

category__and (array) - 使用类别 ID。

category__in (array) - 使用类别 ID。

category__not_in (array) - 使用类别 ID。

正确的参数是:category_name而不是cat_name。更换:

$args = "cat_name=".categories;

和:

$args = "category_name=".$categories;

虽然Partnersslug该类别,但应该可以解决您的问题。

编辑1:

请注意,你写categories的时候没有 $ 符号。PHP 将其视为已定义而非变量。试试这一行: $args = "category_name=".$categories;

编辑 2: 如果您无法获取该类别的 slug,请尝试通过其名称获取其 ID。

    $catID = get_cat_ID( $categories );
    $args = "cat=".$catID;
于 2013-05-21T08:14:33.320 回答