WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to detect subcategory then post a few more with the same subcategory (4 posts)

  1. zyend
    Member
    Posted 4 years ago #

    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.

  2. MichaelH
    Member
    Posted 4 years ago #

    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;
    ?>
  3. zyend
    Member
    Posted 4 years ago #

    Thank you!

  4. zyend
    Member
    Posted 4 years ago #

    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;
    	}
    };

Topic Closed

This topic has been closed to new replies.

About this Topic