Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #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 posts

    Kind regards,
    hermelina

    #585747

    Hey 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,
    Basilis

    #585963

    Hey 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,
    hermelina

    #589600

    Hi!

    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

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.