Cheesetoast » categories http://www.cheesetoast.co.uk Brain Food for Web Developers Mon, 07 Jul 2014 23:18:22 +0000 en-US hourly 1 http://wordpress.org/?v=4.2.10 Highlight Post Categories in WordPress using the_category() http://www.cheesetoast.co.uk/highlight-post-categories-wordpress/ http://www.cheesetoast.co.uk/highlight-post-categories-wordpress/#comments Fri, 16 Nov 2012 19:41:35 +0000 http://www.cheesetoast.co.uk/?p=1000 Here's a quick code snippet that allows you to style the post categories displayed alongside each Wordpress post by wrapping them in a span tag using the_category function. Continue reading

The post Highlight Post Categories in WordPress using the_category() appeared first on Cheesetoast.

]]>
Categories "Featured" and "Services" have been separately highlighted in blue.Here’s a quick code snippet that allows you to style the post categories displayed alongside each WordPress post.

Basically, during ‘the loop’ you’d use the_category() function to output the categories that the current post belongs to. Here is a simple example of the_category function in use:

<p>Posted in <?php the_category(', '); ?></p>

Example output: Posted in Featured, Services

The first parameter in the_category() function is the separator. In the above example I’ve used a comma to separate the categories listed but it’s also common to add arrows, blank spaces, or bullet points.

What I want to accomplish is to highlight each category by wrapping them in a span tag. The problem is that the separator is only placed between categories so in order to wrap each one I need to place an open span before and after I call the_category function. Then I can place a closing and opening span as the separator. Like so:

<p>Posted in <span class="round label"><?php the_category('</span> <span class="round label">'); ?></span></p>

Which will output the following code for two categories:

<p>Posted in
<span class="round label> <!-- First category link code --> </span>
<span class="round label> <!-- Second category link code --> </span>
</p>

Read more about the_category function and it’s parameters from the WordPress codex.

The post Highlight Post Categories in WordPress using the_category() appeared first on Cheesetoast.

]]>
http://www.cheesetoast.co.uk/highlight-post-categories-wordpress/feed/ 0
Exclude Category from WordPress RSS Feed http://www.cheesetoast.co.uk/exclude-categories-wordpress-rss-feed/ http://www.cheesetoast.co.uk/exclude-categories-wordpress-rss-feed/#comments Tue, 28 Aug 2012 19:21:15 +0000 http://www.cheesetoast.co.uk/?p=665 A useful little code snippet to remove categories from the Wordpress RSS feed. Continue reading

The post Exclude Category from WordPress RSS Feed appeared first on Cheesetoast.

]]>
A useful little code snippet to remove categories from the WordPress RSS feed.

Just add the following code to your function.php file:

function exclude_RSS_cat($query) {
if ($query->is_feed) {
$query->set('cat','-12');
}
return $query;
}

add_filter('pre_get_posts','exclude_RSS_cat');

This will remove the category with the ID of 12.

The post Exclude Category from WordPress RSS Feed appeared first on Cheesetoast.

]]>
http://www.cheesetoast.co.uk/exclude-categories-wordpress-rss-feed/feed/ 4
Exclude Category from WordPress Blog Page http://www.cheesetoast.co.uk/exclude-include-category-wordpress-blog-page/ http://www.cheesetoast.co.uk/exclude-include-category-wordpress-blog-page/#comments Mon, 27 Aug 2012 18:37:26 +0000 http://www.cheesetoast.co.uk/?p=642 Often when designing Wordpress themes you'll want to exclude certain post categories from appearing in your blog page. Luckily it's very easy to do. Continue reading

The post Exclude Category from WordPress Blog Page appeared first on Cheesetoast.

]]>
Often when designing WordPress themes you’ll want to exclude a category from your blog page. Luckily it’s very easy to do.

Open up your index.php file (or whichever file you’re using to display your blog posts) and add this line of code right before the loop:

<?php query_posts('cat=-12'); ?>

Add the code right before this line:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

This will exclude the category with id of 12.

If you want to exclude more than one category you can use:

<?php query_posts('cat=-4,-12,-14'); ?>

Include a specific category

If you only want to include a certain category you just remove the minus character from the query:

<?php query_posts('cat=12'); ?>

Additional arguements

You can also add additional arguments to the query. In this example I will only display posts from a specific category and time period:

<?php query_posts('cat=12&year=2012'); ?>

You can take this a step further and only display posts from the current month by adding some php:

<?php query_posts( $query_string . '&cat=13&monthnum=' . date( 'n', current_time( 'timestamp' ) ) ); ?>

There are plenty of other arguments available to further filter your posts. Read more from the WordPress Codex: Query Posts.

The post Exclude Category from WordPress Blog Page appeared first on Cheesetoast.

]]>
http://www.cheesetoast.co.uk/exclude-include-category-wordpress-blog-page/feed/ 0