-
AuthorPosts
-
February 18, 2016 at 7:59 pm #585635
Hello folks,
could you please help me to customize the related posts-section? I would like to
– exclude a certain category
– show only 4 postsKind regards,
hermelinaFebruary 18, 2016 at 10:21 pm #585747Hey hermelina!
To exclude categories, open includes > related-posts.php. Find this code.
$my_query = new WP_Query(“tag=$tag_ids&showposts=$postcount&ignore_sticky_posts=1&orderby=rand”);
Add exclude category. Find the category ID to be excluded.
$my_query = new WP_Query(“tag=$tag_ids&showposts=$postcount&ignore_sticky_posts=1&orderby=rand&cat=-12,-34,-56”);
Please let me now if that works out for you.
Best regards,
BasilisFebruary 19, 2016 at 11:52 am #585963Hey Basilis,
thanx for your quick answer! Unfortunately I can’t find a code like $my_query = new WP_Query…
My related_posts.php includes$my_query = get_posts(
array(
‘tag__in’ => $tag_ids,
‘post_type’ => get_post_type($this_id),
‘showposts’=>$postcount, ‘ignore_sticky_posts’=>1,
‘orderby’=>’rand’,
‘post__not_in’ => array($this_id))
);I tried this:
‘orderby’=>’rand’, ‘cat=-78’,
also
‘orderby’=>’rand’ & ‘cat=-78’,
but it does not work.Cheers,
hermelinaFebruary 26, 2016 at 9:16 am #589600Hi!
The previous code that we suggested should work. Modify this line:
$my_query = get_posts( array( 'tag__in' => $tag_ids, 'post_type' => get_post_type($this_id), 'showposts'=>$postcount, 'ignore_sticky_posts'=>1, 'orderby'=>'rand', 'post__not_in' => array($this_id)) );
.. add the “cat” property plus the id of the category that you want to exclude appended with a minus sign. Example:
$my_query = get_posts( array( 'tag__in' => $tag_ids, 'post_type' => get_post_type($this_id), 'showposts'=>$postcount, 'ignore_sticky_posts'=>1, 'orderby'=>'rand', 'post__not_in' => array($this_id)), 'cat' => '-12,-34,-56' );
https://codex.wordpress.org/Class_Reference/WP_Query
Numbers of post is based on the number of columns by default. If you want to override that look for this code around line 55:
$postcount = ($columns * 1);
Adjust the value.
Cheers!
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.