What you’ve got is basically correct. This is the correct method for getting the total number of posts in the currnet category:
$category = get_queried_object();
$category_total = $category->count;
There shouldn’t be any performance impact from using this, as the count
property is only updated when new posts are published, and doesn’t need to be calculated each time it’s used.
To get the number of posts on the current page though, get_option( 'posts_per_page' )
is not the best option, because it will be the incorrect number on the last page, which could have less than the total number of posts per page.
To get that number you need to check the global $wp_query
object:
global $wp_query;
$post_count = $wp_query->post_count;
Related Posts:
- Get Category ID inside Category template
- Posts URL structure like site.com/category/the-post-title
- On the category page, get the category object
- Display Image for a category using get_categories, or show an image from any child post
- How to filter categories by custom field?
- How to display wp_list_categories on div instead of li?
- Get posts from child categories with parent category ID
- How to Make a Categories Meta Box with Hierarchical Checkboxes on Frontend?
- Add search to list of categories in post editor
- How to? Redirect /category/sci-fi/ to /category/scifi/?
- Sort category page with custom field
- How to make a relationship between books and authors?
- Create a full width responsive header image per page
- Redirecting category link to first child post
- Add specific word before the category page title
- How to have Multiple Archives Widgets, one archive widget per category (in a different page)?
- exclude category from WordPress Form function
- Render Selected Categories in Admin Edit Post Screen differently
- How can I convert some categories to a custom taxonomy?
- Previous/next link to post in category, NOT tag?
- Any way to assign Post Categories and Tags to Links?
- How to add custom script to the particular Product Category page
- get_cat_ID() not wokring
- Get all sub-categories of a parent category
- How to create a page that shows posts from multiple categories
- How to get category name from URL and pass to a template
- Displaying multiple loops based off of category
- Display Categories, Sub-categories, and Sub-sub-categories on separate pages
- WooCommerce IF statements not working
- Should the actual /category/ directory be 404? Is that normal WP behaviour
- Add “page-a” slug to category link if user visit category link from “page-a”
- create a page which displays a list of categories title+ short description?
- How to call custom category field data in theme?
- Query Posts Exclude Entire Category
- One Specific Category To Permalink
- Hide woocommerce category name from specific pages
- select parent category first then child
- WordPress category widget only show categories with children
- How to exclude children categories of parent category
- Menus don’t work after 4.4 [closed]
- Custom post and category template pagination problem
- Multiple category selection in post, shows less important category in breadcrumb
- I want to display the all the posts that are inside a certain subcategory
- How to list all categories in three divs dynamically based on the quatity of categories?
- Grouping categories by genre
- how to “not” display new posts with a specific category on the main page?
- Category template with pagination returns 404 on next pages
- Display custom taxonomy attached to the post on post single page
- How to display numbered pages in a category
- show all WordPress categories in a page
- Ive got a term (get_term_by) but now I want to filter it by a category it is in?
- Get and show all of the available categories
- how to remove the default title of Categories widget
- Recent post by custom select query
- Do not show sub categories in the loop on archive-product.php
- Having a template per category? Bad for server overhead?
- Add custom code at start and end of all posts in a category
- How to get posts by category at /%category%/ url?
- Group search results by category
- Custom row actions on a specific category
- Anchor tag generates blank href attribute using echo esc_url( )
- WordPress monthly archive category specific
- Show multiple categories in query using redux framework variable
- Add multiple orderby with pre get posts
- new WP_Query issues
- Is custom menu name and category name linked in wordpress?
- Output Title of Post’s Current Category
- Related Post by category, but not current category child
- Displaying posts of given category
- Location Based Permalink
- get list of category from a specific post with get_term()
- Return all Tags and Categories in Separate List
- Featured Category or Special Category in WordPress
- How to show given products only using product id array from specific category page in woocommerce
- Ordering terms whilst in loop
- Pass WordPress variable to the next page
- How can I list all slugs categories?
- Remove base category gives 404 error
- Woocommerce product catalog, products with different description
- How can I add metaboxs to Admin Bar?
- How to add Pagination to foreach loop to page
- get_terms sort order with child categories of varying depth
- Getting Post Tags From Certain Categories
- How to get posts from many categories using WP_Query
- WordPress pagination not working because of subcategory
- Show only one of the categories of a post in the breadcrumb?
- Check if a post is in specific category to show an icon
- Category slug in in loop always the same?
- Displaying posts by year
- How to get the post of category
- Logic for Nearby places with post and categories
- Display categories and their IDs
- WordPress a template for subcategories fo a given category, but not for root category
- Image menu showing categories in page
- Is it possible to know which category page the post was linked from?
- Category Archives: Show posts categorized in parent category only
- Change Category Page Display
- Call Current Category
- Country Ways Content Show
- Add Category Descriptions to Montezuma Theme