WordPress.org

Ready to get started?Download WordPress

Forums

Add a "post-counter" by category on your nav-menu (8 posts)

  1. cavimaster
    Member
    Posted 3 years ago #

    >>>>>>>> IN "NAV-MENU.PHP" ADD

    function post_count($cat,$depth){
    global $wpdb;
    $a=0;
    $post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id=$cat;");
    for ($i=0;$i<$post_count;$i++){
    $post_count_id[$i] = $wpdb->get_var("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id=$cat;");
    $post_cont_pub[$i]= $wpdb->get_var("SELECT * FROM $wpdb->posts WHERE post_status='publish' AND ID=$post_count_id[$i];");
    if($post_cont_pub[$i]){ $a++; }
    }
    if ($depth=='1'){
    $post_count= sprintf("%02d", $a);
    return '<span>_'.$post_count.'</span> ';}
    }

    >>>>>>>> IN "NAV-MENU-TEMPLATE.PHP" FOUND AND REPLACE

    found >>> $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    replace by >>>> $item_output .= $args->link_before . apply_filters( 'the_title', post_count($item->object_id,$depth).$item->title, $item->ID ) . $args->link_after;

  2. Jackxugao
    Member
    Posted 3 years ago #

    hi, cavimaster

    I downloaded your private categories plugin. From the description of this plugin: "A login will then appear on the selected category page". How can I let the login area showing on the selected category page ? when I click the category name "History" in my site, the page shows "no pages found".

    Thanks!

  3. cavimaster
    Member
    Posted 3 years ago #

    Hi Jack,

    Perhaps you have not created a post or page in the history class?

    Complies with the procedure:

    - create a category [X]
    - go to the plugin settings and make it private
    - create at least one article (private) and assign it to category [X]

    When you click to go to a login category [X] then appears

  4. Erve1879
    Member
    Posted 2 years ago #

    Cavimaster - can you help me with the code - I couldn't find nav-menu-template.php
    and I wasn't sure which instance of nav-menu.php I should be editing...

    Thanks!

  5. cavimaster
    Member
    Posted 2 years ago #

    Hi Erve1879,

    you can found "nav-menu-template.php" and "nav-menu.php" in wp-include, not inside the theme folder.

    see ya

  6. Erve1879
    Member
    Posted 2 years ago #

    Thanks cavimaster - I tried the code, but it didn't work. I followed your instructions to the t, emptied the cache and reloaded the page.

    The sub-menu items had "_00" or "_02" before them, but the main nav items had nothing....

    I assume your code just isn't compatible with the theme I'm using....?

    Thanks for your help!

  7. Nenad1389
    Member
    Posted 2 years ago #

    Great hack. Thank you very much!

  8. cavimaster
    Member
    Posted 2 years ago #

    Thank you Nenad1389!

    In the same way, i fund a better code on the web. Am sorry but i dont remember the name of the original source...
    So, it's almost the same.

    http://www.devsector.ch/cavimaster/2011/09/compteur-de-posts-par-cat-dans-le-menu-wp

    Obviously this solution disappears if you made the WP next update

    See ya

Topic Closed

This topic has been closed to new replies.

About this Topic