Avatar of Andrew Spackman
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.

<?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 -->

Open in new window


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
PHPWordPress

Avatar of undefined
Last Comment
Michel Plungjan
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Andrew Spackman
Andrew Spackman

ASKER

Thank you, that seemed to work, can I ask if I can put the else statement within the foreach?

<?php foreach (array_slice($notices, 0, 1)  as $notice ): ?>
    <?php if ($notice->status == 'unread') $numUnread++; ?>

    <?php
    $notice_url =  trailingslashit( dokan_get_navigation_url( 'single-announcement' ).''.$notice->ID );
    ?>

    <a href="<?php echo $announcement_url; ?>"><?php echo $numUnread;?></a>
   <?php else :?>
   // code here
   <?php endif;?>

<?php endforeach ?>

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
Avatar of Andrew Spackman

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.

<?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

    }
}
?>




Open in new window

Thanks Michal for your help.

You are welcome.

Regards Michel
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo