Support » Fixing WordPress » Getting category name/id from a post title

  • Hi everyone,

    I was wondering if there is a way through which I could get the category’s name or id of a post by supplying post’s title.

    The thing is that I have a bilingual blog, with some posts in English and others in Urdu. In my current theme, I also display the title of the next and previous posts when a single post is viewed. Since I use a different font for displaying Urdu characters correctly, I have a class (.urdu) in the CSS and I use something like <span class="urdu">Urdu post title here</span> as the title of Urdu posts while saving them. This allowed the previous_post_link() and next_post_link() functions to correctly display the post title in my desired font on a single entry page. (An example is this post).

    Now in my opinion, stuffing tags in post title is not a nice approach. It also required some irritating changes here and there. If I don’t use tags in post title, the only problem I would encounter would be that if, while displaying the next/previous post title, there comes one with its title in Urdu, then it won’t be displayed correctly, since I would have no idea that the upcoming post is in Urdu and thus I won’t be able to use the suitable font.

    The solution which came in my mind was that if I just get the next/previous post title, and then check whether it belongs to the category ‘Urdu’, then I can modify the PHP easily to apply the .urdu class and display the title correctly.

    Any help or ideas about this (or a better solution) would be greatly appreciated. Thanks!

    P.S. Sorry for the awfully long post 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • you can use an ‘if cat=’ clause to change the font in the loop
    is a good place to start…

    Thanks for responding blogtackular (that was fast!).

    However, I might not have been able to explain properly. In the loop, it’s easy. In fact, I do change the font for Urdu content wherever the loop is being used through ‘if cat’. But it’s the previous and next post titles I am having trouble with. They are displayed in the sidebar, and in sidebar.php I check if it’s the single post and then display that post’s category, related posts and posts which are next and previous to it. I want to format the titles of next and previous posts (if they are in ‘Urdu’ category) and couldn’t find any way to determine their categories.

    Ah! So yo mean if the “next post’ is in Urdu, it displays in the font…

    The best place to browse is and the related previous_post page, and you should get some answers, although I think it may be a bit complicated to do!
    With the next and previous posts, you can exclude categories, and only display the next post in the same category, so it can parse the cat number…

    Well, the functions can exclude posts from a specific category, or they can show posts from the same category, but I want to do neither.

    *scratches head*

    OK, I checked the code of previous- and next-posts’ links functions, played a little, and then made a small plugin out of the tweaked code to do the job. Thanks for pointing, blogtackular! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Getting category name/id from a post title’ is closed to new replies.