Display "Post Views Counter" in Wordpress - php

I got this snippet of code to display my post views counter in my wordpress website.
function bac_PostViews($post_ID) {
$count_key = 'post_views_count';
$count = get_post_meta($post_ID, $count_key, true);
if($count == ''){
$count = 0;
delete_post_meta($post_ID, $count_key);
add_post_meta($post_ID, $count_key, '0');
return $count . ' Visitas';
}else{
$count++;
update_post_meta($post_ID, $count_key, $count);
if($count == '1'){
return $count . ' Visita';
}
else {
return $count . ' Visitas';
}
}
}
Then, in my 'single.php' file I added this code for retrieving the count:
$blog_number_visits = bac_PostViews(get_the_ID());
And this code for showing the count:
if ( $blog_author || $blog_date || $show_comments_number ) {
$meta .= '<p class="meta">';
if ( $blog_date ) $meta .= esc_html( date_i18n( $date_format, strtotime( get_the_time( 'Y-m-d' ) ) ) );
if ( $blog_date && $blog_author ) $meta .= ' — ';
if ( $blog_author ) $meta .= '<span class="visitas">' . $blog_number_visits . ' </span>';
if ( ( $blog_date || $blog_author ) && $show_comments_number ) $meta .= ' — ';
if ( $show_comments_number ) $meta .= '<span class="commentCount">' . $comments_number . '</span>';
$meta .= '</p>';
}
My problem is that I can't display it in the home page. The HOME is getting the code from functions.php and if I put my snippet of code in functions.php and try to display it from there... It always shows 0 VIEWS.
I think maybe the problem is that functions.php is not inside 'the LOOP' and thats why is not displaying in the home.
My website: Website home where I want to display VIEWS COUNTER

Related

Breadcrumb Incrementing Number for meta position content

