Filter in functions.php: How to know which widget I'm in?
-
I’m using both widgets, WPP Views and WPP Comments but want my custom HTML filter to differ based on which widget is being used. For example, when viewing popular posts, I want it to display #views. When displaying most comments, I want to display the # comments. What can I use in the php to determine which widget I’m in so I can choose what to display? I set up each widget to flag the correct thing (views has show views on, comments has show comments on) and then used the following code. But stats_views and stats_comments do not appear to be set by the widget… so, is there another way to do it?
function my_custom_popular_posts_html_list( $mostpopular, $instance ){ $counter = 0; $output = '<ul>'; foreach( $mostpopular as $popular ) { $output .= "<li><div class=\"number\">" . ($counter + 1) . "</div><div class=\"detail\"><a href=\"" . get_the_permalink( $popular->id ) . "\" title=\"" . esc_attr($popular->title) . "\">{$popular->title}</a><div class=\"date\">" . date( 'M j, Y', strtotime($popular->date)); if($stats_views == 1) { if($popular->pageviews == 1) { $viewStr = 'view'; } else { $viewStr = 'views'; } $output .= " | {$popular->pageviews} {$viewStr}"; } if($stats_comments == 1) { if($popular->comments == 1) { $cviewStr = 'view'; } else { $cviewStr = 'views'; } $output .= " | {$popular->comments} {$cviewStr}"; } $output .= "</div></div></li>" . "\n"; $counter++; } $output .= '</ul>'; return $output; } add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );
https://www.ads-software.com/plugins/wordpress-popular-posts/
- The topic ‘Filter in functions.php: How to know which widget I'm in?’ is closed to new replies.