Get first category only and excluding one

Within the post loop you can do, $category = get_the_category(); if($category[0]->cat_name == “featured”) { //if first category in array is “featured”, get next category in line $name = $category[1]->cat_name; $cat_id = get_cat_ID( $name ); $link = get_category_link( $cat_id ); echo ‘<a href=”‘. esc_url( $link ) .'””>’. $name .'</a>’; } else { //get the first category … Read more

trouble with my loop

cat accepts category IDs, not slugs or names. a better method here is to use the pre_get_posts action, the Codex page has an example that does exactly what you’re trying to do.

Display 1 category only with get_the_category (by ID or slug)

I think you could do something like this: $categories = get_the_category(); $displayed_category_id = 1; // set this to the category ID you want to show $output=””; if($categories){ foreach($categories as $category) { if ( $displayed_category_id == $category->term_id) { $output .= ‘<a href=”‘.get_category_link($category->term_id ).'” title=”‘ . esc_attr( sprintf( __( “View all posts in %s” ), $category->name ) … Read more

How to exclude one category

You can use “category__not_in” array parameter. Also, I’ve modified your code just a bit, you should use wordpress best practice which is “if -> while” to display the posts. Please try these code below as I haven’t tested it yet. <?php // Default arguments $args = array( ‘posts_per_page’ => 6, // How many items to … Read more

tech