I'm new to PHP, still learning, so please excuse and kindly tell me what would be the appropriate fix. I used the breadcrumb script from here answered by Pieter Goosen. I want the breadcrumbs to work with google and schema, the part that isn't working is the <meta itemprop="position" content="number" /> tag. The content="number" needs to be set for each crumb, in order (1,2,3...). On line 8 I added $i = 1;, then changed $link_after = '<meta itemprop="position" content="' . $i++ . '" /></li>';. But $i++ is not printing a new sequential number, it is just repeating itself as if there was no other $i++. My guess is to apply the rule globally and to search for $i++ used, as it's probably being applied closed within the rule, but wouldn't know how to do this.
Code:
function get_hansel_and_gretel_breadcrumbs()
{
// Set variables for later use
$here_text = __( 'You are currently here!' );
$home_link = home_url('/');
$home_text = __( 'Home' );
$link_before = '<span typeof="v:Breadcrumb">';
$i = 1;
$link_after = '<meta itemprop="position" content="' . $i++ . '" /></span>'
$link_attr = ' rel="v:url" property="v:title"';
$link = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
$delimiter = ' » '; // Delimiter between crumbs
$before = '<span class="current">'; // Tag before the current crumb
$after = '</span>'; // Tag after the current crumb
$page_addon = ''; // Adds the page number if the query is paged
$breadcrumb_trail = '';
$category_links = '';
/**
* Set our own $wp_the_query variable. Do not use the global variable version due to
* reliability
*/
$wp_the_query = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();
// Handle single post requests which includes single pages, posts and attatchments
if ( is_singular() )
{
/**
* Set our own $post variable. Do not use the global variable version due to
* reliability. We will set $post_object variable to $GLOBALS['wp_the_query']
*/
$post_object = sanitize_post( $queried_object );
// Set variables
$title = apply_filters( 'the_title', $post_object->post_title );
$parent = $post_object->post_parent;
$post_type = $post_object->post_type;
$post_id = $post_object->ID;
$post_link = $before . $title . $after;
$parent_string = '';
$post_type_link = '';
if ( 'post' === $post_type )
{
// Get the post categories
$categories = get_the_category( $post_id );
if ( $categories ) {
// Lets grab the first category
$category = $categories[0];
$category_links = get_category_parents( $category, true, $delimiter );
$category_links = str_replace( '<a', $link_before . '<a' . $link_attr, $category_links );
$category_links = str_replace( '</a>', '</a>' . $link_after, $category_links );
}
}
if ( !in_array( $post_type, ['post', 'page', 'attachment'] ) )
{
$post_type_object = get_post_type_object( $post_type );
$archive_link = esc_url( get_post_type_archive_link( $post_type ) );
$post_type_link = sprintf( $link, $archive_link, $post_type_object->labels->singular_name );
}
// Get post parents if $parent !== 0
if ( 0 !== $parent )
{
$parent_links = [];
while ( $parent ) {
$post_parent = get_post( $parent );
$parent_links[] = sprintf( $link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) );
$parent = $post_parent->post_parent;
}
$parent_links = array_reverse( $parent_links );
$parent_string = implode( $delimiter, $parent_links );
}
// Lets build the breadcrumb trail
if ( $parent_string ) {
$breadcrumb_trail = $parent_string . $delimiter . $post_link;
} else {
$breadcrumb_trail = $post_link;
}
if ( $post_type_link )
$breadcrumb_trail = $post_type_link . $delimiter . $breadcrumb_trail;
if ( $category_links )
$breadcrumb_trail = $category_links . $breadcrumb_trail;
}
// Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives
if( is_archive() )
{
if ( is_category()
|| is_tag()
|| is_tax()
) {
// Set the variables for this section
$term_object = get_term( $queried_object );
$taxonomy = $term_object->taxonomy;
$term_id = $term_object->term_id;
$term_name = $term_object->name;
$term_parent = $term_object->parent;
$taxonomy_object = get_taxonomy( $taxonomy );
$current_term_link = $before->labels->singular_name . '<li class="curr-cat" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . $term_name . '</span><meta itemprop="position" content="' . $i++ . '" /></li>';
$parent_term_string = '';
if ( 0 !== $term_parent )
{
// Get all the current term ancestors
$parent_term_links = [];
while ( $term_parent ) {
$term = get_term( $term_parent, $taxonomy );
$parent_term_links[] = sprintf( $link, esc_url( get_term_link( $term ) ), $term->name );
$term_parent = $term->parent;
}
$parent_term_links = array_reverse( $parent_term_links );
$parent_term_string = implode( $delimiter, $parent_term_links );
}
if ( $parent_term_string ) {
$breadcrumb_trail = $parent_term_string . $delimiter . $current_term_link;
} else {
$breadcrumb_trail = $current_term_link;
}
} elseif ( is_author() ) {
$breadcrumb_trail = __( 'Author archive for ') . $before . $queried_object->data->display_name . $after;
} elseif ( is_date() ) {
// Set default variables
$year = $wp_the_query->query_vars['year'];
$monthnum = $wp_the_query->query_vars['monthnum'];
$day = $wp_the_query->query_vars['day'];
// Get the month name if $monthnum has a value
if ( $monthnum ) {
$date_time = DateTime::createFromFormat( '!m', $monthnum );
$month_name = $date_time->format( 'F' );
}
if ( is_year() ) {
$breadcrumb_trail = $before . $year . $after;
} elseif( is_month() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$breadcrumb_trail = $year_link . $delimiter . $before . $month_name . $after;
} elseif( is_day() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$month_link = sprintf( $link, esc_url( get_month_link( $year, $monthnum ) ), $month_name );
$breadcrumb_trail = $year_link . $delimiter . $month_link . $delimiter . $before . $day . $after;
}
} elseif ( is_post_type_archive() ) {
$post_type = $wp_the_query->query_vars['post_type'];
$post_type_object = get_post_type_object( $post_type );
$breadcrumb_trail = $before . $post_type_object->labels->singular_name . $after;
}
}
// Handle the search page
if ( is_search() ) {
$breadcrumb_trail = __( 'Search query for: ' ) . $before . get_search_query() . $after;
}
// Handle 404's
if ( is_404() ) {
$breadcrumb_trail = $before . __( 'Error 404' ) . $after;
}
// Handle paged pages
if ( is_paged() ) {
$current_page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
$page_addon = $before . sprintf( __( ' ( Page %s )' ), number_format_i18n( $current_page ) ) . $after;
}
$breadcrumb_output_link = '';
$breadcrumb_output_link .= '<div class="breadcrumb">';
if ( is_home()
|| is_front_page()
) {
// Do not show breadcrumbs on page one of home and frontpage
if ( is_paged() ) {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $page_addon;
}
} else {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $delimiter;
$breadcrumb_output_link .= $breadcrumb_trail;
$breadcrumb_output_link .= $page_addon;
}
$breadcrumb_output_link .= '</div><!-- .breadcrumbs -->';
return $breadcrumb_output_link;
}

