This should do it:
$real_current_post = $wp_query->current_post + 1;
switch( $real_current_post ){
case 2:
// Do something for 2
break;
case 5:
// Do something for 5
break;
case 7:
// Do something for 7
break;
}
You may need to globalize $wp_query, but as long as you’re using the normal template hierarchy, it should already be in scope. If you do use global $wp_query;
, use it before the loop starts.
Related Posts:
- remove_action or remove_filter with external classes?
- Why, Where, and When to use reference pointers in filters/hooks?
- Combining action and filter?
- Suppress the_content filter in a nested loop
- Passing a parameter to filter and action functions
- Difference Between Filter and Action Hooks?
- add_action reference a class
- How to know what functions are hooked to an action/filter?
- Why does save_post action fire when creating a new post?
- Get a list of all registered actions
- How can I edit post data before it is saved?
- add_action(), add_filter() before or after function
- How to create an API for my plugin?
- Trouble understanding apply_filters()
- What is the very earliest action hook you can call?
- Remove parent theme action in child
- remove custom post type permalink
- How to know what priority to use with add_action()?
- Where is the best place to use add_filter
- Remove Actions/Filters added via Anonymous Functions
- wp_headers vs send_headers. When to use each?
- How to do_action and get a return value?
- Is there any action filter/hook for validating a custom field before publishing the post?
- How many filter/action hooks are healthy?
- AJAX with loop filtering categories
- Earliest hook to reliably get $post/$posts
- Remove an action from an external Class
- Remove Editor From Homepage
- Difference between do_action_ref_array() and do_action()
- What does (10, 2) mean when used with add_filter
- Can my “add_action” function know the name of the hook calling it?
- Why is there both a save_post and wp_insert_post action?
- Does the event ‘wp_version_check’ even exist? What is it doing?
- How to only hook on Single.php after content?
- When is admin_init Action ran?
- How to list some posts first in the loop based on post id
- Can an action callback prevent the parent from continuing execution?
- remove_action in a theme
- Valid characters for actions, hooks and filters
- add_action ‘manage_posts_custom_column’ in a class [closed]
- Advanced Custom Fields and Yoast SEO keyword analysis [closed]
- How do I enqueue a script to run inside the Gutenberg editor?
- How to check if a hook is hooked or not?
- How to change “Draft” string for status of custom post type to “Unavailable”?
- Implementing advanced add_* function wrappers
- How can I find out what functions are assigned to actions?
- Unable to prevent function using save_post firing twice
- Difference between do_action(‘admin_enqueue_scripts’, $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax
- Cron jobs in a class
- Add something to beginning of the content
- Is it possible to use object in add_action?
- Disable WooCommerce action
- How to set custom cookies before output
- How to correctly get post type in a the_title filter
- How can I remove the WP menu from the admin bar?
- How to publish a post with empty title and empty content?
- How do you use the plugin boilerplate loader class to hook actions and filters?
- How to restrict actions and filters “properly” by conditions
- add_filter OO with parameters
- When and Where to use wp_insert_post()
- Get list of registered or enqued styles? looking for filters or actions
- Using add_filter() in Widgets
- How to add a “publish” link to the quick actions
- How do I Make a Theme “plugin-ready”?
- How to order posts tag by tag?
- Better way to enforce category hierarchy in post_categories_metabox?
- Store source permalink on XMLRPC calls
- add_action customize_register not working
- How to make post and comment count unclickable with dashboard_glance_items hook
- Hook into admin post list page
- Passing arguments into ‘init’ function
- Anyway to edit the titlebar of WordPress Widgets in the Admin area?
- Add a Custom Field in Comment Box AFTER text area BUT BEFORE Send button
- How do I know if author field was changed on post save?
- Actions or filters fired when data is saved in a custom table
- How to use conditional add_action for WordPress
- Using wp_insert_post and post_update_meta but need to fire save_post afterward
- WP Schedule Event – Every Day When First Visitor Comes
- enqueuing React script and hooking its target div fails to load script
- Make the ‘all’ filter default instead of ‘mine’ in a custom post type
- wp_logout action not working
- Using hooks to place content in theme dynamically
- Convert hyphen to underscore in permalinks
- Looking for a hook to add attachment information to the media library tab
- What’s the difference between hooks, filters and actions? [duplicate]
- How to remove an action that is added inside a class
- How to use filter hook ‘post_updated_messages’ in coherence with action hook ‘save_post’
- Count singular post views automatically
- About Hooks and Filters
- How to debug removal of rewrite rule flushing?
- Display only text to WordPress loop without loosing the text formatting
- PHP5, Inheritance, Singleton – action & filter hook limitations
- Should I use add_action(‘publish_post or add_filter(‘publish_post?
- Please explain me what the do_action does
- Having an add_action( ‘user_new_form’,) [closed]
- Is it possible to create an action hook using do_action() within add_action()?
- Using auth_redirect returns cannot modify header information
- Apply the_title filter to post titles AND backend auto social-sharing plugin, but not nav menu
- add action which returns modified value
- What would cause the ‘wp’ action to fire twice per page (but only once per post) in Firefox only?