Oh My God! I knew I should have learned php before getting into whole “let’s make a bunch of kinda easy upgrades on our site” =))
That’s OK, we all have to start somewhere to learn stuff 🙂 All credits to you for your effort!
One more little question (though, you know, the hardest ones are always hiding behind that statement =)):
now there is a {$popular->pageviews} where are {$popular->stats} was before, so I couldn’t get pageviews and comments in 2 different widgets if I use filter from the above?
If I remember correctly, to display the comments count you need to add {$popular->comment_count}.
To make this exercise even better, let’s say that you also want the ability to decide whether to display comments / views or not. You want one widget displaying both views and comment count, and another one just the views count. Take a look at the code now:
/// get custom taxonomy using the post ID outside the loop.
function get_year_by_id($post_id){
$out = array();
$taxonomy_slug = 'god';
$terms = get_the_terms( $post_id, $taxonomy_slug );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$out[] =
' <a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a>";
}
}
return implode( '', $out );
}
/// add 'year' to WPP widget
function my_custom_popular_posts_html_list( $mostpopular, $instance ){
$output = '<ul class="wpp-list">';
// loop the array of popular posts objects
foreach( $mostpopular as $popular ) {
$stats = ''; // placeholder for the stats tag
// either the Pageviews or Comments count is activated on the widget, so let's add the stats HTML tag
if ( $instance['stats_tag']['comment_count'] || $instance['stats_tag']['views'] ) {
$stats = "<span class=\"wpp-stats\">";
// Comment count option active, display comments
if ( $instance['stats_tag']['comment_count'] ) {
// display text in singular or plural, according to comments count
$stats .= sprintf(
_n('1 comment', '%s comments', $popular->comment_count, 'wordpress-popular-posts'),
number_format_i18n($popular->comment_count)
);
}
// Pageviews option active, display views
if ( $instance['stats_tag']['views'] ) {
// if comments count is also active, let's add a separator (character)
if ( $instance['stats_tag']['comment_count'] ) {
$stats .= " | ";
}
// If sorting posts by average views
if ($instance['order_by'] == 'avg') {
// display text in singular or plural, according to views count
$stats .= sprintf(
_n('1 view per day', '%s views per day', intval($popular->pageviews), 'wordpress-popular-posts'),
number_format_i18n($popular->pageviews, 2)
);
} else { // Sorting posts by views
// display text in singular or plural, according to views count
$stats .= sprintf(
_n('1 view', '%s views', intval($popular->pageviews), 'wordpress-popular-posts'),
number_format_i18n($popular->pageviews)
);
}
}
$stats .= "</span>";
}
$post_tax = get_year_by_id( $popular->id );
$output .= "<li><a href=\"" . get_the_permalink( $popular->id ) . "\" title=\"" . esc_attr( $popular->title ) . "\"><br>{$popular->title}</a> {$stats}" . $post_tax . "</li>" . "\n";
}
$output .= '</ul>';
return $output;
}
add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );
The $instance variable in function my_custom_popular_posts_html_list( $mostpopular, $instance ) contains the configuration info from the widget. This way, you can have your custom HTML and have each widget behave differently according to its settings.
Amusing, huh? 😛