Just change this one line:
$cat_obj = $wp_query->get_queried_object();
$thiscat_id = $cat_obj->term_id;
$thiscat = get_category($thiscat_id);
$chi = array();
if (!empty($thiscat->parent)) {
$parentcat = get_category($thiscat->parent);
$categories_chi=get_categories(
array( 'parent' => $parentcat->cat_ID )
);
foreach ($categories_chi as $key => $value) {
$chi[] = $value->cat_ID;
}
$pare = array($parentcat->term_id);
$ids = array_merge($pare, $chi);
} else {
$ids = array($cat_obj->term_id);
}
$related = get_posts(array( 'category__in' => $ids, 'numberposts' => 10));
Related Posts:
- Inject post (from specific category) between posts in Loop
- Looking to exclude blog posts from category Previous/Next buttons
- Use template for posts with a particular category grandparent
- how do I get a specific post from a post with a subcategory in WP
- How to show only subcategories in parent category not parent category?
- Get latest post from all categories except one
- Add new post in existing categories using wp_insert_post
- Displaying list of posts in category page
- Do not duplicate posts with multiple categories in multiple loops
- Loading Posts & Category with Ajax
- Pagination at category doesnt work with same name of page
- How to show categories and date on posts
- Trying to show the category of a post in the post display
- Displaying 3 Category Posts differently
- Unable to get Post Category Name and URL
- Trying to see if page is category or single and displaying title with appropriate heading tag
- Display post of specific category on page
- Custom SQL query to get List of posts with featured image url
- Getting key value of WP_Term object in wordpress
- get_the_content if it contains multiple lines it results in SyntaxError
- Automatic Excerpt Not Working
- Edit category output
- WP Insert Post function – Insert Post Thumbnail
- How is WordPress manipulating the posts like there are folders?
- Different background-image by category
- Add HTML Attributes To Anchor Tags In `wp_list_categories()` Function
- Get taxonomy terms only of the WP_Query current posts
- How to get 2 or multiple custom post types in wordpress functions.php
- Where do posts get the sidebar from?
- get post based on category chosen in drop down – The ajax method
- Proper syntax or method for keeping url in modified isotope / category links
- Get page that displays all children of taxonomy parent
- Which hook/action will help me solve my problem?
- selected option if current category is the value
- Open post-content in archive page in a Modal box with bootstrap
- Alert Bar section within WP loop is displaying even though there are no posts
- Get children post mime type using parent post_ID wp_post
- Redirect to another page using contact form 7? [closed]
- How to avoid duplicates when creating recent network posts
- Update post title from external file?
- set post limit at load more ajax wordpress
- Array to string conversion on array_map
- Store metakey value as an array
- How to get category name or ID by post?
- How to use mysql LIKE with wpdb?
- Do I need to edit my theme in order to change the title of my blog page?
- Post not populating for custom post type based on category selection
- Listing titles and custom fields of entries within subcategories
- Incrementing PHP variable onclick to display posts by month
- random display categories – change url
- Removing sidebar on category pages
- WordPress Search
- Update value of a associative array with update_post_meta
- Wrap posts p tags in div
- How to display only one category in a custom post type?
- go to home page when i select default in select-box
- Adding wp_list_categories to a specific div that was appended using jQuery
- Counting number of posts in multiple (sub)categories using shortcode
- Edit and delete permissions pages and posts
- What is the best way to set the post category (Custom Post Type) automatically based on the one of the tags assigned to the post?
- Change category thumbnail size
- Post form – AJAX won’t upload featured image – Plugin development
- Display Visual Composer shortcode if a post belongs in specific categories
- admin panel – How to remove “delete” button from category editing page
- posts_per_page displays only 2 posts instead of 4 posts
- Get parent category name when only child category is applied to a product
- Multiple Tag Filtering
- Infinite looping next post link within a certain category on a post
- WordPress recent post
- Polylang and template files
- Need to show 7 posts from actual date
- Notice: Undefined variable
- Can I disable auto increment of WordPress Posts Database to utilize previously deleted records?
- Show subcategory name selected in specific category woocoommerce
- How to use a PHP file to control the content of a blog post
- My blog posts section seems to be rendering the other blog posts inside the first? :/
- LinkedIn Share Post Button
- Custom content using in_category
- Get the total Author Favorited posts
- How do I generate formatted permalinks as specified in backend options?
- PHP drop down menu for my get_category child of
- Short code into PHP
- Using Tag Groups: Displaying groups and adjacent tags of current post
- Setting at job with shortcode not working
- Display 6 most recent post on homepage?
- Only Get A Certain Number of Posts From WP_Query
- Rearrange get_categories array compared to another array
- Insert wordpress tags below posts via functions.php
- Search in particular category in wordpress
- Query seems to be duplicated
- Top menu disapears in Category Pages when filtering using ACF
- Error trying import one category on page
- Add Custom Post Type on the Fly or create an array
- Remove the_content From Loop
- Category List in Theme Options Page
- link to page_for_posts
- Displaying categories
- How to create a loop that will display one post and stop?
- $post->ID calls for current page, but what is the code to call for any new page created and published in WordPress?
- Add multiple HTML attributes to an Elementor button