Forums

[resolved] dynamic menu: Tags sorted by post views (6 posts)

  1. bloli
    Member
    Posted 1 year ago #

    Hallo, everybody!

    I'm new to Wordpress, PHP, MySQL and all this stuff, so it's quite difficult for me to realize the idea I have:
    I want to create a menu that is composed of tags (no "category"-menu). These tags shouldn't be sorted by their name or by the frequency of their use, but by the views of the posts they belong to. ... Let me show you, what I mean:

    To Post1 belong the tags "tag1" and "tag2". The number of post-views is: 10
    To Post2 belong the tags "tag2" and "tag3". The number of post-views is: 8

    Now, I add the number of post-views to the respective tags and divide the result by the number of their use:

    tag1: (10+0)/1=10
    tag2: (10+8)/2=9
    tag3: (0+8)/1=8

    So, the menu should look like this:

    • tag1
    • tag2
    • tag3

    The effect is that the menu is dynamic and reacts on the behaviour of the users. The tags, which are frequently asked for, automatically ascend within the menu.

    But I have no idea how to realize it! Could anyone please help me?!

  2. MichaelH
    moderator
    Posted 1 year ago #

    Well at the least you will need to get a plugin such as wp-postviews so that have something that keeps track of each post view. See: http://wordpress.org/extend/plugins/wp-postviews/

    Might consider hiring Lester 'GaMerZ' Chan (author of wp-postviews) to do something like that for you.

  3. bloli
    Member
    Posted 1 year ago #

    Thanks, Michael! I installed the plugin and it's really good!
    But I still can't handle the problem (and I don't want to hire anyone, because actually I don't want to spend any money for the launch of my blog... ^^).
    So, I thought about the calculation again:

    The new idea (Hooray!)

    tagX: (sum of views of the posts, tagged with tagX) / (addends or frequency of use of tagX) = Value for the interest for tagX
    or simplified:
    a/b=c <=> c=a/b

    I think I have to create a new column in the MySQL-tables for the result anyway - no matter how I realize this thing. So, the data which I need for "c" will be available (e.g. $wpdb->term_taxonomy.term_value or sth. like that). And the data I need for "b" is already available: $wpdb->term_taxonomy.count
    The only information I don't have is the one for "a". And because I don't want to grapple with this problem too long, I thought about "abolishing" "a":

    c=a/b <=> a=cb
    c=cb/b (<= not correct, yet!)

    When the function is opened, it should add "1" to "cb", so it counts the number of "openings":

    c=(cb+1)/b

    But the question still remains: How can I realize this?! I think the basic structure is much more easier now. But with my lack of php-knowledge, it's still too difficult for me. So, please help me.

  4. bloli
    Member
    Posted 1 year ago #

    Here's a first approach (it's the first time, I've written php-code, so I'm sure that it's full of mistakes):

    <?php
    $sql = 'SELECT term_value, count FROM term_taxonomy';
    $result = $wpdp->query($sql);
    
    insert($wpdb->term_taxonomy.term_value, (1+$result[term_value]*$result[count])/$result[term_value]);
    ?>

    Is it already pointing in the right direction?! What do I have to change and to add?! ...

  5. bloli
    Member
    Posted 1 year ago #

    Here I am... again! ^^ But I can already show some results! The problem nevertheless isn't solved completely, yet.
    Here's the function I have written:

    function TagValueBestimmen($term) {
        $term_id = $term->term_id;
        $tag_value = $term->tag_value;
        $count = $term->count;
    
        $newtv = (1+$tag_value*$count)/$count;
    
        var_dump($newtv);
    
        //Here's the problem:
        //$sql = 'INSERT INTO $wpdb->term_taxonomy(tag_value) VALUES ($newtv) WHERE term_id = $term_id;';
    }

    The var_dump of $newtv dumps the correct value. So, everything's fine up to that point.
    But then $newtv must be written into the MySQL-tables - and I don't know how this works. As I've already said I'm new to PHP, all I know comes from various Tutorials, I have absolutely no experiences with that...
    Here's what should be done: the value of $newtv should be written into the column "tag_value" from the table "term_taxonomy" in the row in which "term_id" is equal to the parameter $term_id.

    So, here we go again: Can anyone please help me?

  6. bloli
    Member
    Posted 1 year ago #

    It's dooooone!!!! :D Hooray!

    Here's the final function:

    function TagValueBestimmen($term) {
    
        $term_id = $term->term_id;
        $tag_value = $term->tag_value;
        $count = $term->count;
    
        $newtv = (1+$tag_value*$count)/$count;
    
        mysql_query("UPDATE wp_term_taxonomy SET tag_value = $newtv WHERE term_id = $term_id;") or die("failed");
    }

    The function is "opened" (don't know the correct term here) in the foreach-loop of get_the_term_list (category-template.php) and a modified version of the "New Tag Cloud" (http://wordpress.org/extend/plugins/new-tag-cloud/) dumps the data as a menu.

Topic Closed

This topic has been closed to new replies.

About this Topic