Simple PHP code question

Hello there,

How might I go about taking all the php lines that echo “Portfolio” into one line of code?

I have tried to make the page IDs (‘10’, ‘524’) or (‘10’ ‘524’) or (‘10, 524’) None of which worked? The code below works, but it’d be nice if they were all combined.

    <body class="<?php if (is_front_page()){
        echo "index";
        }
        elseif (is_page('2')){
        echo "FirmProfile";
        }
        elseif (is_page('9')){
        echo "WhySLA";
        }
		elseif (is_page('10')){
        echo "Portfolio";
        }
		elseif (is_page('524')){
        echo "Portfolio";
        }
		elseif (is_page('533')){
        echo "Portfolio";
        }
		elseif (is_page('518')){
        echo "Portfolio";
        }
		elseif (is_page('520')){
        echo "Portfolio";
        }
		elseif (is_page('522')){
        echo "Portfolio";
        }
		elseif (is_page('528')){
        echo "Portfolio";
        }
		elseif (is_page('526')){
        echo "Portfolio";
        }
		elseif (is_page('530')){
        echo "Portfolio";
        }
        elseif (is_page('12')){
        echo "HumanFactors";
        }
        elseif (is_page('14')){
        echo "Contact";
        }
		elseif (is_page('129')){
        echo "Links";
        }
		elseif (is_page('235')){
        echo "News";
        }
		elseif (is_page('312')){
        echo "CaseStudies";
        }
		elseif (in_category('3')){
        echo "CaseStudies";
        }
		elseif (in_category('4')){
        echo "News";
        }
		else {
        echo "default";
        }?>" >

is_page(array(10,524,533,...));

should work

You could use a switch statement and do something like this



switch($somepage)
{
         case 524:
         case 533:
             echo 'portfolio';
         break:
     
         case 2:
              echo 'something'
         break;

}



and so on

Thanks for the reply.

However, I tried to do what dan has suggested, and my site won’t load because of a syntax error.

		elseif (is_page(array(10,524,533,518,520,522,528,526,530)){
        echo "Portfolio";
        }
        elseif (is_page('12')){
        echo "HumanFactors";
        }

Thanks,
Lorne

you’re missing a closing parenthesis:

elseif (is_page(array(10,524,533,518,520,522,528,526,530)){...} 

should be

elseif (is_page(array(10,524,533,518,520,522,528,526,530))){...}

Also I don’t know if your page Id’s have to strings as you have in the other if statements.

Oops, oh man that’s embarrasing, it worked. Thanks for the help and quick response.

Working code:

		elseif (is_page(array(10,524,533,518,520,522,528,526,530))){
        echo "Portfolio";
        }

No problem, everyones been tormented by parentheses at some point :stuck_out_tongue:

Your first port of call should always be the documentation which (briefly) describes the use of the is_page() function and shows that you can use an array to check for multiple pages.