• Resolved danielpol

    (@danielpol)


    Hi, I’m quite new to WP and not really a coder

    I have several categories and subcategories on a WP site and when a parent category is selected i want to display the post in it and also the posts from its children.
    the problem is that the code I have now https://pastebin.com/1Q4RSKFi it shows only the posts from child category and if there is no child category it works just fine, I mean it shows all the posts from current category
    Any help appreciated.
    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • when a parent category is selected

    how does the category get selected?
    is it essential that it is a parent category?
    what template is using this code?

    get_the_category() will get the categories of the current post (which might not be what you want ?):
    https://codex.www.ads-software.com/Function_Reference/get_the_category

    if this is a category archive, get_query_var('cat') would get the selected category;

    you could use this in the query:

    $catg = new WP_Query('post_type=>any&cat=' . get_query_var('cat').'&orderby=date&order=desc&showposts=100');

    (no foreach needed)

    Thread Starter danielpol

    (@danielpol)

    when a parent category is selected

    i mean when you click on it

    what template is using this code?

    this would be category.php

    ..but your solution actually works. Thanks alchymyth. I owe you.

    And if we’re here maybe you can help me with my other issue. I have the calendar widget and i want to display the posts from a specific day.
    I supose I’ll have to create date.php.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to display posts from category and sucategory’ is closed to new replies.