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
loop_start
runs whenever the loop starts (when you callthe_post()
), so it's expected that your function gets triggered in the loop regardless what file it is, even core ones.