• parthatel

    (@parthatel)


    How do I get the count of links in a link category? This code get the count of posts in a category, but not the links:

    function wt_get_category_count($input = '') {
    	global $wpdb;
    	if($input == '')
    	{
    		$category = get_the_category();
    		return $category[0]->category_count;
    	}
    	elseif(is_numeric($input))
    	{
    		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
    		return $wpdb->get_var($SQL);
    	}
    	else
    	{
    		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
    		return $wpdb->get_var($SQL);
    	}
    }

    Furthermore, I have post categories and link categories with the same id. In this case, both posts and links are added to the same category id.

Viewing 4 replies - 1 through 4 (of 4 total)
  • MichaelH

    (@michaelh)

    <?php
    //get link count for link category id 10
    $term = get_term( 10, 'link_category') ;
    if ($term) {
     echo 'Link category ' . $term->name . ', number of links ' . $term->count;
    }
    ?>

    Use get_terms if you need to do that for all your link categories

    I have post categories and link categories with the same id.

    How did you manage that then, the database shouldn’t allow duplicate term or taxonomy IDs, or are you referring to the name?

    The only difference in terms of how it sits in the database between a link category and a post category is the taxonomy name/type…

    MichaelH

    (@michaelh)

    That’s a good question t31os_ – I should have also provided this:
    How do I determine a Post, Page, Category, Tag, Link, Link Category, or User ID?

    Thread Starter parthatel

    (@parthatel)

    Thanks MichaelH! The code worked.

    Apart from the main question, here’s what I did to get post and link categories with the same ID and NAME.

    I added a post category with a name, for example, Category A.
    When I added a link category with the name Category A, they both pointed to the same id. (So now I’m afraid to delete the link category since it will also delete my post category.)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to get the count of links in a link category?’ is closed to new replies.