0

Please forgive me if my verbiage is incorrect!

I have a working function that outputs a font-awesome icon based on a post's category. I'm looking to expand the function so I can also specify the size within my call, based on an array within the function.

Here's the working code

<?php echo category_icon(); ?> -- html output is <i class="fa fa-desktop"></i>)  

And I'm looking to achieve the following...

<?php echo category_icon($icon_size); ?>  --output would be <i class="fa fa-desktop$icon_size"></i>)  

And here is my current function

function category_icon($icon_size){
    $build_icon_cat = get_the_category();
    $choose_icon = $build_icon_cat[0]->cat_ID;

            // chooses icon by category
            if ($choose_icon == 19) $build_icon_dos = 'fa fa-desktop';
             elseif ($choose_icon == 15) $build_icon_dos = '<i class="fa fa-cog';

    //-------size array
$icon_size = array(
    '' => '',
    '1' => ' fa-lg',
    '2' => ' fa-2x',
    '3' => ' fa-3x',
    '4' => ' fa-4x',
    '5' => ' fa-5x',
  );

//variables for building
        $build_icon_uno = '<i class="';
        $build_icon_cuatro = '"></i>';
        $display_category_icon = $build_icon_uno . $build_icon_dos . $icon_size . $build_icon_cuatro;

return $display_category_icon;
3
  • 1
    You have $icon_size as an argument, and then you're redeclaring it as an array. Change the name of the array to something like $icon_array, and then you can retrieve a size like so: $icon_array[$icon_size], where $icon_size is simply a matching index (ie: '', '1', '2'...) Commented Nov 1, 2014 at 20:21
  • After days of searching it works perfectly.. thank you! Commented Nov 1, 2014 at 20:30
  • Posting as an answer, then. Please accept when you're able. Commented Nov 1, 2014 at 20:31

2 Answers 2

1

You have $icon_size set as an argument, and then you're redeclaring it as an array. Change the name of the array to something like $icon_array, and then you can retrieve a size like so: $icon_array[$icon_size], where $icon_size is simply a matching index (ie: '', '1', '2'...).

You may also want to consider implementing some basic validation using isset, as well as some default sizing.

Sign up to request clarification or add additional context in comments.

1 Comment

Created this function example for general learning so it's dumbed down as much as possible. I guess next on the agenda is learning isset. Thanks again
0

Thanks to maiorano84 for providing the insight. Here is the working code.

Callback on index.php

<?php echo category_icon('5'); ?>  

Working function

function category_icon($icon_size){
    $build_icon_cat = get_the_category();
    $choose_icon = $build_icon_cat[0]->cat_ID;

            // chooses icon by category
            if ($choose_icon == 19) $build_icon_dos = 'fa fa-desktop';
             elseif ($choose_icon == 15) $build_icon_dos = '<i class="fa fa-cog';

    //-------size array
$icon_array = array(
    '' => '',
    '1' => ' fa-lg',
    '2' => ' fa-2x',
    '3' => ' fa-3x',
    '4' => ' fa-4x',
    '5' => ' fa-5x',
  );

//variables for building
        $build_icon_uno = '<i class="';
        $build_icon_cuatro = '"></i>';
        $display_category_icon = $build_icon_uno . $build_icon_dos . $icon_array[$icon_size] . $build_icon_cuatro;

return $display_category_icon;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.