From the codex for get_posts()
The category parameter needs to be the ID of the category, and not the category name.
So you should either use category_name
parameter with name or you could pass the ID to category
.
<?php
$term_name = get_queried_object()->name;
$args = array ( 'category_name' => $term_name, 'posts_per_page' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
or
<?php
$term_id = get_queried_object()->term_id;
$args = array ( 'category' => $term_id, 'posts_per_page' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
Related Posts:
- How can I get archives for specific category without category_base in the url?
- Modify text after post count
- Get archives as array
- Get posts from a top category and group by child categories
- Any kind of custom post type in the front page and in the archive listing
- Parent and child relation code and display only first child list
- Trying to hide subcategories which have categories without posts
- Exclude a specific category link from archive / front page
- Filter the post listing for a particular category from archive month list
- Get rid of WordPress category, tag and author archives?
- How To Create A Paginated List Of All Categories On My Site?
- Filtering a custom post type by custom taxonomy in archive template
- Category archive by year with permalink support /category/YYYY
- How can I lock down an old wordpress install I don’t intend to update?
- 404 Error On Category and Tags Pages
- How can I get the Month Name from Archive?
- Archive widget – limit number of months to 12
- How to hide a specific category posts in my monthly archive?
- Optimal way to redirect home page to category archive?
- Output yearly archive within a page
- How would you create a “weekly” archive?
- Sticky Posts Not Sticking to Top of Category Archive
- How to show specific year archive into dropdown list
- How to make a custom Archive Page
- How can I change the way dates shown in the archive widget?
- how do I get the date in a date archive page
- I want exclude the particular category in sidebar
- Arrange and separate posts
- What type of page should i use? static page or category page? and what’s the difference? [closed]
- if there is only one post in the category, directly open
- How to disable monthly archive
- Sorting posts by season
- weekly archive for custom category
- WordPress subcategories return 404 in custom template
- How to Create Custom Archive page
- How to show year/month archive including months with no posts
- Displaying Archives List
- Category index featured image
- How to change category and year archive permalinks/rewrite rules?
- Some archive by year page return 404
- Get parrent category name
- Custom archive.php: retrieve right post categories?
- wp_dropdown_categories() scripts working erratically on sidebar
- If one taxonomy contains the same term as another then display post
- Yearly Archive for Categories with dropdown
- Having a problem with Next and Previous Links
- Category archives with monthly pagination
- Checking if a specific date archive is being displayed
- Wrap the “show_post_count” between div
- Is a list page usually called an archive page in WordPress?
- I want my yearly archives to show a list of all posts
- Category archive is displaying all posts rather than the specific category
- Archive page showing wrong month
- How to Get The Excerpt of the page that displays Blog Posts
- About title on a page
- Create simple columned archive page
- I created a normal page, but the page displays as an archive page
- Secondary WP_Query being overriden in archive page
- Combining custom taxonomy posts with default posts on one archive page
- Create a custom URL for blog archive
- Post-Archive like a page in a specific subdirectory
- Problem with multiple loops in wordpress theme
- Displaying year once in category.php [duplicate]
- Archive Template being used instead of Category Template for Custom Post Type
- Show Daily Archives after specific dates
- Custom archive pages without “the loop”
- show only sub categories if available?
- Highlighting Current Category in the Archive and Feeds page
- List of post categories only associated to another custom taxonomy
- Archive page no content to display
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?
- Link to index.php from home.php?
- Posts are not displaying on their category and tag page
- I want to be able to access archives to see how recently a post has been read: How can I do that?
- Woocommerce product not appearing in category list page when created programatically [closed]
- Display all posts from specific category
- Meta Box: display single image on an archive-template
- Make category archives page in menu non-clickable
- show image gallery in archives or category page
- Adding Custom Post Types to category/tag/author archives breaks header content
- How to remove or hide archive page?
- Custom Category Archive Pages
- Show full category tree for a year with all post titles?
- How to display total views of all posts of one category?
- Create monthly archive for parent category
- How to count the number of monthly archive?
- Transfering static site to wordpress and retaining category by month paginations
- How to edit wordpress post without republishing
- Archive by Year
- dynamically filter by category via sub-menu
- Stop header code from showing in category page?
- How can i display in themplate file only a few post
- A problem with date archives
- how to hide all posts under one category
- Show monthly or daily archives
- AZ Directory posts directory index
- Category archive page has two URLs even when default theme is active with no active plugins
- How to have infinite scroll consider passed parameters?
- Sort Category Archive pages and place sticky posts first
- How to display the archive for native posts