View count and comments count in media-gallery.php


#1

Hi there. I’m trying to modify media-gallery.php to add counts for views, likes and comments. I was able to create a like counter by adding this:

<?php 
$mediamodel = new RTMediaModel();  
$likecount = $mediamodel->get( array( 'id' => rtmedia_id() ) );  
if ( isset( $likecount[ 0 ]->likes ) ){  
    $likecount = intval($likecount[ 0 ]->likes);  
} else {   
    $likecount = 0;  
}  
?> 
<i class="rtmicon-thumbs-up rtmicon-fw"></i> <?php echo $likecount; ?>

And tried to create comments or views counter the same way:

    	<?php 
$mediamodel = new RTMediaModel();  
$commentcount = $mediamodel->get( array( 'id' => rtmedia_id() ) );  
if ( isset( $commentcount[ 0 ]->comments ) ){  
    $commentcount = intval($commentcount[ 0 ]->comments);  
} else {   
    $commentcount = 0;  
}  
?> 
<i class="rtmicon-comment rtmicon-fw"></i> <?php echo $commentcount; ?>
	<?php 
$mediamodel = new RTMediaModel();  
$viewscount = $mediamodel->get( array( 'id' => rtmedia_id() ) );  
if ( isset( $viewscount[ 0 ]->views ) ){  
    $viewscount = intval($viewscount[ 0 ]->views);  
} else {   
    $viewscount = 0;  
}  
?> 
<i class="rtmicon-eye rtmicon-fw"></i> <?php echo $viewscount; ?>

But it doesn’t seem to do the trick. And i really ■■■■ at PHP. Can anyone please help me on this one?


#2

Hello @Valery_Ergardt,

Try adding below functions for View and comment count and let us know if this works for you or not.

$commentcount = wp_count_comments( rtmedia_media_id() );
if ( isset( $commentcount->total_comments ) ){  
    $commentcount = intval( $commentcount->total_comments );  
} else {   
    $commentcount = 0;  
} 

$viewscount = get_rtmedia_meta( rtmedia_id(), 'view' );
if ( isset( $viewscount ) ){  
    $viewscount = intval( $viewscount );  
} else {   
    $viewscount = 0;  
}

Thank you.


#3

Yay! It worked! Thank you so much, you’re a life saver! One little flaw, though. Right now a media gallery item looks like this

But when i click “Load more” or move to the next page in pagination mode, it doesn’t display author’s name and counts correctly, like this:

I’m now having following code after “<?php do_action( 'rtmedia_after_item' ); ?>” in media-gallery-item.php:

    <div class="authorlink">
    <span class="authorname"> Автор:</span> <?php rtmedia_author_name(); ?>
    </div>
    <p class="numbersinfo">
    <?php 
    $mediamodel = new RTMediaModel();  
    $likecount = $mediamodel->get( array( 'id' => rtmedia_id() ) );  
    if ( isset( $likecount[ 0 ]->likes ) ){  
        $likecount = intval($likecount[ 0 ]->likes);  
    } else {   
        $likecount = 0;  
    }  
    ?> 
    <i class="rtmicon-thumbs-up rtmicon-fw"></i> <?php echo $likecount; ?>
        <?php 
    $commentcount = wp_count_comments( rtmedia_media_id() );
if ( isset( $commentcount->total_comments ) ){  
    $commentcount = intval( $commentcount->total_comments );  
} else {   
    $commentcount = 0;  
} ?>
    <i class="rtmicon-comment rtmicon-fw"></i> <?php echo $commentcount; ?>
        <?php 
    $viewscount = get_rtmedia_meta( rtmedia_id(), 'view' );
if ( isset( $viewscount ) ){
    $viewscount = intval( $viewscount );
} else { 
    $viewscount = 0;
} ?> 
    <i class="rtmicon-eye rtmicon-fw"></i> <?php echo $viewscount; ?>
    </p>
</li>

#4

Okay, so i figured out i need a filter in functions.php for JSON to display media on pagination correctly. Following this guide http://docs.rtcamp.com/rtmedia/developer/customize-media-gallery-template/ i was able to work out the author name issue. But i still don’t know how to get it to work with like / view /comment counts. This:

function rtmedia_backbone_template_filter_likes( $media_array ) {
$mediamodel = new RTMediaModel();  
$likecount = $mediamodel->get( array( 'id' => rtmedia_id() ) );  
if ( isset( $likecount[ 0 ]->likes ) ){  
    $likecount = intval($likecount[ 0 ]->likes);  
} else {   
    $likecount = 0;  
}  
?>
$media_array->likesc = $likecount;
return $media_array;
}
add_filter( 'rtmedia_media_array_backbone', 'rtmedia_backbone_template_filter_likes', 10, 1 );
function custom_rtmedia_likes() {
    global $rtmedia_backbone;
    if ( $rtmedia_backbone[ 'backbone' ] ){
	echo '<%= likesc %>';
    } else {
	echo 0;
    }
}

Doesn’t seem to work. What am i doing wrong?


#5

Hello @Valery_Ergardt,

Please refer this documentation http://docs.rtcamp.com/rtmedia/developer/customize-media-gallery-template/ it will help you to solve this issue.

Thank you.


#6

Yeah, i did read it and i just can’t get this to work. What i try to do is put this code in functions.php of my theme:

function rtmedia_backbone_template_filter_likes( $media_array ) {
$mediamodel = new RTMediaModel();  
$likecount = $mediamodel->get( array( 'id' => rtmedia_id() ) );  
if ( isset( $likecount[ 0 ]->likes ) ){  
    $likecount = intval($likecount[ 0 ]->likes);  
} else {   
    $likecount = 0;  
}  
$media_array->likesc = $likecount;
return $media_array;
}
add_filter( 'rtmedia_media_array_backbone', 'rtmedia_backbone_template_filter_likes', 10, 1 );
function custom_rtmedia_likes() {
    global $rtmedia_backbone;
    if ( $rtmedia_backbone[ 'backbone' ] ){
	echo '<%= likesc %>';
    } else {
	echo 0;
    }
}

and then echo custom_rtmedia_likes in media-gallery-item. but it doesn’t work :frowning: i know i’m rather bothersome, but if anyone can help me on this one it’ll be so awesome.


#7

Hi @Valery_Ergardt,

You could have only called custom_rtmedia_likes() in media-gallery-item.php template and should have created one more function like custom_get_rtmedia_likes() which will fetch the likes of single media.

So it will become,

function custom_get_rtmedia_likes( $id = false ){
    if( $id === false ){
	$id = rtmedia_media_id();
    }
    // your code to fetch likes
}    

function custom_rtmedia_likes() {
        global $rtmedia_backbone;
        if ( $rtmedia_backbone[ 'backbone' ] ){
    	echo '<%= likesc %>';
        } else {
    	echo custom_get_rtmedia_likes();
        }
    }

And in backbone filter rtmedia_backbone_template_filter_likes, you need to call that custom_get_rtmedia_likes function only.

Check this gist code: https://gist.github.com/Ritesh-patel/f0dbb361e2f2023cd4c0 for reference to get comment count in media gallery.


#8

Omg, you totally rock mate! Thank you so much! You almost literally saved my life! :smiley:


#9

Hello @Valery_Ergardt,

Happy to hear that your issue has been solved.

Thank you.


#10