0

I'm stuck in a strange situation right now. So I have my wordpress posts which belong to multiple categories. e.g one of my post belongs to two categories. One is 'News' category, and the other one is 'Top Bar'. (I use it to assign posts to the header posts area which fetches its articles from 'Top Bar' category.

The problem i'm facing is that I want to show only the 'News' Category name on the post block. Right now it randomly selects one of the selected category's name from the list and displays it. Every time I refresh the page, the post block shows the name of one of the two. I just want the main one to show which is called 'News'.

Now if you visit this link http://thexpatt.com/news , the posts category title will be News with some them showing top news. if you keep refreshing the page, the title was keep switching. In reality, all those posts have the Top News category as well as news category.

I'm attaching a screenshot for your understanding. I refreshed the page multiple times. Notice the difference in categories display name blocks over the posts.

1st Visit

3rd Visit

My theme calling one of the selected categories randomly. If yes, how do I make it call the first one only or how do i exclude certain categories from being selected.

EDITED

Code i found in post.php it even says randon ID. how do i change that to ?

$postCategories = get_the_category($post->ID);
$slug = '';
foreach($postCategories as $catSlug) {
    $slug.= $catSlug->slug." ";
}

$categories = wp_get_post_categories($post->ID); 
if(!empty($categories)){
    $catCount = count($categories);
    **//select a random category id**
    $id = rand(0,$catCount-1);
    //cat id
    $catId = $categories[$id];
} else {
    $catId = false;
}

OR HELP check if the yoast primary category is selected and print that. Yoast category inclusion code is something like this. (As found on the Internet).

    $primary_cat_id=get_post_meta($product>id,'_yoast_wpseo_primary_product_cat',true); 
if($primary_cat_id){    
$product_cat = get_term($primary_cat_id, 'product_cat');    if(isset($product_cat->name)) 
echo $product_cat->name; }

Thanks a lot.

closed as off-topic by Michael, Jacob Peattie, cjbj, Pat J, Fayaz Nov 19 '17 at 13:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – Michael, Jacob Peattie, Pat J, Fayaz
If this question can be reworded to fit the rules in the help center, please edit the question.

0

You can use this code in your functions.php to hide the TopBar category.

function the_category_filter($thelist,$separator=' ') {
    if(!defined('WP_ADMIN')) {
        //list the category names to exclude
        $exclude = array('TopBar');
        $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);
  • No the problem is not only in the home page but on all pages. For instance visit my visit and go to news page. That page simply draws posts from 'News' Category but the 'Top Bar' blocks keep showing randomly on posts everytime you refresh. I do not want it to ever show 'Top Bar' in that news block even if it belongs to it. Top Bar category is only used for the main header News Block. – Ghani Rajput Nov 11 '17 at 1:56
  • Updated my answer, now the TopBar category is hiden everywhere. – IXN Nov 11 '17 at 2:31
  • thanks for trying to help me! but its still doing the same thing on thexpatt.com/news - try refreshing the page, you will see what i mean. I even used Yoast SEO primary category function but even that has no effect. I added it to the functions.php in theme's main folder. slug for my category is 'topbar' . Also what does 10,2 define in the last part of the code. Do i need to fill it according to my configurations? Thanks once again! – Ghani Rajput Nov 11 '17 at 3:47
  • Two things:in my code replace TopBar with the exact spelling you defined the category (capitals etc.). Also only use this code to modify the categories, don't use all codes (your previous code, Yoast etc.) together. 10,2 define in what order the filter will be applied to the core code. See also codex.wordpress.org/Function_Reference/the_category – IXN Nov 11 '17 at 4:17
  • Hey I edited my main post and added the code I found in Post.php . I think this is it. We need to change this. If you notice in the end it says it's picking up random ID. Help me change that please. – Ghani Rajput Nov 11 '17 at 5:47
0

I would say root if the issue is that category isn't meant to serve such kind of functional purpose. Categories are purely grouping mechanism, but they have no concept of those groups being functionally differentiated.

Still, that is what you already have in place.

Without changing how categories are used I would adjust category archive template to output name of current archive instead of categories of each individual post.

That would achieve your desired use case (everything on /news says News) and would be less prone to changes over time than micromanaging output of post's categories.

  • Sorry for an unrelated question, couldn't find a way to contact you. Why did you shutdown QueryPosts website? – I am the Most Stupid Person Feb 21 at 10:06
  • 1
    @IamtheMostStupidPerson my profile has my twitter handle. :) I wasn't interested in further maintaining or developing it, simple as that. I hoped to accomplish more with it, but clearly that wasn't happening so it was time to let it go. – Rarst Feb 21 at 11:09
0

Orange These support answered my question. If anyone else is facing the similar situation the use this way to fix Solidus Theme Multiple Category Fix. Thanks

To set up one single selected category for the news block you will need to find in solidus-theme\includes\home-blocks files lines like these:

$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;

and replace to lines like

if(is_array($cat)) {
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
} else {
$catId = $cat;
}

So what will do these lines, in case the homepage block will have just one selected category, then it will show just one, but is you will have select multiple categories then it will show randomly as before. But in case a post will have multiple categories and you will have selected one for the block it will show just one.

Not the answer you're looking for? Browse other questions tagged or ask your own question.