PHP - Is it possible to add a string to a variable?

I would like to make a small change in a code for a table of contents.
I want to add a sign in front of each heading. The character should be recognized as text.
I've tried a few things, but unfortunately I have not found the right variable.
The code comes from a plugin for Wordpress
I have already tried the following variables:
$items
$tic
$find
$replace
$post
Here is the code that prints the list:
if ( $tic->is_eligible($custom_toc_position) ) {
extract( $args );
$items = $tic->extract_headings( $find, $replace,wptexturize($post->post_content) );
$title = ( array_key_exists('title', $instance) ) ? apply_filters('widget_title', $instance['title']) : '';
if ( strpos($title, '%PAGE_TITLE%') !== false ) $title = str_replace( '%PAGE_TITLE%', get_the_title(), $title );
if ( strpos($title, '%PAGE_NAME%') !== false ) $title = str_replace( '%PAGE_NAME%', get_the_title(), $title );
$hide_inline = $toc_options['show_toc_in_widget_only'];
$css_classes = '';
// bullets?
if ( $toc_options['bullet_spacing'] )
$css_classes .= ' have_bullets';
else
$css_classes .= ' no_bullets';
if ( $items ) {
// before widget (defined by themes)
echo $before_widget;
// display the widget title if one was input (before and after titles defined by themes)
if ( $title ) echo $before_title . $title . $after_title;
// display the list
echo '<ul class="toc_widget_list' . $css_classes . '">' . $items . '</ul>';
// after widget (defined by themes)
echo $after_widget;
}
This are the full code of function extract_headings:
public function extract_headings( &$find, &$replace, $content = '' )
{
$matches = array();
$anchor = '';
$items = false;
// reset the internal collision collection as the_content may have been triggered elsewhere
// eg by themes or other plugins that need to read in content such as metadata fields in
// the head html tag, or to provide descriptions to twitter/facebook
$this->collision_collector = array();
if ( is_array($find) && is_array($replace) && $content ) {
// get all headings
// the html spec allows for a maximum of 6 heading depths
if ( preg_match_all('/(<h([1-6]{1})[^>]*>).*<\/h\2>/msuU', $content, $matches, PREG_SET_ORDER) ) {
// remove undesired headings (if any) as defined by heading_levels
if ( count($this->options['heading_levels']) != 6 ) {
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
if ( in_array($matches[$i][2], $this->options['heading_levels']) )
$new_matches[] = $matches[$i];
}
$matches = $new_matches;
}
// remove specific headings if provided via the 'exclude' property
if ( $this->options['exclude'] ) {
$excluded_headings = explode('|', $this->options['exclude']);
if ( count($excluded_headings) > 0 ) {
for ($j = 0; $j < count($excluded_headings); $j++) {
// escape some regular expression characters
// others: http://www.php.net/manual/en/regexp.reference.meta.php
$excluded_headings[$j] = str_replace(
array('*'),
array('.*'),
trim($excluded_headings[$j])
);
}
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
$found = false;
for ($j = 0; $j < count($excluded_headings); $j++) {
if ( #preg_match('/^' . $excluded_headings[$j] . '$/imU', strip_tags($matches[$i][0])) ) {
$found = true;
break;
}
}
if (!$found) $new_matches[] = $matches[$i];
}
if ( count($matches) != count($new_matches) )
$matches = $new_matches;
}
}
// remove empty headings
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
if ( trim( strip_tags($matches[$i][0]) ) != false )
$new_matches[] = $matches[$i];
}
if ( count($matches) != count($new_matches) )
$matches = $new_matches;
// check minimum number of headings
if ( count($matches) >= $this->options['start'] ) {
for ($i = 0; $i < count($matches); $i++) {
// get anchor and add to find and replace arrays
$anchor = $this->url_anchor_target( $matches[$i][0] );
$find[] = $matches[$i][0];
$replace[] = str_replace(
array(
$matches[$i][1], // start of heading
'</h' . $matches[$i][2] . '>' // end of heading
),
array(
$matches[$i][1] . '<span id="' . $anchor . '">',
'</span></h' . $matches[$i][2] . '>'
),
$matches[$i][0]
);
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
}
// build a hierarchical toc?
// we could have tested for $items but that var can be quite large in some cases
if ( $this->options['show_heirarchy'] ) $items = $this->build_hierarchy( $matches );
}
}
}
return $items;
}
I tried it like this:
$items = '>'.$items
$tic = '>'.$tic
$find = '>'.$find
.
.
.
Unfortunately, nothing has hit the right place
$ items addressed only the entire list
The other Variables had no effect or led to errors
The extract_headings is creating the list items. This section of the function...
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
Should look like this:
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">>';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
You can see I've added an extra > inside the hyperlink on the third line. If adding an extra > causes any issues, you can also use >.

