• I currently have the following code which I use for showing more posts within my single.php after the content.

    <?php global $post; $args = array('showposts' => 3, 'post__not_in' => array( $post->ID )); query_posts( $args ); if (have_posts()) : while (have_posts()) : the_post(); ?> 
    
    [html stuff here]
    
    <?php endwhile; wp_reset_query(); endif; ?>

    However I would like to add a category parameter to pull more posts depending on the category the original post is filed under.

    'category_name' => '[code here to get the category name within single.php]'

    For instance, let's say my Post is filed under technology, I would like pull more technology related posts with the above query. However since I have multiple categories, is there a way to dynamically pull the category the post is filed to so that it can choose the relevant stories?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The resource provided by @michael is the perfect way to understand how it works, the quick solution is to use the_catgory() function.

    the_category() invokes another core function get_the_category_list(), which gets associated categories and displays them on screen with their permalinks.

    <?php the_category(); ?>

    However, if you just want to display the names of the categories (without links), you can do this.

    $cats = get_the_category();
    foreach($cats as $c) {
       echo esc_html($c->name) . ', ';
    }
    Thread Starter bcw00

    (@bcwint00)

    Ok I think my explanation is not the best. Within my single.php, I would like to have a section to display the latest posts from the category that the story is filed in.

    If my story is filed in technology for instance, I would then like the WP_Query to determine what category the main post is filed under (whether it be slug, name, or ID) and then add it as a condition within my WP_Query function.

    Currently the function (original post) displays three posts and excludes the current post you are viewing (showing it be within the line up). I would then like to add another condition to pull the category so that the displayed posts are pulled only from the category that the single.php story is listed under.

    Any help? Not too savvy with the coding, so I dunno if anyone has a working example to show me? Thanks.

    Moderator bcworkz

    (@bcworkz)

    Your OP was clear enough, I think Michael was expecting you to connect the dots yourself, assuming more knowledge than you actually have. And Subrata was suggesting an alternative — linking to related posts instead of listing them. This can be a good approach for some sites.

    To accomplish what you want, first you need to get the category of the current post. As Michael suggested, you use get_the_category() within the Loop. You will get an array of category term objects, even if only one term is assigned. You can arbitrarily use the first term or use all of them, or code some sort of custom logic to select.

    After the Loop, you need to make a secondary query that gets X number of posts in the categor[y|ies] determined previously. Also exclude the current post. You may use get_posts() for this, which is basically a wrapper for instantiating a new WP_Query object. I suggest the latter as it helps you understand what’s going on under the wraps.

    The arguments you will utilize include ‘posts_per_page’, ‘category__in’, and ‘post__not_in’. Get the ID for this last argument with get_the_ID() while inside the Loop. After instantiating the class with these arguments (you get the latest posts by default), you run a loop to list the posts found. This loop is very similar to the main Loop, except you use class methods instead of procedural functions. For example, instead of the_post();, you might use $the_query->the_post();

    Examples of secondary loops and the use of arguments are all on the linked Codex page. All the examples work, but I cannot point you to an example that exactly meets your need, though something likely exists. You should be able to piece together what you need from parts of various examples, following the logic that I’ve lined out.

    Give it your best effort. If you still have trouble, post the code you ended up with and someone should be able to help you further.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting single category for WP_Query’ is closed to new replies.