mercredi 20 janvier 2021

How to add an if statement to a php function that returns error when empty

This is probably a stupid question but I'm quite new to php so any help would be appreciated. I'm using this code to show members in my wordpress based website the latest posts they've visited. It works fine except when they haven't visited any posts, then it returns an error. (Warning: array_unique() expects parameter 1 to be array, string given in and Invalid argument supplied for foreach() in ) I know I need to add a if statement to echo "You haven't seen any posts yet" but I'm lost at where to add it. I hope someone can help me out and thanks very much in advance!

/*
 * Plugin Name: WPSE_63266_Recently_Viewed
 */
function wpse_63266_update_recently_viewed(){

    /**
     *  If is admin or isn't single, then return.
     *  To get only singular video posts use; if(!is_singular('videos')) return;
     */
    if(is_admin() || !is_single()) return;

    global $post;

    // Get the current post id.
    $current_post_id = get_the_ID();

    if(is_user_logged_in()){

        // Store recently viewed post ids in user meta.
        $recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
        if( '' == $recenty_viewed ){
            $recenty_viewed = array();            
        }

        // Prepend id to the beginning of recently viewed id array.(http://php.net/manual/en/function.array-unshift.php)
        array_unshift($recenty_viewed, $current_post_id);        

        // Keep the recently viewed items at 5. (http://www.php.net/manual/en/function.array-slice.php)
        $recenty_viewed = array_slice($recenty_viewed, 0, 10); // Extract a slice of the array

        // Update the user meta with new value.
        update_user_meta(get_current_user_id(), 'recently_viewed', $recenty_viewed);

    } else {

    /**
     * For non-logged in users you can use the same procedure as above
     * using get_option() and update_option()
     */

    }
}
add_action('wp_footer', 'wpse_63266_update_recently_viewed');

function wpse_63266_show_recently_viewed(){
    $recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
$evets=array_unique($recenty_viewed);
    $hadi = get_the_terms( $evets, 'video-genres' ); 
        $hadi2 = get_the_terms( $evets, 'video-type' ); 
            $hadi3 = get_the_terms( $evets, 'video-category' ); 
    
    
    
    foreach($evets as $evet) {
 echo '<div class="soncular"><div class="soncol1"><a href="'.get_permalink($evet).'"><img src="'.get_the_post_thumbnail_url($evet, 'progression-studios-video-index').'"</a></div>'; 
echo '<div class="soncol2"><b><strong><a href="'.get_permalink($evet).'">'.get_the_title($evet).'</a></strong></b>';
echo '<br><span>'.$hadi->name.'</span>';
    echo '<br><span>'.$hadi3->name.'</span>';
    echo '<br><span>'.$hadi2->name.'</span></div></div>';
}
    

}
add_action('wpse_63266_recently_viewed', 'wpse_63266_show_recently_viewed');

Aucun commentaire:

Enregistrer un commentaire