0

I have the following structure:

add_action('my_content', 'standard_loop');
function standard_loop() {
if (have_posts()) :
   while (have_posts()) :
      the_post();
         do_action('loop_entry_before');
         do_action('loop_entry');
         do_action('loop_entry_after');
   endwhile;
endif;
}
add_action('loop_entry_before', 'do_entry', 1);
function do_entry() {
   if ( get_the_title() ) {
      add_action('loop_entry_before', 'function_that_adds_h2_structure');
   }
   add_action('loop_entry_entry', 'function_that_adds_content_structure');
}

The issue is that all post display the function_that_adds_h2_structure even if is only empty html and the post has no title. This is just an example for using login in the do_entry();

Thanks

1 Answer 1

1

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');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.