You can get the number of posts in the current loop from the $wp_query
object. How you do it will depend on whether you use the main loop or a custom one.
Main loop
Use the global query object.
<?php
global $wp_query;
$is_single_post = ($wp_query->post_count === 1);
?>
<?php if ($is_single_post): ?>
<div class="one-post"></div>
<?php else: ?>
<div class="multi-post"></div>
<?php endif; ?>
Custom loop
Use your own query object.
<?php
$args = array(
// fetch post type and whatnot
);
$query = new WP_Query($args);
?>
<?php if ($query->have_posts()): ?>
<?php if ($query->post_count === 1): ?>
<div class="one-post"></div>
<?php else: ?>
<div class="multi-post"></div>
<?php endif; ?>
<?php endif; ?>
Related Posts:
- What does this PHP function code mean? [closed]
- Get the ID of the latest post
- Possible to change the URL for the regular post type without affecting the URL of other custom post types?
- Change permalinks for posts but not for custom post types
- Front end post editing using a form
- Set posts of a custom post type to be private by default?
- How to add a button to custom post type’s posts-page
- How can I add a column/s to wp_posts table?
- How make get_next_post() return first post when viewing last one
- post_row_actions for custom post type
- modify all posts of a category to “no comments allowed”
- Getting a div content of a post in wordpress?
- How can I add dropdown widget/box to admin post page?
- Custom permalinks – post type – hierarchical taxonomy’s
- How to show related posts by category or custom post type?
- ascending order custom post type
- add_meta_box: Datepicker like the one for postdate?
- [Plugin: Posts 2 Posts] Changing display order of connections
- Display taxonomy term only if there’s a value
- How to show post title outside of loop? [closed]
- How do you use JavaScript to detect the homepage
- How to delete all posts from a custom post type?
- Update content of multiple pages/posts at once?
- Display types of posts in edit.php instead of All, Published, Scheduled and Draft
- CPT’s archive and single page not working [closed]
- Custom Taxonomy – Tags Metabox is showing instead of Categories
- Trying to edit the single page from a Custom Post
- How to Submit Post Via cURL & PHP
- apply filters only to specific post listing without check the url parameters
- How Can I Set the Post Author of a Post I Just Created With PHP?
- WordPress Orderby Numeric Value Not Working
- is there a way to retrieve posts that do not have a featured image assigned?
- Missing Posts in Custom Taxonomy List
- Post image in WordPress not appearing on home page
- custom post type category count shortcode
- How to integrate a form (Ninja Form or Contact Form 7) with Custom Post Types?
- How to sort posts in a custom post type by title in ascending order by default?
- Conditional for a Single Post That Belongs to a Category?
- $post not working working in AJAX plugin with custom post type
- Create new custom post and post category of same name
- Multiple pages per post (not pagination)
- Custom post type menu
- wordpress lists similar type of posts in a custom post type
- Auto post creation
- Modify Posts from Custom_Post_Type within the plugin
- How do I edit the WordPress post.php file?
- Posts are not rendering perfectly [closed]
- How I can made a custom post type to page templates?
- List all posts in taxonomy term
- list posts of two post types in a single template
- how to redirect to a custom post template
- Post/Custom Post Type URL Access
- Adding /blog in front of single posts (only)
- Why is my custom post content only viewable when signed into WordPress?
- Add a form in every post and save data in post meta
- list articles in admin showing nested categories or slugs?
- Search CPT by tag ids
- Post template with breadcrumb needs to show page navigated from
- Define multiple prefixes for custom post type
- Publishing failed. The response is not a valid JSON response
- add all blog posts to folder
- Limiting the Number of User Posts to Their Own Posts
- Correct way adding External classes to Custom Post type and output their methods to template
- Show posts from WP Custom Post Type selected from a field in a metabox
- order custom post type posts by custom date
- How to show posts of the same category on a page?
- Assign a Post to a User
- News post with multiple permalinks? When opened from different page
- custom post type to post association in wordpress
- Rest Api not working with Iframe in form data
- Need to display author’s email id in the “Edit post” field in wp dashboard. How do I do this?
- Display title of all custom posts from same taxonomy term on a custom post template
- Use WP pagination functions on a custom page template
- Event Manager: Events Deleted are still in DB as published but hidden in admin
- Rewrite url post_tag for custom post type
- How to get all posts related to a taxonomy?
- call a function when insert and update a custom post type
- Work and Display a Custom Post Type as a normal Post
- Most efficient way of showing children posts?
- Single Post (CPT) in two pages – Normal and Extended
- Limit or filter edit view in the dashboard to a specific ID only?
- Custom post type with two templates
- One Post with different content, depending on a Page
- Custom Post Type
- Popup panel is only displaying 1 entry ignoring all others
- Custom Post Status & Taxonomies
- How to display data with pagaination on backend?
- Edit post meta direct from post.php?
- How to Arrange Support => Thumbnail
- WordPress Author Posts Review After Every Change In The Same WordPress Post
- Front-end form submission not creating post
- Post being scheduled that shouldnt be schedulded
- Post type cloud like tag cloud?
- Get custom post type parent category
- How to delete unnecessary custom post types in the UI
- Display posts from another page on home page in wp-editor
- show the most recent date of all posts to display on front end
- Error 403 when posting comments to a custom post type from a different page
- Two pagination in one page without AJAX
- Change post-name when inserting new Post if Specific Category is selected in WP