• gmedia123

    (@gmedia123)


    ok, so I noticed nobody is getting the answers they need, or getting their questions answered, so I have decided to start posting tips and tricks to answer some commonly asked questions.

    Here is the first one.

    How to nest a sub-page on a page. Or how to show a child page on a parent page.

    Copy this to your page.php file right before <php get_sidebar;>

    <?php $pages = get_pages('child_of='.$post->ID.'&amp;sort_column=post_date&amp;sort_order=desc&amp;parent='.$post->ID); $count = 0; foreach($pages as $page) { $content = $page->post_content; if(!$content) continue; if($count >= 2) break; $count++; ?>
    
    <a href="<?php echo get_page_link($page->ID) ?>"><h1 class="title"> <?php echo $page->post_title ?> </h1></a> <?php echo $page->post_content ?>
    <?php
        }
    ?>

    If you find that it pushes your sidebar down paste it between <get_sidebar> and <get_footer> Some themes need it put between the sidebar and footer to reduce overlay.

    GMedia123
    Web Developer
    Tech Support
    Home Theater PC’s w/voice control
    If you found any of this useful or it helped you in any way please consider a donation or at least leave a reply.
    Donate or Pay for Services here

    Tags: categories category Comments database error feed Help installation nextgen-gallery page php plugin plugins post posts RSS sidebar tags theme Themes upgrade wordpress upload sub-pages

Viewing 4 replies - 1 through 4 (of 4 total)
  • byandreasdotcom

    (@byandreasdotcom)

    Thanks for posting this tip!
    What I would like to learn how to do…
    are these 2 things
    1. Create a Page that lists subpages like the screenshot shows.
    The latest added subpage would show up on top, with a full size image and a short text.
    the rest of the subpages would be shown with a thumbnail and a short text

    2. I would like to be able to place 4 random subpages in the bottom of the page.

    Is this possible in wordpress?

    Screenshot: http://tinyurl.com/d5gw2y

    Thread Starter gmedia123

    (@gmedia123)

    Yes it is possible in WordPress.

    Great! Just what I was looking but I’m running into 2 glitches. I wonder if anyone has any suggestions.

    Firstly, it’s ignoring NextGen code since it is just outputting the db data without processing. Ideally, stripping tags would be nice. Suggestions would be brill.

    Secondly, for what I intended this, I wanted a brief summary of the page content, not all of it. I was wondering if I could limit the data to the first paragraph / <!--more--> / word count.

    Obviously I don’t expect the answers to be fed off to me but a point in the right direction may help greatly. Thanks in advanced.

    🙂

    To answer my own question:

    Seems that “Pages” do not have excerpts and don’t look for the <!--more--> tag so I had to do it by word count.

    I found the answer in a couple of places and put it together into 1 function. I edited it to my own means, however check the references for the original code.

    References:

    <?php
    function trim_the_content( $the_contents, $read_more_tag = ‘ READ MORE…’, $perma_link_to = ”, $all_words = 60 ) {

    //remove NextGen BBcode style from post content
    $pattern = ‘|[[\/\!]*?[^\[\]]*?]|si’;
    $replace = ”;
    $the_contents = preg_replace($pattern, $replace, $the_contents);

    // make the list of allowed tags
    $allowed_tags = array( ‘a’, ‘abbr’, ‘b’, ‘blockquote’, ‘b’, ‘cite’, ‘code’, ‘div’, ’em’, ‘fon’, ‘i’, ‘p’, ‘pre’, ‘span’, ‘strong’, ‘title’, ‘ul’, ‘ol’, ‘li’, ‘object’, ’embed’ );
    if( $the_contents != ” ) {
    // process allowed tags
    $allowed_tags = ‘<‘ . implode( ‘><‘, $allowed_tags ) . ‘>’;
    $the_contents = str_replace( ‘]]>’, ‘]]>’, $the_contents );
    $the_contents = strip_tags( $the_contents, $allowed_tags );
    // exclude HTML from counting words
    if( $all_words > count( preg_split( ‘/[\s]+/’, strip_tags( $the_contents ), -1 ) ) ) return $the_contents;
    // count all
    $all_chunks = preg_split( ‘/([\s]+)/’, $the_contents, -1, PREG_SPLIT_DELIM_CAPTURE );
    $the_contents = ”;
    $count_words = 0;
    $enclosed_by_tag = false;
    foreach( $all_chunks as $chunk ) {
    // is tag opened?
    if( 0 < preg_match( ‘/<[^>]*$/s’, $chunk ) ) $enclosed_by_tag = true;
    elseif( 0 < preg_match( ‘/>[^<]*$/s’, $chunk ) ) $enclosed_by_tag = false;
    // get entire word
    if( !$enclosed_by_tag && ” != trim( $chunk ) && substr( $chunk, -1, 1 ) != ‘>’ ) $count_words ++;
    $the_contents .= $chunk;
    if( $count_words >= $all_words && !$enclosed_by_tag ) break;
    }
    // note the class named ‘more-link’. style it on your own
    $the_contents = $the_contents . ‘ – ‘ . $read_more_tag . ‘‘;
    // native WordPress check for unclosed tags
    $the_contents = force_balance_tags( $the_contents );
    }
    return $the_contents;
    }
    ?>

    This will plain text almost all of it, leaving a link at the end to read more. Example of how it works is available at: Neil’s in (africa theme). Thanks GMedia123 for your nested child pages code.

    I hopw this helps!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Fix nest sub-pages on parent page — child/parent’ is closed to new replies.