Genesis Custom Loop by category

You can customise your Genesis loop to show only categories you specify. For example, you have four categories but only wish to have one or two categories show up in your loop.

Replace all loops

If you wish to replace all the loop output across the site, simply add the following code to functions.php.

[php title="functions.php"]
remove_action(‘genesis_loop’, ‘genesis_do_loop’);
add_action(‘genesis_loop’, ‘custom_do_cat_loop’);

function custom_do_cat_loop() {
	global $query_args; // any wp_query() args
	$args= array(‘cat’ => ’1’);
	genesis_custom_loop(wp_parse_args($query_args, $args));
}

[/php]

The above code will replace the default Genesis loop with a custom one that only displays posts from category ID 1. The wp_parse_args() function combines the $args array with the current $query_args for that page.

Replace page loop

If you want to replace only the loop on a page, create a template and set the page to use the template.

For example, if I want to replace the loop on my Portfolio page, I create page-portfolio.php and select Portfolio as the template for the page.

[php title="page-portfolio.php"]
<!--?php
/**
*
* Template Name: Portfolio
* This file displays posts within the specificed category for any pages using this template.
*
*/
remove_action(‘genesis_loop’, ‘genesis_do_loop’);
add_action(‘genesis_loop’, ‘custom_do_cat_loop’);

function custom_do_cat_loop() {
	global $query_args; // any wp_query() args
	$args= array(‘cat’ =--> ’1’);
	genesis_custom_loop(wp_parse_args($query_args, $args));
}

genesis();

[/php]

Target categories

There are several ways to target the category you wish to filter:

  • cat – (int) using category ID
  • category_name – (string) using category slug, not name
  • category__and – (array) – using category ID
  • category__in – (array) – using category id
  • category__not_in – (array) – using category id

Show posts for a single category

Display posts that have this category (and any children of that category), using category id:

array( 'cat' => 4 )

Display posts that have this category (and any children of that category), using category slug:

array( 'category_name' => 'staff' )

Display posts that have this category (not children of that category), using category id:

array( 'category__in' => 4 )

Show Posts From Several Categories

Display posts that have these categories, using category id:

array( 'cat' => '1,8,11,14' )

Display posts that have these categories, using category slug:

array( 'category_name' => 'blog,portfolio' )

Display posts that have all of these categories:

array( 'category_name' => 'blog+portfolio' )

Exclude Posts Belonging to Category

Display all posts except those from a category by prefixing its id with a ‘-‘ (minus) sign.

array( 'cat' => '-7,-23,-31' )

Multiple Category Handling

Display posts that are in multiple categories. This shows posts that are in both categories 1 and 8:

array( 'category__and' => array( 1, 8 ) )

To display posts from either category 1 OR 8, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

array( 'category__in' => array( 1, 8 ) )

You can also exclude multiple categories this way:

array( 'category__not_in' => array( 1, 8 ) )