Created
November 4, 2014 04:40
-
-
Save trevorgreenleaf/3b0a00990e8866683e65 to your computer and use it in GitHub Desktop.
Using categories with the wp loop the manual way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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