B2Evolution: Top Articles Hack

Here is a hack to make B2evolution show the most viewed articles. As this is scanning the hitlog on every page display, it could be more efficient. For me, it is fast enough now, and by the time I have enough hits to be slow, b2evo should have some sort of page caching.

UPDATE: I also have a b2evolution 1.6 version of this hack

To install:
Copy and paste the following code into the sidebar of your skin. For example I put mine into skins/custom/_main.php right after the Last Comments link.

        <div class="bSideItem">
                <h3 class="sideItemTitle">Top Articles</h3>

$TOPART_rc = $DB->get_results("SELECT visitURL, count(*) as count
FROM evo_hitlog
WHERE hit_blog_ID = $blog
AND hit_ignore NOT
IN ( 'invalid', 'rss', 'robot' )
AND visitURL NOT REGEXP '/[0-9]*$'

$TOPART_counts = Array();
$TOPART_parts = Array();
foreach ($TOPART_rc as $TOPART_idx => $TOPART_row) {
        $TOPART_url = $TOPART_row['visitURL'];
        // Full link with date
        if (ereg('.php/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-zA-Z0-9_]+)', $TOPART_url, $TOPART_parts)) {
                $TOPART_item = Item_get_by_title($TOPART_parts[1]);
                $TOPART_counts[$TOPART_item->ID]+= $TOPART_row['count'];
        # Title without date
        } else if (ereg('.php/([a-zA-Z0-9_]+)', $TOPART_url, $TOPART_parts)) {
                $TOPART_item = Item_get_by_title($TOPART_parts[1]);
                $TOPART_counts[$TOPART_item->ID]+= $TOPART_row['count'];
        # Query by num
        } else if (ereg('.php?p=([0-9]*)', $TOPART_url, $TOPART_parts)) {
                $TOPART_counts[$TOPART_parts[1]]+= $TOPART_row['count'];
        } else {
                //echo "Unknown URL: $TOPART_url<br>n";

arsort($TOPART_counts, SORT_NUMERIC);
echo "<ul>";
foreach($TOPART_counts as $TOPART_key => $TOPART_val) {
        if ($TOPART_key != "") {
                $TOPART_item = Item_get_by_ID($TOPART_key);
                if ($TOPART_item->status == "published") {
                        echo "<li><a href="", $TOPART_item->gen_permalink(),
                                "">", $TOPART_item->title,
                        if ($TOPART_numtoshow<=0) break;
echo "</ul>";

10 thoughts on “B2Evolution: Top Articles Hack”

  1. some how this is not working
    Just add it like you described it
    But exept of the header “TOP artiekls “nothing shows up .
    I use the Kubrick reloaded skin
    Searched on the Forum to nothing to find .
    Do i miss something ?

  2. This hack only knows about certain url styles. Your blog seems to use a different one. Uncomment the line that says echo “Unknown URL… and then see if you can come up with a new pattern rule for it.

    Perhaps this would work:

    # Query by title
    } else if (ereg('.php?(blog=[0-9]*&)?title=([^&]*)', $TOPART_url, $TOPART_parts)) {
    $TOPART_item = Item_get_by_title($TOPART_parts[2]);
    $TOPART_counts[$TOPART_item->ID]+= $TOPART_row['count'];}

  3. Thanks for the fast help
    get a bit closer but
    what to due with the other queries
    #Titel without date
    #Query by num
    #//Ful link with date

    And wher i have to put in the Code
    at the moment i comment out the other queries to get rid off the unknown urls and some parse failers.
    Thnak you for your frindly answers

  4. I now changed the code to :
    What you recomend ,it seems to work
    at least now pars failers (sorry have now idea about coding)
    have to uncoment one else comand at the end and ad somme } bevor the qeris hope that was ok .
    and its working like it shoud
    IF i make something wrong pleas
    correct me.
    Thank you for this nice plugin


  5. Thanks for your works.

    I do as your instructions but it can not work.

    MySQL error!

    Unknown column ‘visitURL’ in ‘field list'(Errno=1054)

    Your query:

    SELECT visitURL, count(*) as count

    FROM evo_hitlog

    WHERE hit_blog_ID = 2

    AND hit_ignore NOT
    IN ( ‘invalid’, ‘rss’, ‘robot’ )

    AND visitURL NOT REGEXP ‘/[0-9]*$’

    GROUP BY visitURL

    LIMIT 20

    Thanks in advance.

Comments are closed.