Andrew Spackman
asked on
Display number of posts count in a foreach loop for current vendor.
Hi,
I am trying to find a way I can output a counter that displays the number of unread announcements my vendors have.
The announcements are a custom post type called 'dokan_announcement'
I have the following code that displays the widget of announcements in the dashboard area.
So what I am trying to achieve is a check to see if the current_user or vendor has any unread posts ($notice->status == 'unread') loop through and display the number of these else display nothing.
Many Thanks,
Andrew
I am trying to find a way I can output a counter that displays the number of unread announcements my vendors have.
The announcements are a custom post type called 'dokan_announcement'
I have the following code that displays the widget of announcements in the dashboard area.
<?php
/**
* Dokan Dashboard Template
*
* Dokan Dahsboard Announcement widget template
*
* @since 2.4
*
* @package dokan
*/
?>
<div class="dashboard-widget dokan-announcement-widget">
<div class="widget-title">
<?php _e( 'Latest Notifications', 'dokan' ); ?>
</div>
<?php
?>
<?php if ( $notices ): ?>
<ul class="list-unstyled">
<?php foreach (array_slice($notices, 0, 2) as $notice ): ?>
<?php
$notice_url = trailingslashit( dokan_get_navigation_url( 'single-announcement' ).''.$notice->ID );
?>
<li>
<div class="dokan-dashboard-announce-date <?php echo ( $notice->status == 'unread' ) ? 'dokan-dashboard-announce-unread' : 'dokan-dashboard-announce-read'; ?>">
<span class="announce-day"><?php echo date_i18n( 'd', strtotime( $notice->post_date ) ); ?></span>
<span class="announce-month"><?php echo date_i18n( 'M', strtotime( $notice->post_date ) ); ?></span>
<span class="announce-year"><?php echo date_i18n( 'Y', strtotime( $notice->post_date ) ); ?></span>
</div>
<div class="dokan-dashboard-announce-content">
<a href="<?php echo $notice_url; ?>"><h3><?php echo $notice->post_title; ?></h3></a>
<p><?php echo wp_trim_words( $notice->post_content, 8, '...' ); ?></p>
</div>
<div class="dokan-clearfix"></div>
</li>
<?php endforeach ?>
</ul>
<a class="btn-vendor-primary mb-0" href="<?php echo $announcement_url; ?>"><?php _e( 'See All Notifications', 'dokan' ); ?></a>
<?php else: ?>
<div class="dokan-no-announcement">
<div class="annoument-no-wrapper">
<img class="dokan-announcement-icon" src="<?PHP echo get_template_directory_uri();?>/inc/images/icons/bell-thin.svg" alt="Bell"/>
<p><?php _e( 'No notifications', 'dokan' ) ?></p>
</div>
</div>
<?php endif ?>
</div> <!-- .products -->
So what I am trying to achieve is a check to see if the current_user or vendor has any unread posts ($notice->status == 'unread') loop through and display the number of these else display nothing.
Many Thanks,
Andrew
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Hi,
So I figured it out and here is my final code which I am using to display a count of unread dokan notifications for vendors when they are logged in.
So I figured it out and here is my final code which I am using to display a count of unread dokan notifications for vendors when they are logged in.
<?php
$template_notice = dokan_pro()->notice;
$query = $template_notice->get_announcement_by_users(apply_filters('dokan_announcement_list_number', 3));
$announcement_url = dokan_get_navigation_url( 'announcement/single-announcement' )."$post_id/";
$args = array(
'post_type' => 'dokan_announcement',
'post_status' => 'unread',
'orderby' => 'post_date',
'order' => 'DESC',
'meta_key' => '_announcement_type',
'meta_value' => 'all_seller',
);
$template_notice->add_query_filter();
$all_seller_posts = new \WP_Query($args);
$template_notice->remove_query_filter();
$notices = array_merge($all_seller_posts->posts, $query->posts);
$numUnread = 0;
?>
<?php
global $user_login, $current_user;
if (is_user_logged_in()) {
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);
if (in_array('vendor', $user_info->roles) || in_array('administrator', $user_info->roles)) { ?>
<?php if ($notices): ?>
<?php foreach ($notices as $notice): ?>
<?php if ($notice->status == 'unread') $numUnread++; ?>
<?php
$notice_url = trailingslashit(dokan_get_navigation_url('single-announcement') . '' . $notice->ID);
?>
<?php endforeach ?>
<a href="<?php echo $announcement_url; ?>"><?php echo $numUnread; ?></a>
<?php else: ?>
<div class="dokan-no-announcement">
<div class="annoument-no-wrapper">
<img class="dokan-announcement-icon"
src="<?php echo get_template_directory_uri(); ?>/inc/images/icons/bell-thin.svg" alt="Bell"/>
<p><?php _e('No notifications', 'dokan') ?></p>
</div>
</div>
<?php endif ?>
<?php
}
}
?>
Thanks Michal for your help.
You are welcome.
Regards Michel
Regards Michel
ASKER
Open in new window
When I try this it tells me a statement is expected.Also, I only want to display this if the user has a role type of vendor