Created
September 18, 2017 08:26
-
-
Save filipvanreeth/c5660d7c567e14e225a1c3dccf5e7b59 to your computer and use it in GitHub Desktop.
Get category title and posts through WP_Query
This file contains hidden or 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 | |
$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