WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to say “if category exists..

[Resolved] How to say “if category exists..

  • 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

Viewing 13 replies - 1 through 13 (of 13 total)
  • esmi

    @esmi

    Forum Moderator

    <?php if( is_category('3, 14, 55, 11') ) echo 'Hello';
    elseif( is_category('1, 10, 22') ) echo 'Goodbye';?>

    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..!

    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>

    esmi

    @esmi

    Forum Moderator

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

    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

    esmi

    @esmi

    Forum Moderator

    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;?>

    thanks 4 reply but not works.. 🙁

    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!

    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 } ?>

    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-<?php echo get_post_meta($post->ID, "artist", true); ?>/

    how I code php?

    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?

    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>');

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

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘[Resolved] How to say “if category exists..’ is closed to new replies.
Skip to toolbar