Currently you are looping through the array of categories retrieved with get_the_category() and for each category you retrieve two posts. In stead I understand you want to pull two posts from one category only.
To do so, you would have to remove the foreach loop and in stead define $category as the first category in the array only, like this: $category = $categories[0];
Related Posts:
- Display current category title on category page
- Inject post (from specific category) between posts in Loop
- Show content if parent page has children
- Allow authors to post only in categories they create in WordPress
- Conditional statement for parent, child and grandchild pages
- List all categories but exclude current post category on single post page
- Categories Template Assistance
- When is is_admin() available?
- Edit category output
- Check if current page is wp-admin
- Problems with function on function.php
- Custom Loop through category menu to include sub categories
- Different background-image by category
- Output 2 items within the Loop
- Get posts with at least one category in common with current post?
- Woocommerce – remove product from category
- Get category name from custom loop and echo it once
- hover image appears below placeholder instead of overlayed
- get current product name in functions.php
- Filter Select results based on selection
- How to exclude category ID from Looper in WordPress
- Add a specific part of current category page url to shortcode
- how do I get a specific post from a post with a subcategory in WP
- WP grandchild categories in nested ul li
- Pass Category Name, Description and Photo into variables to pass to jQuery
- How to show single category archive
- How to pick the default selected value in wordpress dropdown?
- Get page that displays all children of taxonomy parent
- Hide subcategories (widget)
- How to show only subcategories in parent category not parent category?
- Dilemma of Populating all the categories in a drop down list
- JSON REST API WordPress only showing first 10 categories
- Get all categories post is not in
- How can I display a div ONLY if it is not a certain term page
- Add class to DIV depending on page loaded
- Rewrite with pagination /foo/page/2/ to posts of a given category, page 2
- In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?
- How to get category name or ID by post?
- Undefined index for default custom theme option
- Post not populating for custom post type based on category selection
- Listing titles and custom fields of entries within subcategories
- random display categories – change url
- Do not duplicate posts with multiple categories in multiple loops
- Removing sidebar on category pages
- “if parent category is” conditional?
- go to home page when i select default in select-box
- Adding wp_list_categories to a specific div that was appended using jQuery
- page 1 is not paged
- Remove Custom Post Category In WordPress Slug Using .Htaccess
- Counting number of posts in multiple (sub)categories using shortcode
- Wrong Number of Products When Hiding Subcategory Products From Showing In Parent Category
- How do I edit the terms output args or array data?
- Change category thumbnail size
- Display Visual Composer shortcode if a post belongs in specific categories
- admin panel – How to remove “delete” button from category editing page
- Category Page Displaying all Pages
- Get parent category name when only child category is applied to a product
- Multiple Tag Filtering
- Link to index.php from home.php?
- How does Permalink work with the AMP plugin after Removing Parent Permalink Catalog from Posts
- Polylang and template files
- Notice: Undefined variable
- Add code to the header of posts by particular author
- Hide a specific category title from displaying on site
- Show subcategory name selected in specific category woocoommerce
- Return to the beginning of the results of a for loop
- How to get Post sidebar & footer including html like get_the_content() which only gets content section?
- Exclude category and post from loop in custom category.php
- Admin backend, show post from a category and exclude the posts from subcategories
- Populate Posts based on category selected using AJAX that means without page refresh
- random woocommerce categories are not showing when count enabled?
- Exclude a category slug on pagination
- Custom content using in_category
- How to Retrieve All Category Images on Front-End
- PHP drop down menu for my get_category child of
- Short code into PHP
- Need Quick Help With Product Category Page
- Get category id for a custom category and display it in a class
- Search in particular category in wordpress
- Is there a way to randomize and connect a background and header image?
- wp_get_archive for category returning different URLs on different but similar sites
- Error trying import one category on page
- dynamically filter by category via sub-menu
- Adding else if conditional statement to purchased theme
- Get unique post in parent category
- Code that displays images – images get shown multiple times
- Incomplete term slugs output from a foreach loop with get_the_terms
- Display post of specific category on page
- Displaying categories
- Product Category Page in Full Width
- How to insert the first letter in uppercase [closed]
- Display all categories (with link) of custom post type – WordPress
- Hide child of parent categories already hidden with “get_terms_args” – Woocommerce
- if is specific custom post in cpt
- Get category name of current post (CPT UI)
- How to limit number of posts in the certain category and exclude the oldest post automatically
- How to make my scroll bar show under condition
- Count post with tags within one category?
- child category under correct parent
- Prevent publishing of uncategorized posts