Skip to content

Instantly share code, notes, and snippets.

@trevorgreenleaf
Created November 4, 2014 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevorgreenleaf/3b0a00990e8866683e65 to your computer and use it in GitHub Desktop.
Save trevorgreenleaf/3b0a00990e8866683e65 to your computer and use it in GitHub Desktop.
Using categories with the wp loop the manual way
<?php get_header();
/*
Template Name: Cats Grid Page
*/
?>
<p>Show the cats grid</p>
<div class="well"><ul>
<?php
$categories = get_categories();
foreach ($categories as $category) {
echo '<li><a href="?c='.$category->slug.'">'.$category->slug.'</a></li>';
}
?>
</ul></div>
<hr>
<?php
// get only the cats that you get the c from the url
if(isset($_GET['c'])){
$category = $_GET['c'];
$args = array(
'post_type' => 'cat',
'category_name' => $category
);
}
// get all them cats
else {
$args = array(
'post_type' => 'cat',
);
}
$cats = new wp_query($args);
if ($cats->have_posts()) : while ($cats->have_posts()) : $cats->the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, no pages matched your criteria.</p>
<?php endif; ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment