Skip to content

Instantly share code, notes, and snippets.

@filipvanreeth
Created September 18, 2017 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save filipvanreeth/c5660d7c567e14e225a1c3dccf5e7b59 to your computer and use it in GitHub Desktop.
Save filipvanreeth/c5660d7c567e14e225a1c3dccf5e7b59 to your computer and use it in GitHub Desktop.
Get category title and posts through WP_Query
<?php
$args = array(
'cat' => set_your_id,
'posts_per_page' => set_your_posts_per_page_ex_5,
);
// Get the category object with properties
$category = get_category( $args['cat'] );
// Define WP_Query withe the arguments
$query = new WP_Query( $args );
?>
// Get the category title
<?php echo $category->name ?>
// Get the category descriptions
<?php echo $category->description ?>
// Loop through the posts
if ($query->have_posts()) {
echo '<ul>';
while ($query->have_posts()) {
$query->the_post();
echo '<li>';
echo '<p>' . the_time('F d, Y') . '</p>';
echo '<h2>' . get_the_title() . '</h2>';
the_excerpt();
echo '</li>;
}
echo '</ul>';
wp_reset_postdata()
} else {
// no posts found
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment