WordPress.org

Ready to get started?Download WordPress

Forums

New wp_list_categories() template file (12 posts)

  1. Lorelle
    Member
    Posted 7 years ago #

    A "first draft" of the specifications for the new replacement template tag for wp_list_cats() is now on the WordPress Codex, online WordPress manual at wp_list_categories().

    There are some changes to the parameters so WordPress Theme and WordPress Plugin authors should check their WordPress Themes in WordPress 2.1.

    WordPress 2.1 now includes blogroll link categories in the post category list. The idea is to match content with blogroll links. The upgrade adds and changes database tables to accomodate this new feature, which may mess up your sidebar category links.

    Theoretically, if you do not have any posts assigned to your blogroll categories, blogroll link categories will not appear in your categories list unless you have it set to "show all" categories and override the default settings.

    If you do have post categories that matched your blogroll categories, you might have a bit of a mess. Hopefully, someone here will jump in with a solution on how to clean that up.

    WordPress 2.1 should be backwards compatible with wp_list_cats() but if you are having trouble, consider changing your sidebar template file ( sidebar.php ) to the new wp_list_categories() template tag.

  2. alfaspider71
    Member
    Posted 7 years ago #

    Is there any way to prevent child categories from being displayed? wp_list_cats supported this in the 2.0.x series, but it doesn't seem to work in wp_list_cats or wp_list_categories in 2.1.

  3. mariogarcia
    Member
    Posted 7 years ago #

    If you look for it you can find. I had the same problem and i solutionated it before
    http://wordpress.org/support/topic/101667?replies=9#post-503031

  4. songfta
    Member
    Posted 7 years ago #

    Per Lorelle's comment: if anybody can figure out how to untangle the mess she described, I'm all ears!

  5. Lorelle
    Member
    Posted 7 years ago #

    To prevent categories from being displayed, put them in an "excludes" parameter:

    <?php wp_list_categories('show_count=0&use_desc_for_title=0&title_li=0&exclude=1, 16, 30, 31, 32, 78, 80, 9'); ?>

    This will generate a list of categories, except for those listed, without a post count, and no title for the list.

  6. schrade
    Member
    Posted 7 years ago #

    I installed 2.1 a few days ago, and trying to exclude a category. I am unable to get the code you display to work (in any form or fashion).

    <?php wp_list_categories('optioncount=1&hierarchical=0&title_li=<h2>Categories:</h2>&exclude=6'); ?>

  7. klklaser
    Member
    Posted 7 years ago #

    This sounds like a really cool function from a blog conversation standpoint.

    I would venture a guess that one way to eliminate a conflict between blogroll categories and category names would simply be to list each of them, note any duplicates, then rename either the category or blogroll category name to something else before performing the upgrade.

  8. cousinagam
    Member
    Posted 7 years ago #

    Using "exclude" is fine if you don't have many categories to exclude. However, I have over 100 categories, and I want the sidebar to show only three of them. All the others are children of those three.

    Since the site is full of family stories, and one of the main categories is "Characters" (people in the stories), there are both figuratively and literally a lot of children. I don't need all those people cluttering up the sidebar.

    I'm going to try falling back to list_cats, though I'm not optimistic.

    In the meantime I hope someone will add the highly useful "no children" feature to list_categories.

  9. cousinagam
    Member
    Posted 7 years ago #

    Well, list-cats did not work. However, I found something that does for me.
    (Meaning, something that lets me show only categories 2, 3, and 4 in the sidebar, not their children.)
    It's kludgy but it works.

    1. I edited the sidebar to have a text label for the word "Categories" (e.g., <h2>Categories</h2> )
    2. I added <ul><li> below that
    3. I entered the hyperlink for Category 2, with a text label for the category's name ("Stories")
    4. I repeated that for the next two categories I want to have in the sidebar
    5. I closed the list ( </ul> )

    As I say, it's kludgy, but it works just fine.

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    (Meaning, something that lets me show only categories 2, 3, and 4 in the sidebar, not their children.)

    You mean, like this?
    wp_list_categories('include=2,3,4');

  11. csleh
    Member
    Posted 6 years ago #

    Include is great if you have categories that won't be added to. If you want to add categories later you have to edit the template file. Not good if the site is being handed off to others to use.

  12. joe1972
    Member
    Posted 6 years ago #

    I helped myself by introducing the children=0parameter to wp-includes/categeroy-template.php file inside the function function wp_list_categories($args = ''). I placed the following code snippet pretty at the top of the function (after the line $r = wp_parse_args( $args, $defaults );):

    if ( isset($r['children']) ) {
    		if($r['children'] == 0) {
    			$query = "SELECT w.term_id AS id FROM wp_terms w, wp_term_taxonomy t
    						WHERE t.term_id = w.term_id AND
    						t.taxonomy = 'category' AND
    						t.parent = 0 ORDER BY w.name";
    			$tlc_ids = $wpdb->get_results($query);
    			foreach($tlc_ids as $c) {
    				$tlcs[] = get_category($c->id);
    			} /* now we have all top level category objects. */
    
    			/* Which is the active category? */
    			if($_SERVER['REQUEST_METHOD'] == 'GET') {
    				if(isset($_GET[cat])) {
    					$current_cat_id = $_GET[cat];
    				}
    				else {
    					$current_cat_id = NULL;
    				}
    
    				/* Let's mark the active top level category. */
    				foreach($tlcs as $cat) {
    					if("{$cat->term_id}" == $current_cat_id) {
    						$cat->active = true;
    					}
    					else {
    						$cat->active=false;
    					}
    					$res[]=$cat;
    				}
    				$tlcs = $res;
    			}
    			return $tlcs;
    		}
    	}

    It may not be the best solution but it does the job for me: Just gives me all top level categories only!

Topic Closed

This topic has been closed to new replies.

About this Topic