Hack WordPress “Top 10 Posts”

Petit hack utile

« Top 10 Posts » coded by LaughingLizard http://dinki.mine.nu/word/
Compatible with all versions of WordPress

http://www.mindfulmusings.net/weblog/

1) Open up mysql command line tool, and type in these lines:
(this is considering wpdatabase is the name of your wordpress database)
.

 use wpdatabase;

create table mostAccessed

(

postnumber int not null,

cntaccess int not null,

primary key(postnumber),

unique id(postnumber)

);

Or you could use phpMyAdmin or any other MySql admin tool to create a new table called usersOnline inside your WordPress database with the above columns.

2) If you are using WordPress 1.0 or above AND are using my-hacks.php, copy the functions from below to your my-hacks.php
and place it within the <?php and the ?> tags.
If you are using 0.72 or below, copy these to the end of your b2functions.php which is inside your b2include folder;
again make sure that they are within the <?php and the ?> tags.

function add_count($p_number) {

$result = mysql_query("select postnumber, cntaccess from mostAccessed where postnumber = '$p_number'");

$test = 0;

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

$row[1] += 1;

@mysql_query("update mostAccessed set cntaccess = '$row[1]' where postnumber = '$row[0]'");

$test = 1;

}

if ($test == 0) {

@mysql_query("insert into mostAccessed(postnumber, cntaccess) values('$p_number', '1')");

}

}
 


function show_pop_posts() {

global $wpdb, $siteurl, $tableposts;

$results = $wpdb->get_results("select postnumber, cntaccess from mostAccessed ORDER BY cntaccess DESC LIMIT 10");

foreach ($results as $result) {

$postnumber = $result->postnumber;

$post = @$wpdb->get_row("SELECT ID, post_title  FROM $tableposts WHERE '$postnumber' = ID");

//$post_title = substr($post->post_title, 0, 20);

$post_title = $post->post_title;

//$url = get_permalink($post->ID);

echo "<li><a href=\"$siteurl"."index.php?p=".$post->ID."&more=1&c=1\" title=\"$text\" />$post_title</a>&nbsp;&nbsp;<strong>($result->cntaccess)</strong></li><br/>";

}

}

3) Now open your index.php and find this line:
<?php } } else { // end foreach, end if any posts ?>

And place this line just above that:
<?php if ($p > 0) { add_count($p);}?>

Now place this code where you want your top10 posts to show up (outside the wp-loop):
<?php show_pop_posts(); ?>

All done!

This new functions allows you to show the number of times a certain post is visited.

As above, add this function to my-hacks.php (or to b2functions.php if earlier than 1.0)

function show_post_count($postcountID, $before="(Visited ", $after=" times)") {

global $wpdb, $tableposts;

$resultscount = $wpdb->get_results("select postnumber, cntaccess from mostAccessed WHERE postnumber = $postcountID");

if (isset($resultscount)) {

foreach ($resultscount as $resultcount) {

$postcount = $resultcount->cntaccess;

echo $before.$postcount.$after;

}

}

}

Then find this line to your index.php:
<?php comments_popup_link(‘Comments (0)’, ‘Comments (1)’, ‘Comments (%)’); ?>

And add this line right below it (or anywhere around it):
<?php show_post_count($post->ID, $before= »(Visited « , $after= » times) »); ?>

PS: if you get an error, click on one of your posts to make that the most popular and things will catch on from there.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *