Call function in array php

Complete newbie to programming 🙂

i am fiddling around with a WordPress theme and hit a roadblock. If i put this in archive.php,

Read More
<?php 

if (is_category(array( 9, 13, 24,)) {
 echo 'Light is GREEN';
  }else {
 echo 'Light is RED}';
 ?>

That part works fine, when i go to category 9, 13, 24 i get “light is Green” and “light is red” for all other categories.

Now i have this function which gets the id’s of categories from theme options where i need to echo “light is green”

function am_get_cat_layout_ids()
{

                global $am_option;
                $catnumend = $am_option['main']['category_onecolumn_hidden']-1;
                for($i=0; $i<$catnumend; $i++)
                {

                    $value = $am_option['main']['category_onecolumn_'.$i].',';


                }

                return $value;
}

if i echo $value instead of return, i get the category id’s say 2, 15, 7, 34, fine but if i do this :

  <?php 

if (is_category(array(am_get_cat_layout_ids())) {
 echo 'Light is GREEN';
  }else {
 echo 'Light is RED}';
 ?>

its not working.

anything that can be done to make it work?

Thank you ,
Srik

Related posts

Leave a Reply

3 comments

  1. Try this:

    function am_get_cat_layout_ids()
    {
        global $am_option;
        $ret = array();
        $catnumend = $am_option['main']['category_onecolumn_hidden']-1;
    
        for($i=0; $i<$catnumend; $i++)
        {
                $ret[] = $am_option['main']['category_onecolumn_'.$i];
        }
    
        return $ret;
    }
    

    and

    if (is_category(am_get_cat_layout_ids()) {
        echo 'Light is GREEN';
    }else {
        echo 'Light is RED}';
    }
    
  2. If your am_get_cat_layout_ids() function returns 2, 15, 7, 34, then you should do:

    if (is_category(explode(', ',trim(am_get_cat_layout_ids(),','))) {
     echo 'Light is GREEN';
      }else {
     echo 'Light is RED}';
    
  3. What you have made is an array containing one string, which has comma-separated numbers. You want to build an array of numbers.

    function am_get_cat_layout_ids()
    {
        global $am_option;
    
        $ids = array(); // create array
        $catnumend = $am_option['main']['category_onecolumn_hidden']-1;
    
        for($i=0; $i<$catnumend; $i++)
        {
            $ids[] = $am_option['main']['category_onecolumn_'.$i]; // add value to array
        }
        return $ids; // return array
    }
    

    Your original code was the equivalent to this:

    if (is_category(array( "9, 13, 24,")) {
    

    Whereas this new code is the equivalent to the code at the beginning of the question:

    if (is_category(am_get_cat_layout_ids()) {