Skip to main content
added 619 characters in body
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

Use either:

<?php $the_query = new WP_Query ('posts_per_page=2&category_name=events'); ?>

or

<?php $the_query = new WP_Query(array(
    'posts_per_page' => 2,
    'category_name' => 'events', // this is the category SLUG
));

// EDIT
Please note that category_name actually is the category slug (which is for one-word names originally the same). It is not the actual name (maybe having spaces, special characters etc. in it), though. In your case, where you just want to specifiy a certain category, you can do this either by means of the ID (cat=42) or the slug (category_name=events).

If you really want/have to use the name, you have to use get_cat_ID('Category Name'), for instance like so:

$the_query ?>= new WP_Query(array(
    'posts_per_page' => 2,
    'cat' => get_cat_ID('Events'),
));

Use either:

<?php $the_query = new WP_Query ('posts_per_page=2&category_name=events'); ?>

or

<?php $the_query = new WP_Query(array('posts_per_page' => 2, 'category_name' => 'events')); ?>

Use either:

$the_query = new WP_Query('posts_per_page=2&category_name=events');

or

$the_query = new WP_Query(array(
    'posts_per_page' => 2,
    'category_name' => 'events', // this is the category SLUG
));

// EDIT
Please note that category_name actually is the category slug (which is for one-word names originally the same). It is not the actual name (maybe having spaces, special characters etc. in it), though. In your case, where you just want to specifiy a certain category, you can do this either by means of the ID (cat=42) or the slug (category_name=events).

If you really want/have to use the name, you have to use get_cat_ID('Category Name'), for instance like so:

$the_query = new WP_Query(array(
    'posts_per_page' => 2,
    'cat' => get_cat_ID('Events'),
));
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

Use either:

<?php $the_query = new WP_Query ('posts_per_page=2&category_name=events'); ?>

or

<?php $the_query = new WP_Query(array('posts_per_page' => 2, 'category_name' => 'events')); ?>