• Resolved Rick Alday

    (@mrdaro)


    I have a form which asks the user for one or more category ID’s separated by comma. Thus the user will enter 1,2,3 without quotes. The value is stored in a variable called $usercategories
    If I use
    <?php wp_list_categories('include='.$usercategories.'&title_li=' ); ?>
    It works fine, I’ll get a list of only those categories. However when I try to use the variable in my single.php

    if (in_category (array($usercategories) ) { ... }
    it does not recognize the variable, I try to manually enter the value just to check and it does work.

    if (in_category (array(1,2,3) ) { ... }

    if I echo the variable it outputs the list too eg 1,2,3
    I just don’t understand why the variable it’s not working when used as a parameter for a function. Any ideas?

Viewing 5 replies - 1 through 5 (of 5 total)
  • if (in_category (array($usercategories) )

    From what you describe, $usercategories isn’t an array. It’s a string.

    EDIT: As confirmed above.. ??

    if (in_category (array($usercategories) ) { ... }

    All you’re doing there is placing a string into an array (array('1,2,3')), so the array has one item, not several, like in this case..

    if (in_category (array(1,2,3) ) { ... }

    Just because the string contains commas doesn’t mean it’ll convert into array items when placed inside array() ..

    For splitting up a delimeter seperated string, use explode() ..
    https://php.net/manual/en/function.explode.php

    @t31os_: You need to type faster. ??

    But just to make this post serve some serious purpose..

    $cat_array = explode( ',', $usercategories );
    if (in_category (array( $cat_array ) ) [...]

    might just do it.

    Thread Starter Rick Alday

    (@mrdaro)

    @t31los_ I see what you’re saying.

    @esmi I had tried that route before but it’s still not working.
    Here’s my template code

    <?php get_header(); ?>
    <?php
    
    $cat_array = explode (',',$usercategories);
    if (in_category(array($cat_array)) ) {
    include(TEMPLATEPATH."/portfolio_single.php");
    } else {
    ?>
    
    <div id="main"><!-- Begin Main -->
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="pagetitle"><h1><?php the_title(); ?></h1></div>
    <div class="entry">
    <div class="entry_content">
    <?php echo $usercategories;?>
    
    <?php the_content(); ?>
    <div class="entry commententry">
    <?php comments_template(); ?>
    </div>
    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>
    </div><!-- END Entry Content -->
    </div><!-- END Entry -->
    
    </div><!-- End Main -->
    <div class="clear">&nbsp;</div>
    <?php get_footer(); }?>
    Thread Starter Rick Alday

    (@mrdaro)

    got it

    <blockquote>$cat_array = explode (',',$usercategories);
    if (in_category($cat_array) ) {
    </blockquote>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘User inputted text as variable not working all the time’ is closed to new replies.