1

I have added this code in the function.php file but when I use while loop in any other files this function is getting triggered.

<?php
add_action('loop_start', 'userlogin');
function userlogin()
{
if (is_user_logged_in()) :
$current_user = wp_get_current_user();
?>
    <div class="dropdown na-dropdown">
        <button class="na-btndropdown" id="na-dropdowndashboard" type="button" data-toggle="dropdown">
            <img src="<?php echo esc_url(get_avatar_url($current_user->ID)); ?>" alt="User Image">
            <span><?php echo $current_user->display_name; ?> </span>

            <i class="fa fa-caret-down"></i>
        </button>
        <ul class="dropdown-menu na-dropdownusermenu" aria-labelledby="na-dropdowndashboard">
            <li><a href="<?php echo get_dashboard_url(); ?>">Dashboard</a></li>
            <li><a href="<?php echo get_edit_profile_url(); ?>">Edit Profile</a></li>
            <li><a href="<?php echo wp_logout_url(); ?>">Sign Out</a></li>
        </ul>
    </div>
<?php else : ?>
    <a id="na-btnsignin" class="na-btn" href="<?php echo wp_login_url(); ?>"><span>Login</span></a>
<?php endif;
}
?>

The above function value is getting loaded anywhere where I have used While loop.

<?php
while ($the_query->have_posts()) {
$the_query->the_post();              
 ?>

Sorry for bad english Thank You

4
  • can you edit out the screenshot and instead insert the code as code/text in a code block? Anybody who wants to help has to type out the code by hand to work with it
    – Tom J Nowell
    Commented Mar 4, 2022 at 16:20
  • When I paste code it automatically hides some div and HTML parts
    – Nabeen
    Commented Mar 4, 2022 at 16:47
  • 1
    loop_start runs whenever the loop starts (when you call the_post()), so it's expected that your function gets triggered in the loop regardless what file it is, even core ones.
    – Sally CJ
    Commented Mar 4, 2022 at 17:16
  • 1
    Thank You so much Sally CJ. I have changed loop_start and it worked.
    – Nabeen
    Commented Mar 4, 2022 at 17:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.