• Is there anyway I can modify the is_page code so it can detect the parent of sub-pages.

    To clarify that here’s an example this is what I got but I trimmed the CCS code to only one element to make things short.

    /********************** START ************************/
    if ( is_page(array('avdg','extension','pasantias','coordinaciones_avdg','coordinacion-tega','coordinacion-servicio-comunitario','depart_avdg','dpto-artes-visuales','dpto-diseno-grafico','dpto-teoria-e-historia')) ) {
    echo "<style type=\"text/css\">
    #sidebar {
    	border-left: ".$color_avdg." solid 1pt;
    }
    </style>";
    } 
    
    elseif ( is_page(array('artes-escenicas','depart_art_esce','dpto-de-actuacion')) ) {
    echo "<style type=\"text/css\">
    #sidebar {
    	border-left: ".$color_art_esce." solid 1pt;
    }
    </style>";
    }
    /*----------------END-------------------------*/

    So this works but the array is quite big besides that any time a page is added the array would have to be modified to add the new page is there anyway I can just define something like is_page(parent_page) so any child of parent page would be included in the if statement with out modifying the code?

    Thanks in advanced.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You could try this function (add it to your theme’s functions.php file):

    function mf_is_child_of( $page ){
    	global $post;
    	$a = $post->ancestors;
    
    	if ( count( $a ) > 0 ){
    		if ( in_array( $page, $a ) )
    			return true;
    	}
    	else
    		return false;
    }

    Then you could modify your code to something like:

    if ( mf_is_child_of( 123 ) ) {
    echo "<style type=\"text/css\">
    #sidebar {
    	border-left: ".$color_avdg." solid 1pt;
    }
    </style>";
    } 
    
    elseif ( mf_is_child_of( 67 ) ) {
    echo "<style type=\"text/css\">
    #sidebar {
    	border-left: ".$color_art_esce." solid 1pt;
    }
    </style>";
    }

    note: you will need to change the numeric values inside of mf_is_child_of() to the page id’s of your parent pages.

    This solution should work in WordPress 2.5+

    Thread Starter highvoltag3

    (@highvoltag3)

    It didn’t work ?? but having to change the code to add the parent ID still kind of makes the whole idea the same, even though would make things a bit easier.

    highvoltage, This seems to be exactly what you asked for, you will only ever need to add one value to the mf_is_child_of() function – the value of the “root parent”. So, if you had these pages:

    Food
    -Mexican
    –taco
    –burrito
    –salsa

    And you added:

    -Italian

    as a child of food, your styles should be applied without modification to your template.

    Thread Starter highvoltag3

    (@highvoltag3)

    Well yeah, I mean it is what I asked for but still needs to be coded manually if you add a new parent with child’s it a big step forward though I appreciate your help, It helps out a lot.

    There’s one thing tho… it’s not working for me I tried it and the style don’t apply. Witch mean the function is not working.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Different Styles for different pages BUT without using is_page array’ is closed to new replies.