WordPress tagss Categories and Archives Hacks
WordPress tagss Categories and Archives Hacks
Find WordPress category ID
Some themes requires that you fill a form field with a category ID. It may be easy to find a category ID if you’re an advanced WordPres user, but what about beginners? Here’s a very simple manner to find any category ID.
Once you’re logged in your WordPress dashboard, go to Manage and then in Categories.
Simply put your mouse cursor on the “edit” link related to the category you want to know the ID and look on your browser’s status bar: As you can see on the screenshot below, 1 is the id of the category.
Display Certain Categories in a Menu
In many cases, users only want to display certain categories in their navigation menu at the top of the page. There are limited spots, that can only be filled by top categories, but if you use the default wp_list_categories code, it will show all categories. This is why this hack below comes in very handy when you want to create a navigation menu and only display certain categories.
< ?php wp_list_categories(‘orderby=name&include=7,9,19,16,1,5,17,23’); ?>
Note, you can also change the ‘include’ text to ‘exclude’ and show all categories and exclude those that you don’t want displayed. The numbers displayed in the code are the category IDs. Remember since WordPress shows categories in a list format, you will need to edit the CSS in order to make it work.
Display tagss In A Dropdown Menu
I never liked tag clouds, for the simple reason that most of the time, they’re aren’t readable properly. Here is the solution to this problem: Displaying tags in a dropdown menu.
The first thing to do is to create the function. Paste the following code to your functions.php file:
< ?php function dropdown_tag_cloud( $args = ‘’ ) { $defaults = array( ‘smallest’ => 8, ‘largest’ => 22, ‘unit’ => ‘pt’, ‘number’ => 45,
‘format’ => ‘flat’, ‘orderby’ => ‘name’, ‘order’ => ‘ASC’,
‘exclude’ => ”, ‘include’ => ”
);
$args = wp_parse_args( $args, $defaults );
$tags = get_tags( array_merge($args, array(‘orderby’ => ‘count’, ‘order’ => ‘DESC’)) ); // Always query top tags
if ( empty($tags) )
return;
$return = dropdown_generate_tag_cloud( $tags, $args ); // Here’s where those top tags get sorted according to $args
if ( is_wp_error( $return ) )
return false;
else
echo apply_filters( ‘dropdown_tag_cloud’, $return, $args );
}
function dropdown_generate_tag_cloud( $tags, $args = ” ) {
global $wp_rewrite;
$defaults = array(
‘smallest’ => 8, ‘largest’ => 22, ‘unit’ => ‘pt’, ‘number’ => 45,
‘format’ => ‘flat’, ‘orderby’ => ‘name’, ‘order’ => ‘ASC’
);
$args = wp_parse_args( $args, $defaults );
extract($args);
if ( !$tags )
return;
$counts = $tag_links = array();
foreach ( (array) $tags as $tag ) {
$counts[$tag->name] = $tag->count;
$tag_links[$tag->name] = get_tag_link( $tag->term_id );
if ( is_wp_error( $tag_links[$tag->name] ) )
return $tag_links[$tag->name];
$tag_ids[$tag->name] = $tag->term_id;
}
$min_count = min($counts);
$spread = max($counts) – $min_count;
if ( $spread < = 0 ) $spread = 1; $font_spread = $largest - $smallest; if ( $font_spread using_permalinks() ) ? ‘ rel=”tag”‘ : ”;
foreach ( $counts as $tag => $count ) {
$tag_id = $tag_ids[$tag];
$tag_link = clean_url($tag_links[$tag]);
$tag = str_replace(‘ ‘, ‘ ‘, wp_specialchars( $tag ));
$a[] = “t$tag ($count)“;
}
switch ( $format ) :
case ‘array’ :
$return =& $a;
break;
case ‘list’ :
$return = “
nt
“; $return .= join(“
nt
“, $a); $return .= “
n
n”; break; default : $return = join(“n”, $a); break; endswitch;
return apply_filters( ‘dropdown_generate_tag_cloud’, $return, $tags, $args ); } ?>
Once done, you can use the function to get your dropdown menu of tags. Just open the file where you want the list to be displayed (Most of the time it is sidebar.php) and paste the following code:
Liste d’auteurs< ?php dropdown_tag_cloud('number=0&order=asc'); ?>
source
Display Categories in a drop down Menu
To add a good looking drop down that will list all your existing categories, insert the following code in your blog template. You can do it either in your sidebar.php file or anywhere in the index.php. This is the code :-
source
Display Archives in a drop down Menu
Just like in categories, you can have your monthly archives listed in a drop down. Add the following code to your template files :-
< ?php echo attribute_escape(__('Select Month')); ?>< ?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
source
Exclude Categories From Search
If for some reason, you’d like to exclude some categories from searches, you have to tweak WordPress a bit because there’s no built-in solution to this problem. Happilly, here’s a code to do that job!
To achieve this recipe, replace the categories IDs on line 3 and paste the following code on your search.php Template:
< ?php if( is_search() ) : $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("s=$s&paged=$paged&cat=1,2,3"); endif; ?>
source You are done!!!!!!!
Exclude Categories from your RSS Feed
Depending to your blog structure, it may be interesting to exclude some categories from your rss feeds. If you always wanted to find a clean way to do it, I’m pretty sure this recipe will make you happy.
Before starting to code, you’ll have to know the numeric ID of the categories you want to exclude. If you don’t know how to get the ID of a particular category, just read the first post in the category “WordPress tagss,Categories and Archives Hacks”.
Once you have the ID of the categories you want to exclude from your rss feed, open the functions.php file from your theme. If your theme doesn’t have a functions.php file, create one.
Paste the following code in it:
function myFilter($query) { if ($query->is_feed) { $query->set(‘cat’,’-5′); //Don’t forget to change the category ID =^o^= } return $query; }
add_filter(‘pre_get_posts’,’myFilter’);
source
Display your categories in two columns
The stadard wp_list_categories() functions echoes a list of all your categories. Let’s see how we can easily force the display of categories on two columns.
Simply paste the following piece of code where you’d like your categories to be displayed
< ?php $cats = explode("“,wp_list_categories(‘title_li=&echo=0&depth=1&style=none’)); $cat_n = count($cats) – 1; for ($i=0;$i< $cat_n;$i++): if ($i