WordPress.org

Ready to get started?Download WordPress

Forums

Excluding link categories by name from wp_list_bookmarks (2 posts)

  1. bogeywebdesign
    Member
    Posted 3 years ago #

    I am trying to build a theme that will be used by other people (i.e. placed in the themes directory or some such). I want to add a simple feature that allows them to put links in the footer simply by creating a link category called "footer links".

    I am able to get that to work, however, I also have default sidebars that show links (unless they override them) and would like it not to redundantly put the "footer links" category in.

    I realize you can exclude from wp_list_bookmarks by id - however, since this will be released into the wild the id will be different for each user. So I would like to be able to do something like

    $footer_link_id = (some code to get footer link category id via it's name, similar to get_cat_id)
    wp_list_bookmarks('exclude_category='.$footer_link_id)

    (note, I realize my php may be mangled in the 2nd line, but basically it should do exclude_category=[footer_link_id])

    I have done myriad searches via Google and the Codex and have found nothing similar to this - many about using get_cat_id to get post categories, but nothing to get link categories. It's very likely the answer is out there and I'm just unaware.

    Please enlighten me. Many thanks in advance.

  2. bogeywebdesign
    Member
    Posted 3 years ago #

    Found the solution on my own. In case others run into the same problem, here is how I solve it:

    First, I made a function in functions.php

    function get_footer_link_id () {
      $link_categories = get_terms('link_category', array('name__like' => $category_name, 'include' => $category, 'exclude' => $exclude_category, 'orderby' => $category_orderby, 'order' => $category_order, 'hierarchical' => 0));
    
      foreach ( (array) $link_categories as $cat ) {
    		$params = array_merge($r, array('category'=>$cat->term_id));
    		$catname = apply_filters( "link_category", $cat->name );
    		// footer links category reserved for next to copyright
    		if($catname == 'footer links') {
    		  return $cat->term_id;
    		}
    	}
    }

    Then, in the file where I was calling the links, I simply did:

    <?php
          $footer_link_id = get_footer_link_id(); 
    
          $links = wp_list_bookmarks('exclude_category='.$footer_link_id.'&category_before=&category_after=&title_before=<p class="sidebarTitle">&title_after=</p>&show_images=0&echo=0');
    
    			if($links) { ?>
    				<?php echo $links; ?>
    			<?php } ?>

    This calls the above function, returns the ID I need, builds the call to wp_list_bookmarks and then saves it. This way I can check if it's empty and not bother to echo.

    Further note: this is still probably messy and will be cleaned up as I polish the theme. Suggestions welcome to improvements as well. I am but a novice at this and likely there are simpler ways. Perhaps make $footer_link_id global so I don't have to call the function repeatedly and/or do something to extend the defaults for wp_list_bookmarks to include excluding that category (though I'm not even sure how to do that without rebuilding the function in my functions.php - something that I started with to get me here and did not sit well with me since overriding base WP functions seems non-ideal)

Topic Closed

This topic has been closed to new replies.

About this Topic