WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to say "if category exists.. (14 posts)

  1. andreacutelli
    Member
    Posted 4 years ago #

    Hi there!

    I have a trouble.. but I don't know if is it possible to resolve.
    (sorry for my english..)

    example, how can I say:

    if the category (es. 3, 14, 55, 113) exists.. show me "hello!"
    else do nothing
    while
    if category (es. 1, 10, 22) exists.. show me "goodbye!"
    else do nothing

    than'you in advance!
    Andrea

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    <?php if( is_category('3, 14, 55, 11') ) echo 'Hello';
    elseif( is_category('1, 10, 22') ) echo 'Goodbye';?>
  3. andreacutelli
    Member
    Posted 4 years ago #

    thank'you very much esmi !! :)))

    ..may I ask another thing ?

    I must write this:

    <?php if( is_category('4') ) echo '';?>

    but the category must be this:
    category-<?php echo get_post_meta($post->ID, "whatiwant", true); ?>

    and the echo must be this:

    <?php echo get_post_meta($post->ID, "whatiwant", true); ?>/">
    how can I write this in php..??

    sorry again..!

  4. andreacutelli
    Member
    Posted 4 years ago #

    ops.. I have write wrong href..

    and the echo must be this:

    <li><a href=" <?php echo get_option("home"); ?>/<?php echo get_post_meta($post->ID, "whatiwant", true); ?>/">example</a></li>

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    Sorry? What is it that you're actually trying to do?

  6. andreacutelli
    Member
    Posted 4 years ago #

    well, I have a custom field with a specific value (es. xxxxx)

    this specific value is a part of a category name (es. category-xxxxx)

    I wish that:

    if category-xxxxx exists, display a link of this kind
    <a href="www.site.com/category-xxxxx/>category of xxxxx</a>
    else do nothing

    thanks a lot esmi

  7. esmi
    Forum Moderator
    Posted 4 years ago #

    The code I gave above isn't going to work then. It tests to see if you're in a particular category - not whether that category exists.

    See if this works:

    <?php
    $cat_name = 'category-' . get_post_meta($post->ID, "whatiwant", true);
    if( get_cat_ID( $cat_name ) ) :?>
    <li><a href="<?php bloginfo('url'); echo $cat_name; ?>/">example</a></li>
    <?php endif;?>
  8. andreacutelli
    Member
    Posted 4 years ago #

    thanks 4 reply but not works.. :-(

  9. andreacutelli
    Member
    Posted 4 years ago #

    I have resolve half problem with:
    <?php if( is_category('categoria-' . get_post_meta($post->ID, "whatiwant", true)) ) echo '';

    but the problem, now, is echo ..

    I cannot display the correct url made of a static url + a query string.

    es.
    <li><a href="http://www.mydomain.com/categoria-<?php echo get_post_meta($post->ID, "whatiwant", true); ?>/">example</a></li>

    how is the correct sintax?

    Thank'you very much, I really appreciate your efforts to help me!

  10. Harrison O.
    Member
    Posted 4 years ago #

    Not sure if I completely understood your need, but if you want to place the code on the category.php and test: “if it is a certain category show this link, else shows nothing” then this code may work for you.

    <?php if (is_category('id or name here')) { ?>
        Category of <?php the_category(' '); ?>
        <?php } else { ?>
        <?php } ?>

    No need to add link tag to the <?php the_category(' '); ?> as it will generate by itself.

    Or maybe for the custom field:

    <?php if (is_category(id or name here)) { ?>
    <?php if( get_post_meta($post->ID, "whatiwant", true) ): ?>
    <?php echo get_post_meta($post->ID, "whatiwant", $single = true); ?>
    <?php endif; ?>
    <?php } else { ?>
    <?php } ?>
  11. andreacutelli
    Member
    Posted 4 years ago #

    uhm.. sorry but the problem is more difficult.

    I have tryed this line:
    <?php if( is_category('photographs-' . get_post_meta($post->ID, "artist", true)) ) echo '<li><a href="' . get_option('home') . '/">photographs</a></li>';
    ..and it works fine!

    but the echo link must be different that home url
    I would display this:

    http://www.domain.com/artist/photographs-of-/

    how I code php?

  12. andreacutelli
    Member
    Posted 4 years ago #

    I have resolved!

    if( is_category('photographs-' . get_post_meta($post->ID, "artist", true)) ) echo('<li><a href="http://www.domain.com/artists/photographs-'.get_post_meta($post->ID,'artists',true).'">'.'
    photographs'.'</a></li>');

    but It works only on archives page.. but not in a simple page..

    anyone knows why?

  13. Harrison O.
    Member
    Posted 4 years ago #

    For regular pages, try if in_category.

    if( in_category('photographs-' . get_post_meta($post->ID, "artist", true)) ) echo('<li><a href="http://www.domain.com/artists/photographs-'.get_post_meta($post->ID,'artists',true).'">'.'
    photographs'.'</a></li>');
  14. suluwa
    Member
    Posted 4 years ago #

    You probably just need to include it to the page.php - you were only referring to archive.php...

Topic Closed

This topic has been closed to new replies.

About this Topic