WordPress.org

Forums

Theres any php that can get the posts of a category? Please Help me! (3 posts)

  1. migbrasil
    Member
    Posted 7 years ago #

    hi, first of all i want to tell yall that im not american so my english is not that good.

    I'm here to ask if theres any php that can get the the posts of a category. Like, i want to insert that on my sidebar, so when people go to a certain post the php get the category of it and show all of the posts of that category, so they dont have to go back to choose another post of that category.

    If yall cant understand what im trying to say, please tell me, so i can try to explain in another way.

    Thanks

  2. Kafkaesqui

    Posted 7 years ago #

    Since when does being American make for good English? ;)

    There are a few built-in tools in WordPress which you can use to collect the posts of a category:

    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Template_Tags/query_posts

    Getting the category the currently viewed post resides in outside of The Loop can be a little complicated, but this should do it:

    <?php
    global $post;
    $categories = get_the_category();
    $category = $categories[0];
    $cat_ID = $category->cat_ID;
    ?>

    $category is an array which will hold the first (or only) category a post is assigned to. You can pass either get_posts() or query_posts() the category ID # using $cat_ID. Example:

    $myposts = get_posts("numberposts=5&category=$cat_ID");

  3. mfshearer72
    Member
    Posted 7 years ago #

    Also, if using sidebar widgets, get a php code plugin (e.g. Samsarin PHP Plugin to put the above code into a sidebar.

    Further, here is a plugin so that you get all the posts by category when viewing the category: Other Posts from Cat plugin

Topic Closed

This topic has been closed to new replies.

About this Topic