Need Help Modifying a WP Page/Post Slider Plugin to go to Unique Page at End

I'm using the Theia Post Slider and I was hoping someone could look at this part of the plugin's code to tell me which part I can modify to get it to go to a unique url at the end.
I tried entering it in place of the $url in this part:
$button = '' . $html . '';
But it didn't work. It looks like it should.
I'd like to just remove the part about checking if there's another post and just have it go straight to the URL but I'm not sure how to do it (beyond my php skill set).
Here's the rest of that section of code the part I modified was near the bottom:
/*
* Get a button for a navigation bar.
* #param direction boolean False = prev; True = next;
*/
public static function get_navigation_barButton( $options, $direction ) {
global $page, $pages;
$directionName = $direction ? 'next' : 'prev';
$url = TpsMisc::get_post_page_url( $options['currentSlide'] + ( $direction ? 1 : - 1 ) );
$leftText = null;
$rightText = null;
// Check if there isn't another page but there is another post.
$urlIsAnotherPost = ! $url && $options[ $directionName . 'PostUrl' ];
if ( $urlIsAnotherPost ) {
$url = $options[ $directionName . 'PostUrl' ];
}
// Check what text we should display on the buttons.
if ( TpsOptions::get( 'post_navigation' ) && ( ( $direction == false && $page == 1 ) || ( $direction == true && $page == count( $pages ) ) ) ) {
$buttonText = TpsOptions::get( $directionName . '_text_post' );
} else {
$buttonText = TpsOptions::get( $directionName . '_text' );
}
switch ( TpsOptions::get( 'theme_type' ) ) {
case 'font':
$text = $directionName == 'next' ? TpsOptions::get_font_icon( 'right' ) : TpsOptions::get_font_icon( 'left' );
$width = 0;
if ( $directionName == 'next' ) {
$leftText = $buttonText;
} else {
$rightText = $buttonText;
}
break;
case 'classic':
$text = $buttonText;
$leftText = '';
$rightText = '';
if ( $urlIsAnotherPost ) {
$width = TpsOptions::get( 'button_width_post' );
} else {
$width = TpsOptions::get( 'button_width' );
}
break;
default:
return '';
}
$style = $width == 0 ? '' : 'style="width: ' . $width . 'px"';
$htmlPart1 = '<span class="_1">' . $leftText . '</span><span class="_2" ' . $style . '>';
$htmlPart2 = '</span><span class="_3">' . $rightText . '</span>';
// HTML
$html = $htmlPart1 . $text . $htmlPart2;
$class = $urlIsAnotherPost ? ' _another_post' : '';
if ( $url ) {
$button = '' . $html . '';
} else {
$button = '<span class="_button _' . $directionName . $class . ' _disabled">' . $html . '</span>';
}
return $button;
}
In case someone wants to do this it turned out to be in the tps.js file line 717. Below where it says URL GOES HERE:
// Set link.
if (
(direction == false && me.options.prevPost && me.currentSlide == 0) ||
(direction == true && me.options.nextPost && me.currentSlide == me.numberOfSlides - 1)
) {
buttonEl.addClass('_another_post');
href = 'URL GOES HERE';
} else {
buttonEl.removeClass('_another_post');
if (me.needToRefreshPage()) {
var slide = me.slides[me.currentSlide + (direction ? 1 : -1)];
if (slide) {
href = slide.permalink;
}
}
else {
href = '#';
}
}

