0

I am using Ciola theme from Theme forest and this is my site: http://www.gazetaswietojanska.org/pomorzekultury/. I have an issue concerning category callback on the homepage. As you can see, there are three vertical modules that display posts from selected categories, namely "Aktualnosci", "Recenzje i Relacje" and "Nadchodzace". Between the image and the title there is a callback category box ( the little red boxes). When I assign a post to category, it is displayed in this box. The problem is when I have my post assigned to more than one category, e.g. three of them and the theme randomly chooses one category to be displayed in this box. This is the code that generates this box:

     // Get category meta data
             foreach( ( get_the_category() ) as $category ) 
                    {
                        $category_name = $category->cat_name;
                        $category_url = get_category_link($category);
                        $cat_com_url = get_comments_link();
                    }

  <span class="cb-category"><a href="<?php echo $category_url; ?>"><?php echo $category_name; ?></a></span>

My question is whether there is any possibility to have total control of which category is displayed in this box? It would be an extremely useful feature on my website.

If I didn't submit all the important data/code, this is the whole PHP file http://pastebin.com/QBXjCzgA

I hope that You will help me :)

0

Yes there is such possibility.

Just change your code to this:

$main_category_name = YOUR MAIN CATEGORY NAME;  // get the main category of this section (i.e nadchodzące)

foreach( ( get_the_category() ) as $category ) {
    if ( $category->cat_name != $main_category_name ) {
        $category_name = $category->cat_name;
        $category_url = get_category_link($category);
        $cat_com_url = get_comments_link();
    }

<span class="cb-category"><a href="<?php echo $category_url; ?>"><?php echo $category_name; ?></a></span>

Just change YOUR MAIN CATEGORY NAME to your real category name.

EDIT: I did what you told me to do, asked my friend for some more help and finally it works! thank you very much Krzysiek, you have solved this problem for me and I am very grateful :) Now it looks much better.

I am adding the code just to make things crystal clear:

     $main_category_name = $parent_cat_name; // get the main category of this section (i.e nadchodzące)

$faki = 0;
foreach( ( get_the_category() ) as $category ) {


    if ( $category->cat_name != $main_category_name ) {
    $category_name = $category->cat_name;
    $category_url = get_category_link($category);
    $cat_com_url = get_comments_link();
    }

    if($faki ==0)
    {
        $category_name = $category->cat_name;
        $category_url = get_category_link($category);
        $cat_com_url = get_comments_link();
    }
    $faki++;
}

Thanks again for your help!

  • Thank you very much for your concern and rapid answer :) Unfortunately my knowledge of PHP is horribly limited and I do not know if I get it right and what is more, if I am capable of doing that... I have started using Wordpress a month ago so I still have many "dummy" questions. Could you give me a simple example of how it would work? Thanks again for help :) – Rafał Wyszomirski Jul 12 '13 at 21:27
  • Well, I could, but you'll have to tell me which category should be displayed? If there are 3 categories assigned to given post, which two of them should be ignored? – Krzysiek Dróżdż Jul 12 '13 at 21:33
  • For example it would be great if in "Nadchodzace", the category of the same name (slug is nadchodzace) wouldn't be displayed in the box, because it is already known that this post is in this category :). P.S. As you can see, the way these category boxes are applied is strange... Two out of four posts have nadchodzace, even though they have other categories checked. Thanks for help Krzysiek :) – Rafał Wyszomirski Jul 12 '13 at 21:51
  • I've updated my answer. The only thing you have to do now is to set $main_category variable. – Krzysiek Dróżdż Jul 12 '13 at 21:57
  • Ok, so I replaced what I had with yours: $main_category = nadchodzące; // get the main category of this section (i.e nadchodzące) foreach( ( get_the_category() ) as $category ) { if ( $category->cat_name != $main_category->cat_name ) { $category_name = $category->cat_name; $category_url = get_category_link($category); $cat_com_url = get_comments_link(); } } But it didn't work... Or most probably I did something wrong. – Rafał Wyszomirski Jul 12 '13 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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