the_ID()
prints the current post’s ID. You need to use get_the_ID()
to return it.
Also, get_the_category
returns an array of category objects – one foreach category term the post belongs to.
The ‘category’ attribute expects a category term ID. So you need to pick a category object, then obtain its ID:
$cats = get_the_category();
$cat_obj = array_shift($cats);
$cat_id = (int) $cat_obj->cat_ID;
Then:
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => $cat_id );
Related Posts:
- Show last post from multiple categories using wp_list_categories
- Is it necessary to reset the query after using get_posts()?
- How can I custom order the results from wp_list_categories?
- wp_list_categories: get latest featured_image of category
- Exclude category by slug for for each loop
- Display page of custom posts?
- Post Loop not Returning Permalink
- Get posts that matches specific terms of multiple custom taxonomies
- Showing current subcategory
- Is get_posts() more efficient than The Loop?
- How to get_posts where ‘menu_order’ is more than 0/zero?
- Why is my loop not dynamically grabbing the correct Category and displaying all categorized posts?
- Display the_content on click jquery
- What is the maximum number of posts a WordPress loop can work with?
- Why only one post (and no pagination) on this variation of the loop?
- Sort posts by most recently modified
- A smarter way to display multiple loops inside a page template
- Publish Categories for a particular Author on a given Post
- Get all posts including sticky ones with get_posts(),setup_postdata(), and foreach loop?
- Show all posts using the template page and the loop?
- Showing sticky posts with get_posts()
- query_posts() vs get_posts() multiple loops [duplicate]
- How do I paginate a get_posts() request inside another page loop (Paginating childpages)
- Both a page loop and posts loop on the same page
- How to get The Loop working with $wpdb->get_results()
- Multiple get_posts() queries on one page
- 404 Template customization | Want 10 recent Post on the 404.php error page apart from the error Notice
- Output meta into arrays
- Get post offset/posts page offset in single post page (outside the loop)
- Loop Problem: Displays same set of post for all categories
- Category ID returns as ‘0’?
- Three Column Loop
- get_posts() forcing ignore sticky posts on main loop
- Exclude category from get_the_category
- Display all categories as plain text
- php loop error for count posts in category
- numberposts not responding to wp_reset_postdata()
- Show all child categories associated to post ID within loop
- Secondary loop cuts off at 10 posts?
- get_posts and wp_autop (remove filter)
- Need some help with “the_widget()” function
- Is there a way to target only images within the loop?
- My custom query not detecting the correct category
- How to create a custom loop ordered by Categories on a Page Template?
- Question about custom plugin
- loop mix my child-category and parent-category
- View post with specific category id and name which I selected in the backend (drop-down option)
- How to fix pagination for custom loops?
- Jquery Slider for profile template
- Counting the posts of a custom WordPress loop (WP_Query)?
- Get Posts Under Custom Taxonomy
- if ( is_home() && ! is_front_page() )
- Get excerpt using get_the_excerpt outside a loop
- How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
- Get post content from outside the loop
- Why should I put if(have_posts()), is while(have_posts()) not enough?
- Display featured products through custom loop in woocommerce on template page
- Split Content and Gallery
- Get the children of the parent category
- How to get Author ID outside the loop
- How can i display the content in plaintext
- How to check if I’m on the last page of posts?
- Redirect loop when trying to login to /wp-admin/ [duplicate]
- How to split a loop into multiple columns
- How to show related posts by category
- Is there any difference between the_title() and echo get_the_title()?
- AJAX with loop filtering categories
- WP_Query vs get_posts
- Do I need to use The Loop on pages?
- Remove the Homepage Query
- remove tags from the_content
- the_title() shows title of the first post instead of the page title?
- How to force excerpts / teasers in the loop
- Retrieve each widget separately from a sidebar
- Why am I being limited to ten posts on a custom loop?
- exclude category from get_posts?
- Should category.php and The Loop be used if the query needs to be customizable?
- Should I use loop in the single.php file?
- Inject post (from specific category) between posts in Loop
- A search for ‘0’ returns results
- How to return results of a get_posts() in explicitly defined order
- Exclude the category from the WordPress loop
- Why do themes rely on “The Loop”?
- How do I get the category URL from get_the_category?
- Display all posts from specific categories on a page
- How to return loop contents
- Using the Loop to show all levels of subpages under a parent page? Halfway there
- Cleanest Way to Select Every Second Element in a Loop?
- How to show list of posts by author and category?
- get_posts with multiple categories
- Insert image or ad script after 3 posts using the loop
- Get date of last update outside of loop
- get_template_part in for loop
- Pagination not working on home page
- Child Pages Loop
- How to place comments_template(); outside the loop?
- Multiple Loops Homepage?
- get comments and get posts in loop
- How-to exclude terms from the main query the most performant way?
- How to Change Loop to Order Posts by Views (using wp-postviews plugin)