Last active
December 7, 2018 16:29
-
-
Save justiny/4e416f99563e99dfc2f959508425823e to your computer and use it in GitHub Desktop.
Get latest posts from category, excluding current post
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 | |
// convert to timber | |
$current_post_id = get_the_ID(); | |
$current_post_cats = get_the_category(); | |
$current_post_first_cat_id = $current_post_cats[ 0 ]->term_id; | |
$args = array( | |
'cat' => $current_post_first_cat_id, | |
'post__not_in' => array( $current_post_id ) | |
); | |
$context['related-posts'] = Timber::get_posts($query); |
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 the current post id. | |
$current_post_id = get_the_ID(); | |
// Get the current post's category (first one if there's more than one). | |
$current_post_cats = get_the_category(); | |
$current_post_first_cat_id = $current_post_cats[ 0 ]->term_id; | |
// Setup arguments. | |
$args = array( | |
// Get category's posts. | |
'cat' => $current_post_first_cat_id, | |
// Exclude current post. | |
'post__not_in' => array( $current_post_id ) | |
); | |
// Instantiate new query instance. | |
$my_query = new WP_Query( $args ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment