Once a fucntion has been hooked to an action, the function will be executed every time the action is called (with do_action
). To stop this, the action should be removed. See remove_action
:
function do_entry() {
if ( get_the_title() ) {
add_action('loop_entry_before', 'function_that_adds_h2_structure');
} else {
remove_action('loop_entry_before', 'function_that_adds_h2_structure');
}
add_action('loop_entry_entry', 'function_that_adds_content_structure');
}
In your case, I think that the actions hook approach is not very good and it can be better to do direct function calls:
function do_entry() {
if ( get_the_title() ) {
function_that_adds_h2_structure();
}
add_action('loop_entry_entry', 'function_that_adds_content_structure');
}
Related Posts:
- the_content and wp_link_pages
- Count singular post views automatically
- Combining action and filter?
- need help looping add_action in wp
- Query Nopaging action not having effect
- Custom Header added within Dashboard pages, is this possible with custom template files?
- Count singular post views automatically
- An action that runs after each post in the loop on index/archive page?
- Best hook for a function which adds shortcodes to the system?
- Hook in the loop after the first post
- How to get Author ID outside the loop
- remove tags from the_content
- the_title() shows title of the first post instead of the page title?
- Why do themes rely on “The Loop”?
- Check php version before theme activation
- How Do I Load My Action Earlier Enough?
- Hook into admin post list page
- What exactly defines a Main Loop and a Secondary Loop?
- Loop code is displaying pages but not actual posts
- after login that will redirect user role into a page
- Use external link in the add sub menu
- post loop causes wp_users and wp_usermeta DB queries for each users
- Ajax loop refresh on click
- Create a plugin to change the action to which a function is hooked
- WordPress Plugin Boilerplate – add_action hook in static “activate” function
- the_excerpt(), get_the_excerpt() and the_content() all killing “the Loop”
- Is get_posts() more efficient than The Loop?
- loop through all posts memory error
- Can’t unregister parent theme’s CPT from my child theme
- Targeting a Certain Post in Loop (CSS)
- Get post/page data outside the loop
- Don’t delete a page if it holds users
- Display most viewed post from last 30 days is not working correctly
- Split loop to load WordPress gallery into Bootstrap modal and carousel
- Set global $wp_query/$post variable for dynamic page generation
- Set meta field to publish date + 2 weeks
- Trouble Formatting DateTime
- Order archive results by post id in custom taxonomy template
- Comment search plugin
- pre_get_posts for two loops on same page
- Modified loop-single-forum.php to work inside of a sidebar Widget, not working on some site pages
- WooCommerce Hook: Content get’s displayed twice and not AFTER the order total
- Problem with displaying HTML content after in_array [closed]
- Both a page loop and posts loop on the same page
- WordPress Blog has an infinite redirect loop
- Stepping through a foreach loop – adding +1 to the end of it so next time it loops it starts on item 2
- Can’t access PHP array inside script localization from javascript
- ACF – add a group inside a repeater [closed]
- How to create bootstrap grid loop?
- the_post hook is not firing for me
- Is it possible to reverse the order of a list of posts pulled from a loop?
- What is the action hook that deletes a taxonomy term from the backend? And how to retireve the term id before deleting it?
- Get post offset/posts page offset in single post page (outside the loop)
- First article : single column; Other articles : 2 columns
- How can I access specific posts brought back by query_posts?
- Put multiple custom loops on same page
- Contact information footer
- WordPress redirect redirecting too many times or not at all
- Content Visibility for WordPress is not working (Drafts, public etc.)
- Adding JavaScript file in Admin Panel
- Loop is crazy – one row displays wrong count of posts [closed]
- Notify admin when Custom post meta data gets updated or deletet
- Generating a perfect loop
- I need to display all post in onepage
- Action hook with wrapper html
- How can I edit comment meta value before it is saved?
- WordPress loop not working only on home page, works on all other pages
- What hook can I use to modify custom post data before it is displayed on the page?
- How to put a block of code between [shortcode][/shortcode] in do_shortcode()?
- current-user_can not working in loop
- For Each +1 WordPress Loop
- How can I access a post’s feature image outside the loop?
- Is it possible to set archive.php instead of index.php to display blog?
- How to read taxonomy in loop?
- Is there a way to know when a page has been updated and do some action only once?
- style_loader_tag not changing stylesheet to preload
- Undefined index: page in loop load ajax content
- add_action doesn’t work for my function
- Only Show Excerpt After First 3 Posts
- Add HTML code before the title of the Tag page
- How to insert content before pagination in loop?
- Deeply Nested Menu Loop with Twig using Timber [closed]
- Can add_image_size be added earlier
- WordPress redundant posts based on meta field
- WordPress with woocommerce custom query
- Get featured image outside the loop using foreach
- pre_get_posts main_query not displaying after applying tax_query filters
- setup_postdata in “loop-handling” php file called via $.ajax
- Secondary loop cuts off at 10 posts?
- get all page templates
- strange parse error when including a loop template within another template
- Previous Posts Link and Next Posts Link Show All The Same Articles?
- How to add custom checkout field in user details mail template
- Posts Loops To Display Specific Posts Inside Page Template?
- Use callback function in AJAX pure javascript [closed]
- Undefined variable: woocommerce_loop [closed]
- title not being printed within html tag [closed]
- How i can display all posts order by years
- delete_term is not working properly with add_action()
- Add text when displying attribute with a hook on single product page