By yazuk


2019-01-08 19:44:55 8 Comments

In my theme, to display the featured post I have to add an extra category "featured". Now I have articles showing featured in the url instead of the main category. Any help please? Thanls

1 comments

@Krzysiek Dróżdż 2019-01-08 21:26:16

It all depends, how these categories are displayed in your theme.

If it's done nicely and with proper use of WP template tags, then that list comes from get_the_category() function (all other functions are using this one).

And at the end of that function you can find

return apply_filters( 'get_the_categories', $categories, $id );

So it's great news, because it means, that you can write your own filter and remove given category from the lists:

function prefix_remove_featured_category_from_post_categories_list( $categories, $id ) {
    // do whatever you want with $categories list
    // for example remove some category from the list
    $categories_to_remove = array(
        'cat-slug-a',
        'cat-slug-b'
    ); // Array of categories slug to be remove. Put your slugs in here

    foreach ( $categories as $index => $single_cat ) {

        if ( in_array( $single_cat->slug, $categories_to_remove ) ) {
            unset( $categories[ $index ] ); // Remove the category.
        }
    }

    return $categories;
}
add_filter( 'get_the_categories', 'prefix_remove_featured_category_from_post_categories_list', 10, 2 );

@Jacob Peattie 2019-01-09 07:54:03

They accepted the answer, so it must've worked, but the question was regarding the URL, not anything in the theme. I suppose the in the permalink is also based on get_the_category().

@Krzysiek Dróżdż 2019-01-09 07:55:34

@JacobPeattie commenting wrong answer/question? There is nothing about any URLs in this question ;)

@Jacob Peattie 2019-01-09 08:42:51

"Now I have articles showing featured in the url instead of the main category"

@Krzysiek Dróżdż 2019-01-09 08:45:40

Exactly - if you use category in urls, then it also is generated based on getting list categories (as all other lists of categories ;))

@Jacob Peattie 2019-01-09 08:46:52

So the issue with this solution would be that it's hiding the categories from the front-end also, when the question was only about deciding which category to show in the URL.

@Krzysiek Dróżdż 2019-01-09 08:48:57

The question was about using one category (featured) only for queries (so featured articles can be selected) and minimize other outcomes. Of course you can know it better than OP ;)

@Jacob Peattie 2019-01-09 08:50:56

They only mentioned the URL, so I'm not the one who's making assumptions about what OP wants.

@Krzysiek Dróżdż 2019-01-09 08:52:59

Just add your answer if you have better solution, downvote my answer, if you think it's a bad one. Why are we wasting time on this discussion?

Related Questions

Sponsored Content

0 Answered Questions

How to display the name of categories when multiple filtered?

2 Answered Questions

List only child categories a post is in, of a specific parent category in wordpress

  • 2017-01-25 11:52:48
  • pandu
  • 584 View
  • 0 Score
  • 2 Answer
  • Tags:   categories

1 Answered Questions

2 Answered Questions

[SOLVED] Specified number of categoy posts not showing correctly

  • 2017-04-01 15:23:08
  • Tarrmie de Man
  • 40 View
  • 2 Score
  • 2 Answer
  • Tags:   get-the-category

1 Answered Questions

[SOLVED] How to display category name from commenter's custom post

2 Answered Questions

[SOLVED] How to exclude specific category from the get_the_category(); array

1 Answered Questions

[SOLVED] create post with main category and other categories from admin panel

  • 2016-03-31 05:56:05
  • Lalji Nakum
  • 186 View
  • 0 Score
  • 1 Answer
  • Tags:   categories

1 Answered Questions

How to exclude the last post from a category(featured for example) from the main loop?

  • 2014-09-15 13:26:04
  • Cristian N
  • 140 View
  • 1 Score
  • 1 Answer
  • Tags:   categories loop

1 Answered Questions

Display deepest post category, when post have multiple categories

  • 2013-02-21 07:41:05
  • Justas Ju
  • 111 View
  • 0 Score
  • 1 Answer
  • Tags:   categories

1 Answered Questions

[SOLVED] How to add a post without assigning to a category?

Sponsored Content