WordPress.org

Ready to get started?Download WordPress

Forums

Different Styles for different pages BUT without using is_page array (5 posts)

  1. highvoltag3
    Member
    Posted 5 years ago #

    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.

  2. Michael Fields
    Themer
    Posted 5 years ago #

    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+

  3. highvoltag3
    Member
    Posted 5 years ago #

    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.

  4. Michael Fields
    Themer
    Posted 5 years ago #

    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.

  5. highvoltag3
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic