WordPress.org

Ready to get started?Download WordPress

Forums

Need paid help! Print a phrase depending on the category name. (19 posts)

  1. Webmaster7
    Member
    Posted 8 years ago #

    Hello!

    I'm willing to pay $10 by Paypal if you give me this simple hack!

    This should be very easy if you know the right variables, but I've searched, and searched, and nothing...

    The hack must work in the single page where the post is shown, because I want to print a a different phrase depending on the name of the category, at the bottom of each article.
    The code can be put on the single.php file.

    For example, if the category is called dogs, I would like to print the phrase: Dogs are the man's best friend.
    If the category is called "giant animals", I would like to print: Elephants and wales are examples of big animals.
    Well, the examples are dumb, but you get the picture :)

    This could even be used to print different images depending on the category name. If the post is in the category dogs, you could show an image of a dog at the bottom of each article!
    The real problem is doing that in the single page, not in the category main page :/

    So, I only need a simple case structure, linked to the right variable, that can be called on the every page of my blog.
    I can't use functions like the_category(); because it prints a character, so the if clauses or cases don't work.

    I use WordPress 2.0.

    Thank you!!!

    Nuno

  2. Webmaster7
    Member
    Posted 8 years ago #

    Oh, if you don't need the money, I can donate it to WordPress :)

  3. Webmaster7
    Member
    Posted 8 years ago #

    Hello again!

    Oh, and it would be great if I could put the category name in my title too, for example: Post name - Category name

    Many thanks!

  4. Can't give you $10 worth of solution, but this might help:

    http://codex.wordpress.org/Template_Tags/get_the_category

    Many WP functions have corresponding get_ versions.

  5. Webmaster7
    Member
    Posted 8 years ago #

    Thank you, I will try it out :) I already tried so many things and variants...
    But my offer is still standing, if someone knows how to do it, even more now with the help of LesBessant :)

    Bye!!!

    Nuno

  6. brittanie
    Member
    Posted 8 years ago #

    I use the category_description tags on all my category pages, just under the category title. You can see it here, for example, under the word "Oklahoma."

    This tag works by reading the current category (no parameters needed), so it would pretty easy to put this under a post, right after where the category link for that post is.

    You can set the description or saying for each category in your dashboard, under "Manage —> Categories."

  7. Webmaster7
    Member
    Posted 8 years ago #

    Hello!

    I also have it working on my category pages, but I couldn't call it from a single page. I will try your advice, and if it works, I will code the decision tree :)

    Many, many thanks :)

  8. Webmaster7
    Member
    Posted 8 years ago #

    If you are inside the category archive, this tag <?php echo single_cat_title(); ?> also shows the title of the category, but if you are inside a single page it won't show anything. It doesn't recognize that page as belonging to a determined category.

    But, the suggestion of Les_Bessant does print the category name inside the single page, without any other special character attached!

    <?php
    foreach((get_the_category()) as $cat) {
    echo $cat->cat_name . ' ';
    } ?>

    Now I will try to attribute that to a variable, so that I can make some decision trees. Thanks!!!!!!

  9. Silkjaer
    Member
    Posted 8 years ago #

  10. Webmaster7
    Member
    Posted 8 years ago #

    Hello Silkjaer!

    I will check your advice. I think that an invisible character is being exported, and because of that this doesn't work.

    Using this:

    <?php foreach((get_the_category()) as $cat) { $nuno=$cat->cat_name . ' '; } if ($nuno == "cats") { echo "Hello";} ?>

    it doesn't print the word hello, and the category is really called cats :/
    So maybe the == doesn't work because what inside the variable nuno isn't just the word cats, but something more, like a special character or something.
    If I manually echo the variable $nuno it really prints cats. but the comparison doesn't work...

  11. Webmaster7
    Member
    Posted 8 years ago #

    Oh, those conditions tags may be useful someday, but it doesn't solve my problem :/
    I just really want to have the category name, without any invisible character, inside a variable. But the name that is being exported, always has some strange things that doesn't let the comparison work :(

  12. geoffe
    Member
    Posted 8 years ago #

    Do you know about these functions:
    http://codex.wordpress.org/Conditional_Tags#A_Category_Page

    You could just do


    if (is_category('cats')) {
    echo "Hello Cat Lovers";
    }

    or

    if (is_category('giant animals')) {
    echo "Elephants and wales are examples of big animals."
    }

  13. Webmaster7
    Member
    Posted 8 years ago #

    Hello Geoffe!

    That does work if you are in the category archive page, but if you click in any of the articles, it won't work on the single page, because the function is_category isn't recognized there :/ And I need those phrases in the end of each of my posts, not on the categories pages :(
    Oh, we were almost there...

    I really thank all your help.

    Nuno

  14. Webmaster7
    Member
    Posted 8 years ago #

    I found this plugin:
    http://guff.szub.net/2005/07/21/post-templates-by-category/

    and maybe I will try to work with it. I can then post a different template (including the texts I need) to all pages contained in a determined category. I hope it works...

  15. Webmaster7
    Member
    Posted 8 years ago #

    Woho, I think that the plugin from guff.szub.net is really what I needed! It worked. I will try it further, but if it does what I need, he will receive my donation.
    Thank you!

  16. geoffe
    Member
    Posted 8 years ago #

    That would be Kafkaesqui's site. Kafkaesqui is a moderator in this forum.
    http://codex.wordpress.org/User:Kafkaesqui/Projects

    You could alternatively use
    if(single_cat_title('', false)=='giant animals'){
    echo "Elephants and wales are examples of big animals.";
    }

    ...if all you want is to draw out the category name for some small custom displays in a single post page. But if you have a large scale of customization for each category, better to try the plugin. I'm not familiar with it but good luck.
    http://codex.wordpress.org/Template_Tags/single_cat_title

  17. davidchait
    Member
    Posted 8 years ago #

    The code earlier:
    <?php foreach((get_the_category()) as $cat) { $nuno=$cat->cat_name . ' '; } if ($nuno == "cats") { echo "Hello";} ?>
    ... isn't valid because it adds on a space each time. you'd need to either compare each category as you go:
    <?php
    foreach((get_the_category()) as $cat) {
    if ($cat->cat_name=='cats') {
    echo 'hello';
    }
    }
    ?>

    The issue is if you use multiple categories per post, and what to do in those cases.

    If you want to store text in the description and just print without 'checking', you could do something like:
    <?php
    foreach((get_the_category()) as $cat) {
    echo '<p>'.category_description($cat->cat_ID).'<p>';
    }
    ?>

    That will print ALL category descriptions if multiple cats per post...

    -d

  18. Webmaster7
    Member
    Posted 8 years ago #

    Hello!

    Geoffe, your code works perfectly if you are inside the category, but if you click on any entrie, the code stops working maybe because the single_cat_title function can't recognize the category to which the single entries belong.

    David, your multiple categories problem raise some questions, but at least for that site I won't have such problems, because each entry belongs to just one category.

    I decided to use Kafkaesqui plugin, and make a donation to him.

    Thank you all, this was really a great thread!!!

    Bye!

    Nuno

  19. Webmaster7
    Member
    Posted 8 years ago #

    Donations made:

    $5 for SzubNet
    $5 for Matthew Mullenweg

    Even without asking, I always keep my word :)

Topic Closed

This topic has been closed to new replies.

About this Topic