Support » Fixing WordPress » How to detect subcategory then post a few more with the same subcategory

  • Resolved zyend

    (@zyend)


    I’m kinda lost on how to do this,

    Detect subcategory then post a few more with the same subcategory, sort of like I have a feature post, with Category A and Sub B, I want to detect it’s sub category then pull 6 more from the same Sub B as “Some thing you may also like…”

    Also the feature post is randomly pulled from Category A, may have different subcategory.

    One more thing, I’ve coded few themes, so ‘code-speak’ is welcomed. Just not on top of things today… and getting stuck on this.

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You could use Function_Reference/get_the_category to return the categories for a post then use that to query for other posts:

    <?php
    $parent='';
    foreach((get_the_category()) as $category) {
    if ($category->parent != 0 ) {
    $parent=$category->parent;
    }
    echo 'parent is '.$parent;
    ?>

    Thank you!

    Thanks after final changes, the code to grab subcategory of a post as follows:

    foreach((get_the_category($post->ID)) as $category) {
    	if ($category->category_parent == 4 ) {
    		$subcat=$category->cat_ID;
    	}
    };
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to detect subcategory then post a few more with the same subcategory’ is closed to new replies.