WordPress.org

Forums

How to get single post categories and then all the posts under that category (3 posts)

  1. nahidgeb07
    Member
    Posted 11 months ago #

    I have multiple posts of different categories in my index page, and whenever I click on a single post title it opens at the single.php page with its all content. So far so good! now on single.php page I want to find out the category/categories of that particular post and show all the posts under those categories. Could anyone please help me to figure this out?

  2. alchymyth
    Forum Moderator and Sweeper
    Posted 11 months ago #

    find out the category/categories of that particular post

    review http://codex.wordpress.org/Function_Reference/get_the_category
    or
    http://codex.wordpress.org/Function_Reference/wp_get_post_categories

    show all the posts under those categories

    create a secondary query and loop using the retrieved category ids;
    https://codex.wordpress.org/The_Loop
    http://codex.wordpress.org/Class_Reference/WP_Query
    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    the edits would go into single.php;

    what theme are you working with?

  3. nahidgeb07
    Member
    Posted 11 months ago #

    Thank you for your response. So far I have found the way to retrieve the category name I am looking for but can't find a way to pass this category name to the loop so that I can retrieve the posts under this category. Please help

    I am using this query to get the category name
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>

Reply

You must log in to post.

About this Topic

Tags