0

I am trying to retrieve post from a category using query_posts but it returns post from the unspecified category other parameters such as sort and showposts works fine.

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();
}

1 Answer 1

1

According to the wordpress documents (http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters)

In order to show posts from a certain category you can use the next parameters:

cat (int) - use category id.

category_name (string) - use category slug (NOT name).

category__and (array) - use category id.

category__in (array) - use category id.

category__not_in (array) - use category id.

The right parameter is: category_name and not cat_name. Replacing:

$args = "cat_name=".categories;

with:

$args = "category_name=".$categories;

While Partners is the slug of the category, should solve your problem.

Edit 1:

Please notice , you wrote categories without the $ sign. PHP treats it as a defined and not as a variable. Try this line: $args = "category_name=".$categories;

Edit 2: If you can't fetch the slug of the category, try getting the ID of it by its name.

    $catID = get_cat_ID( $categories );
    $args = "cat=".$catID;
4
  • when I try category_name it does not return anything at all
    – Syed Waqas
    May 21, 2013 at 8:19
  • Apparently, Partners is not the slug of the category. May 21, 2013 at 8:20
  • Edited my answer, try the Edit1 & Edit2 suggestions. May 21, 2013 at 8:23
  • I was specifying category name instead of the slug, by changing to slug it solves the problem. thanks
    – Syed Waqas
    May 21, 2013 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.