• orangjul

    (@orangjul)


    Hi,
    I am not sure how to properly ask this, aka what terms to use, but hopefully someone can steer me in the right direction.

    I want to write something into my functions.php which basically allows me to abbreviate a very long if statement and call it from other files within my theme:

    <?php $ancestors = get_post_ancestors($post);
    if((is_page(array('2641','2654','2656','2658','2660','2662','2664'))) || (in_array(2641,$ancestors)) || (in_array(2654,$ancestors)) || (in_array(2656,$ancestors)) || (in_array(2658,$ancestors)) || (in_array(2660,$ancestors)) || (in_array(2662,$ancestors)) || (in_array(2664,$ancestors))) { ?>do something<? } ?>

    I was thinking that when I call it from other files in my theme, the code would look like this:

    <?php if(is_concierge_page()) { ?>
    do something
    <? } ?>

    Does that make sense? Can someone help me write this?

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter orangjul

    (@orangjul)

    Well I couldn’t figure it out myself, but I was able to convince one of my programmers to help me. Here’s the function…hope it’s useful to someone:

    function isConciergePage()
    {
    	global $post;
    	$ancestors = get_post_ancestors($post);
    	//print_r($ancestors);
    	$result = false;
    
    	if( 	(is_page(array('2641','2654','2656','2658','2660','2662','2664'))) ||
    		(in_array(2641,$ancestors)) ||
    		(in_array(2654,$ancestors)) ||
    		(in_array(2656,$ancestors)) ||
    		(in_array(2658,$ancestors)) ||
    		(in_array(2660,$ancestors)) ||
    		(in_array(2662,$ancestors)) ||
    		(in_array(2664,$ancestors))	)
    	{
    		$result = true;
    	}
    	return $result;
    }

    And then to call it from another file:

    ‘if(isConciergePage())
    {
    echo “<!– do something –>\n”;
    }’

Viewing 1 replies (of 1 total)
  • The topic ‘help writing a function or filter to shorten a long if statement’ is closed to new replies.