Pages & Subpages – Is Parent?

wp_subpage
WordPress allows to create sub-pages in your backend. This is why many uses WordPress as a CMS. Sometimes you like with the help of the Conditional Tags to start several queries with a certain respond – but then you see frequently in the template static queries on any ID of a page.
But it's possible to handle this differently, and simpler when it comes to pages or sub-pages of this page.

With this solution you can, for example, in dependence of a page, load a specific layout. This small request I'll take as an example and show a function that gives queries to pages / sub-pages and I will show an example in action.

Background

If you create in WordPress a new page, then you can define it in the attributes as a subpage of an existing page. This can not only create tree views, but also structures which are not uncommonly needed in a CMS.

wp_subpage_new

If you want control the output depending on the page, then you use the Conditional Tag is_page(), which you use to implement the requirement. For not using the tag for every page and use the template constantly, you can use a simple function, which queries the main page and returns a true back (true) when there is a subpage of the main page.

Solution

This function, the following syntax is used in the functions.php of the theme.

// check ID post and child-post
function has_parent( $post, $post_id ) {
	if ($post->ID == $post_id)
		return true;
	elseif ($post->post_parent == 0)
		return false;
	else
		return has_parent( get_post($post->post_parent), $post_id );
}

This little function checks whether the output page is a page or subpage of a page with the passed ID; sounds complicated, but it really isn't. This feature can be, after put in the theme, be used in all the templates now. The following example checks whether it is a page or subpage of the ID 1, and if so, then the stylesheet Alternative 1 (alternative_1.css) will be output.

Otherwise will be ID 2, or if it doesn't fit to none of the queries, then the default stylesheet will be used.

To have the ability to expand a little, I set in the query variable $bodyclass that I output in the body-tag. Thus I have an additional point and depending on the page to realize an according design.

    <?php
    $bodyclass = '';
    if ( function_exists('has_parent') ) {
	    if ( has_parent($wp_query->post, 1) ) {
	        echo '<link rel="stylesheet" href="' . get_bloginfo('stylesheet_directory') . '/alternative_1.css" type="text/css" media="screen" />' . "\n";
	        $bodyclass = 'class="alternative_1"';
	    } elseif ( has_parent($wp_query->post, 2) ) { 
	        echo '<link rel="stylesheet" href="' . get_bloginfo('stylesheet_directory') . '/alternative_2.css" type="text/css" media="screen" />' . "\n";
	        $bodyclass = 'class="alternative_2"';
	    } else {
	    	echo '<link rel="stylesheet" href="' . get_bloginfo('stylesheet_directory') . '/default.css" type="text/css" media="screen" />' . "\n";
	    }
  	} ?>

</head>
<body<?php echo ' ' . $bodyclass; ?>>

Conclusion

I think that this small possibility can have a large effect in the output and can have various possibilities. Also this solution can be realized with the change of a theme, which I mentioned in the article „Change your WordPress Theme on Dependency“.

Thank you for reading and even more I thank you for contributing any improvements in the comments section.

Comments are closed.

4 comments

  1. Peter

    I've often had to do this when I put together a theme for a client who wants a CMS style site, but I've never come up with anything as elegant of the has_parent function!

    I was about to comment on the fact that you were missing situations where the child was actually a grandchild - but it turns out it was just pretty enough that I missed it.

    I'd never seen a really solid practical example of recursion until now. Brilliant.

  2. Peter Kahoun

    Very nice function. IMHO, why not to eliminate one parameter by mod. like this:


    function has_parent($post_id, $post = false) {
    if ($post === false) global $post;

  3. Peter

    I just had to put together a function to find the topmost parent of a page - using your function as a template, here's what I came up with:

    function get_topmost_parent($post_id){
    $parent_id = get_post($post_id)->post_parent;
    if($parent_id == 0){
    return $post_id;
    }else{
    return get_topmost_parent($parent_id);
    }
    }

One pingback

  1. HealthMatters | Round WordPress: Summer of Code, Kubrick Retired, How Tos, WebWare 100, and WordPress Fan Blogs