• I have a block of code that works fine, but my feeling is that it should be something that PHP can accomplish in just a few lines of code, probably using an array somehow. I’ve been all over trying to find an answer, but if any of the PHP wizards here can help, that would be great.

    I am checking the page title for certain strings created by NextGen Gallery and serving up an addendum to the post title based on which string is present. Here is my working code:

    <?php
    // Create names for each portfolio page and display them after the portfolio title
    	$mystring = wp_title('', false);
    	$posweb = strpos($mystring, 'Gallery 9');
    	$posar = strpos($mystring, 'Gallery 10');
    	$posadv = strpos($mystring, 'Gallery 11');
    	$pospack = strpos($mystring, 'Gallery 12');
    	$poslogo = strpos($mystring,'Gallery 13');
    	$postrade = strpos($mystring, 'Gallery 14');
    	$posnews = strpos($mystring, 'Gallery 15');
    	$posbroch = strpos($mystring, 'Gallery 16');
    
    	if ($posweb == true) {
    		echo ' &raquo; Web Sites';
    	}
    	if ($posar == true) {
    		echo ' &raquo; Annual Reports';
    	}
    	if ($posadv == true) {
    		echo ' &raquo; Advertising';
    	}
    	if ($pospack == true) {
    		echo ' &raquo; Packaging';
    	}
    	if ($poslogo == true) {
    		echo ' &raquo; Logos & Identity';
    	}
    	if ($postrade == true) {
    		echo ' &raquo; Trade Shows';
    	}
    	if ($posnews == true) {
    		echo ' &raquo; Newsletters';
    	}
    	if ($posbroch == true) {
    		echo ' &raquo; Brochures';
    	}
    // end create names for each portfolio page...
    ?>

    I’d like to use this technique of checking for NextGen Gallery numbers in other areas of the site, but first I want to see how much simpler and shorter it can be.

    Thanks for any and all advice.

    cvc

Viewing 1 replies (of 1 total)
  • Here is one way to do it. This code assumes there is only one exact match possible each time you run this routine. If that is untrue this code will not do what you want.

    I only put a few choices in. You can follow along and add the rest.

    You probably want a default condition at the end in case all the tests are false. If not delete that part

    if ( strpos($mystring, 'Gallery 9') !== false ) {
       echo ' &raquo; Web Sites';
    } elseif ( strpos($mystring, 'Gallery 10') !== false ) {
       echo ' &raquo; Annual Reports';
    } elseif ( strpos($mystring, 'Gallery 11') !== false ) {
       echo ' &raquo; Advertising';
    } else {
       ... default condition here
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Simplify conditional code using an array??’ is closed to new replies.