Modify code to pass a post id as a parameter in order to create a breadcrumb via AJAX in Wordpress

I have a function I use to create a breadcrumb on a wordpress site:
function the_breadcrumb() {
$delimiter = '>';
$currentBefore = '<li><a>';
$currentAfter = '</a></li>';
if ( !is_home() && !is_front_page() || is_paged() ) {
echo '<nav class="breadcrumb"><ul>';
global $post;
if ( is_page() && !$post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter; }
elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<li>' . get_the_title($page->ID) . '</li>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb;
echo $currentBefore;
the_title();
echo $currentAfter;
}
echo '</ul></nav>';
}
}
But I would like this function to take a post_id (id of a page) as parameter in order to use it in a AJAX function that create the breadcrumb for that page.
function ajaxify() {
$post_id = $_POST['post_id'];
$breadcrumb = the_breadcrumb($post_id);
print_r($breadcrumb);
die(); // remove trailing 0
}
How can I achieve that?
Many thanks for your help.
You need to pass post_id as an argument and retrieve the post information with wp_query like this :
function the_breadcrumb($post_id){
$delimiter = '>';
$currentBefore = '<li><a>';
$currentAfter = '</a></li>';
// here your query
$args = array(
'page_id' => $post_id, // id of a page, post, or custom type
'post_type' => 'any');
// here is the informations
$myPost = new WP_Query($args);
if ( !is_home() && !is_front_page() || is_paged() ) {
echo '<nav class="breadcrumb"><ul>';
if ( is_page() && !$myPost->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter; }
elseif ( is_page() && $myPost->post_parent ) {
$parent_id = $myPost->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<li>' . get_the_title($page->ID) . '</li>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb;
echo $currentBefore;
the_title();
echo $currentAfter;
}
echo '</ul></nav>';
}
This code works with asynchron content for your posts: (to copy in functions.php)
function ariane() {
$cat_id = get_the_category()[0]->term_id;
$breadcrumb = '<li>' . get_the_title() . '</li>';
$if_parent = TRUE;
while($if_parent == TRUE) :
$cat_object = get_category($cat_id);
$cat = $cat_object->term_id;
$categoryURL = get_category_link($cat);
$name = $cat_object->name;
$cat_id = $cat_object->parent;
$add_link = '<li>' . $name . '</li>';
$breadcrumb = substr_replace($breadcrumb, $add_link, 0, 0);
if($cat_id == 0) :
$if_parent = FALSE;
endif;
endwhile;
echo '<ul>' . $breadcrumb . '</ul>';
}
In your archive.php or in the loop WP_QUERY
<div class="ariane"><?php ariane(); ?></div>
in css :
.ariane ul{
display: flex;
justify-content: flex-start;
align-items: center;
}
.ariane li:not(:last-child):after {
content: '>';
margin: 0 5px;
}

how to show content in popular post in wordpress?

I am using top 10 -popular post plugin.
I added the following lines but it wont display...
$output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';
code:
function tptn_pop_posts( $args ) {
global $wpdb, $siteurl, $tableposts, $id, $tptn_settings;
$defaults = array(
'is_widget' => FALSE,
'daily' => FALSE,
'echo' => FALSE,
'strict_limit' => FALSE,
'posts_only' => FALSE,
'is_shortcode' => FALSE,
'heading' => 1,
);
$defaults = array_merge( $defaults, $tptn_settings );
// Parse incomming $args into an array and merge it with $defaults
$args = wp_parse_args( $args, $defaults );
// OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
extract( $args, EXTR_SKIP );
if ($daily) {
$table_name = $wpdb->prefix . "top_ten_daily";
} else {
$table_name = $wpdb->prefix . "top_ten";
}
$limit = ( $strict_limit ) ? $limit : ( $limit * 5 );
$exclude_categories = explode( ',', $exclude_categories );
$target_attribute = ( $link_new_window ) ? ' target="_blank" ' : ' '; // Set Target attribute
$rel_attribute = ( $link_nofollow ) ? ' nofollow' : ''; // Set nofollow attribute
parse_str( $post_types, $post_types ); // Save post types in $post_types variable
if ( ! $daily ) {
$args = array();
$sql = "SELECT postnumber, cntaccess as sumCount, ID, post_type, post_status ";
$sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
$sql .= "AND post_status = 'publish' ";
if ( '' != $exclude_post_ids ) {
$sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
}
$sql .= "AND ( ";
$multiple = false;
foreach ( $post_types as $post_type ) {
if ( $multiple ) $sql .= ' OR ';
$sql .= " post_type = '%s' ";
$multiple = true;
$args[] = $post_type; // Add the post types to the $args array
}
$sql .= " ) ";
$sql .= "ORDER BY sumCount DESC LIMIT %d";
$args[] = $limit;
} else {
$current_time = current_time( 'timestamp', 0 );
$current_time = $current_time - ( $daily_range - 1 ) * 3600 * 24;
$current_date = date( 'Y-m-j', $current_time );
$args = array(
$current_date,
);
$sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status ";
$sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
$sql .= "AND post_status = 'publish' AND dp_date >= '%s' ";
if ( '' != $exclude_post_ids ) {
$sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
}
$sql .= "AND ( ";
$multiple = false;
foreach ( $post_types as $post_type ) {
if ( $multiple ) $sql .= ' OR ';
$sql .= " post_type = '%s' ";
$multiple = true;
$args[] = $post_type; // Add the post types to the $args array
}
$sql .= " ) ";
$sql .= "GROUP BY postnumber ";
$sql .= "ORDER BY sumCount DESC LIMIT %d";
$args[] = $limit;
}
if ( $posts_only ) { // Return the array of posts only if the variable is set
return apply_filters( 'tptn_pop_posts_array', $wpdb->get_results( $wpdb->prepare( $sql , $args ) , ARRAY_A ) );
}
$results = $wpdb->get_results( $wpdb->prepare( $sql , $args ) );
$counter = 0;
$output = '';
$shortcode_class = $is_shortcode ? ' tptn_posts_shortcode' : '';
$widget_class = $is_widget ? ' tptn_posts_widget' : '';
if ( $heading ) {
if ( ! $daily ) {
$output .= '<div id="tptn_related" class="tptn_posts ' . $widget_class . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title );
} else {
$output .= '<div id="tptn_related_daily" class="tptn_posts_daily' . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title_daily );
}
} else {
if ( ! $daily ) {
$output .= '<div class="tptn_posts' . $widget_class . $shortcode_class . '">';
} else {
$output .= '<div class="tptn_posts_daily' . $widget_class . $shortcode_class . '">';
}
}
if ( $results ) {
$output .= apply_filters( 'tptn_before_list', $before_list );
foreach ( $results as $result ) {
$sumcount = $result->sumCount;
$result = get_post( apply_filters( 'tptn_post_id', $result->ID ) ); // Let's get the Post using the ID
$categorys = get_the_category( apply_filters( 'tptn_post_cat_id', $result->ID ) ); //Fetch categories of the plugin
$p_in_c = false; // Variable to check if post exists in a particular category
foreach ( $categorys as $cat ) { // Loop to check if post exists in excluded category
$p_in_c = ( in_array( $cat->cat_ID, $exclude_categories ) ) ? true : false;
if ( $p_in_c ) break; // End loop if post found in category
}
$title = tptn_max_formatted_content( get_the_title( $result->ID ), $title_length );
if ( ! $p_in_c ) {
$output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );
if ( 'after' == $post_thumb_op ) {
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title if post thumbnail is to be displayed after
$output .= '</a>'; // Close the link
}
if ( 'inline' == $post_thumb_op || 'after' == $post_thumb_op || 'thumbs_only' == $post_thumb_op ) {
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= tptn_get_the_post_thumbnail( array(
'postid' => $result->ID,
'thumb_height' => $thumb_height,
'thumb_width' => $thumb_width,
'thumb_meta' => $thumb_meta,
'thumb_html' => $thumb_html,
'thumb_default' => $thumb_default,
'thumb_default_show' => $thumb_default_show,
'thumb_timthumb' => $thumb_timthumb,
'thumb_timthumb_q' => $thumb_timthumb_q,
'scan_images' => $scan_images,
'class' => "tptn_thumb",
'filter' => "tptn_postimage",
) );
$output .= '</a>'; // Close the link
}
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '<span class="tptn_after_thumb">';
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
// $output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';
$output .= '</a>'; // Close the link
}
if ( $show_author ) {
$author_info = get_userdata( $result->post_author );
$author_name = ucwords( trim( stripslashes( $author_info->display_name ) ) );
$author_link = get_author_posts_url( $author_info->ID );
$output .= '<span class="tptn_author"> ' . __( ' by ', TPTN_LOCAL_NAME ).'' . $author_name . '</span> ';
}
if ( $show_date ) {
$output .= '<span class="tptn_date"> ' . mysql2date( get_option( 'date_format', 'd/m/y' ), $result->post_date ).'</span> ';
}
if ( $show_excerpt ) {
$output .= '<span class="tptn_excerpt"> ' . tptn_excerpt( $result->ID, $excerpt_length ).'</span>';
}
if ( $disp_list_count ) $output .= ' <span class="tptn_list_count">(' . number_format_i18n( $sumcount ) . ')</span>';
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '</span>';
}
$output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
$counter++;
}
if ( $counter == $limit/5 ) break; // End loop when related posts limit is reached
}
if ( $show_credit ) {
$output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );
$output .= sprintf( 'Popular posts by Top 10 plugin', TPTN_LOCAL_NAME );
$output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
}
$output .= apply_filters( 'tptn_after_list', $after_list );
} else {
$output .= ( $blank_output ) ? '' : $blank_output_text;
}
$output .= '</div>';
return apply_filters( 'tptn_pop_posts', $output );
}
You seem to be returning $output. You will need to use echo in order for your HTML to display.
Ref: http://php.net/manual/en/function.echo.php
to show content in popular post by adding the following line below the code.
$output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';
top10.php (top10 popular post plugin)
code:
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '<span class="tptn_after_thumb">';
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
$output .= '</a>'; // Close the link
$output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';
}

Categories