Display certain post categories under post title
-
Hi, I found a bit of code via the search on the forums here, but its not quite how I hoped. Im using this:
<?php $category = get_the_category(); if($category[0]){ echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } ?>Which only displays the first category.
Im hoping some php/wordpress wizard could help me do the following:
1) Display all categories, apart from one (This because the theme requires all posts to have part of feature (so they appear on the home page).
2) Format them like [Movies}[TV] etcI did try the codex thing, struggled to get this far would really appreciate some advice!
Thanks in advance
-
nice one alchymyth, thanks a lot for posting those topics…I didnt see them (or scrolled right by). On my way to do it just now!
Cheers buddy!
Hmm, after trying them all…I can only seem to get the following to work
<?php foreach((get_the_category()) as $cat) { if (!($cat->cat_ID=='322')) echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. $cat->cat_name . '</a>' . ', '; } ?>I put 322 as the parent (video) category, which now has them all (they also link to the page which is awesome), but I still need to exclude one cateory. I noticed in the thread which you posted has:
$exclude = array('Something','Something Else','Blah','YAY');Can I incorporate that into the first bit of code?
Thanks
Or if somebody could help me get this one working (this is the one everybody else seems like rate):
function the_category_filter($thelist,$separator=' ') { if(!defined('WP_ADMIN')) { //list the category names to exclude $exclude = array('Something','Something Else','Blah','YAY'); $cats = explode($separator,$thelist); $newlist = array(); foreach($cats as $cat) { $catname = trim(strip_tags($cat)); if(!in_array($catname,$exclude)) $newlist[] = $cat; } return implode($separator,$newlist); } else return $thelist; } add_filter('the_category','the_category_filter',10,2);I put it in my function.php, (nothing in my index?) but nothing shows up in under my posts. I changed the following:
function the_category_filter($thelist,$separator=' ') { if(!defined('ADMIN')) { //list the category names to exclude $exclude = array('Something','Something Else','Blah','YAY'); $cats = explode($separator,$thelist); $newlist = array(); foreach($cats as $cat) { $catname = trim(strip_tags($cat)); if(!in_array($catname,$exclude)) $newlist[] = $cat; } return implode($separator,$newlist); } else return $thelist; } add_filter('the_category','the_category_filter',322);322 = parent video category
ADMIN = Was WP_ADMIN ? Tried both default and ADMIN, just incase
Exluded names = Featued (the cat i wont to exclude)Did I mess things up or am I missing something?
Cheers folks
untested:
change from:
if (!($cat->cat_ID=='322')) echo '<.....using the array with category names to exclude; change to:
if (!in_array($cat->category_name,$exclude)) echo '<.....So close, yet so far. Ok, so I have:
<small><?php foreach((get_the_category()) as $cat) { if (!in_array($cat->category_name,$exclude)) echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. $cat->cat_name . '</a>' . ', '; } ?></small>I still have the other code in my function.php with the names I want excluded…but they are all still showing.
Thanks for this btw, appreciated!
Tested and works, excludes the category link “Test Category One” under the posts but shows the others
<?php // For each of the categories, create them as category variables foreach((get_the_category()) as $category) { // If the category is NOT the cat_name (category name) then echo some stuff // So grab all... except where they match cat_name (exclude basically) if ($category->cat_name != 'Test Category One') { echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '; } } ?>you can replace the_category with it.
Amazing, thats so much deepbevel (and alchymyth). That works a treat, I also see the notes for me to read. I wanted to post and say thanks before i go back over it (and your notes) so I try get my head round it.
Thanks for taking to post such a cool bit of code.
*Virtual high five*The Sweepers idea is better, using an array to exclude more than one category if needed. I’ll have to try and apply that.
If you need to hide more than one, this includes rather than excludes:
<?php if (is_category( array( 1,3,5 ) )) {$category = get_the_category(); if($category[0]){ echo '<a>term_id ).'">'.$category[0]->cat_name.'</a>'; } } ?>From another related thread that alchymyth was helping on.
Haha, I was trying to exclude more than one, but no success. Kept giving me the dreaded white page. I might just move all the categories up a level (i have feature + videos above tv, movies, music etc)
Can the method you posted (thats works!!!) be easily stylized? I was hoping to go for [Movies][TV][Music] etc or something to that effect.
You read my mind man, going to try now π
<?php if (is_category( array( 1,3,5 ) )) {$category = get_the_category(); if($category[0]){ echo <a>term_id ).'">'.$category[0]->cat_name.'</a>'; } } ?>Is there an extra ‘ after the echo but I couldnt get it working…dreaded white screen again.
<?php if (is_category( array( 3,7,18,28 ) )) {$category = get_the_category(); if($category[0]){ echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } } ?>I’m really bad with writing this stuff, try this, just worked for me.
Can the method you posted (thats works!!!) be easily stylized? I was hoping to go for [Movies][TV][Music] etc or something to that effect.
Are you talking about styling each link with a different style? not sure about that but Ill think about it. I don’t know how to write it but I imagine it would involve something like this psudo-code:
<?php if (the cat link is ('category-slug')) { ?> <style> .entry-meta a, .entry-utility a { color: #888888; </style> <?php } ?>but I have no idea if this is the way to go about it.
The topic ‘Display certain post categories under post title’ is closed to new replies.