WordPress Plugins > WP-PostViews

Custom Total Views

(1/1)

dubsnipe:
Hello,

I've been lurkin around and found that the PostViews has already built some custom functions to show totals and most views, but I want to try something different for my site (I'm learning my PHPs so I'm just being curious). I run a blog and I created a custom template to show the last 100 posts along with their view count:


--- Code: ---<?php query_posts("showposts=100"); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="box">
<a href="<?php the_permalink() ?>" class="title"><?php the_title(); ?></a>
<br/><span class="author">By <?php the_author_posts_link('namefl'); ?></span>
<?php if(function_exists('the_views')) { the_views(); } ?>
</div>
--- End code ---

Everything runs nicely. Now, I wanted to try using a summatory of the "the_views()" function, since I figured that this approach would help me get, for example, the total view count in a given time or a category, or whatever I want as long as it's powered by Wordpress' loop. Who knows if it's possible, I just wanted to give it a try, and ended up with something like this:

--- Code: --- <?php query_posts("showposts=100"); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="box">
<a href="<?php the_permalink() ?>" class="title"><?php the_title(); ?></a>
<?php $var = if(function_exists('the_views')) { $var = the_views(); } ?>
<?php $total += $var; ?>
</div>
  <?php endwhile; ?>
<?php echo $total; ?>

--- End code ---

I'm having a bit of trouble with the $var = the_views(); part, since it didn't work. Is there any way to output the_views() into a variable? I know it's a PHP problem, but I thought that the solution might benefit someone who might be looking for a power use of this plugin. I hope you can help me out, thank you!

aSKer:
I didn't try it, but I suppose it would be something like that:


--- Code: --- <?php $total = 0; ?><!-- In case you have several loops in the same page -->
<?php query_posts("showposts=100"); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="box">
<a href="<?php the_permalink() ?>" class="title"><?php the_title(); ?></a>
<?php if(function_exists('the_views')) { $var = the_views(); } else { $var = 0; } ?><!-- You had an error here -->
<?php $total = $total+$var; ?>
</div>
  <?php endwhile; ?>
<?php echo $total; ?>

--- End code ---

dubsnipe:
That's what I thought as well, but it returns a 0 since it's the value at the beginning. It's as if the return from the_views cannot be used within operation even if it's a number, or saved into a variable.

I think the problem lies here:


--- Code: ---$var = the_views();
--- End code ---

How can I output the result into a variable? Perhaps there's a little something missing. I have faith that someone will come with the right answer. :)

GaMerZ:

--- Code: ---$views = get_post_meta(get_the_id(), 'views', true);

--- End code ---

Navigation

[0] Message Index

Go to full version