WordPress.org

Ready to get started?Download WordPress

Forums

redirect parent page to first child page (6 posts)

  1. Phartog
    Member
    Posted 5 years ago #

    I found on another thread how to redirect a parent page to a first child page. I used the code, but it gives me some error.

    Parse error: parse error in C:\xampplite\htdocs\wordpress\wp-content\themes\AID INDIA website\page.php on line 41

    I am not a php programmer, so please help. This is the code in my page.php:

    <div id="container">
    
    		<?php
    		/*
    		Template Name: Redirect To First Child
    		*/
    		if (have_posts()) {
      			while (have_posts()) {
        				the_post();
        				$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    				if ($pagekids) {
    				$firstchild = $pagekids[0];
    				wp_redirect(get_permalink($firstchild->ID));
    					}
    				}
    			}
    		?>
    
    			<div class="post">
    
    				<h2><?php the_title();?></h2>
    
    				<div class="entry">	
    
    					<?php the_content(); ?>
    
    					<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
    
    					<?php edit_post_link('Edit','<p>','</p>'); ?>
    
    				</div>
    
    			</div>
    
    		<?php endwhile;?>
    
    		<?php else: ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    
    				<h2><?php_e('Not Found'); ?></h2>
    			</div>		
    
    		<?php endif;?>
    	</div>
    
    	<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
    
    </div>
  2. Phartog
    Member
    Posted 5 years ago #

    nobody knows the answer???

  3. Phartog
    Member
    Posted 5 years ago #

    really nobody, I really need help with this!!!

  4. johnpax
    Member
    Posted 5 years ago #

    Hi, try this...

    <div id="container">

    <?php
    /*
    Template Name: Redirect To First Child
    */
    if (have_posts()) : while (have_posts()) : the_post();

    $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    if ($pagekids) {
    $firstchild = $pagekids[0];
    wp_redirect(get_permalink($firstchild->ID));
    }

    ?>

    <div class="post">

    <h2><?php the_title();?></h2>

    <div class="entry">

    <?php the_content(); ?>

    <?php link_pages('<p>Pages: ', '</p>', 'number'); ?>

    <?php edit_post_link('Edit','<p>','</p>'); ?>

    </div>

    </div>

    <?php endwhile;?>

    <?php else: ?>

    <div class="post" id="post-<?php the_ID(); ?>">

    <h2><?php_e('Not Found'); ?></h2>
    </div>

    <?php endif;?>
    </div>

    <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>

    </div>

    hope it will solve you problem.

  5. gregvanbrug
    Member
    Posted 5 years ago #

    I had the same problem. Try this plug-in:
    http://wordpress.org/extend/plugins/redirector/

    You'll have to specify which pages redirect but should fix your issue. If it doesn't automatically redirect to the child page you want, change the order on the page you want to redirect to to 1.

    Hope it helps.

  6. jcdesign
    Member
    Posted 5 years ago #

    I had the same problem, and the code above gave me a great start.

    The problem is that you're including output code prior to trying to redirect. Seems to be like php header(), which requires that nothing is actually output to the page before being called.

    So I stripped down to basics, and this seemed to fit the bill:

    <?php
    /*
    Template Name: Go to first child
    */
    	$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    	if ($pagekids) {
    		$firstchild = $pagekids[0];
    		wp_redirect(get_permalink($firstchild->ID));
    	} else {
    		// Do whatever templating you want as a fall-back.
    	}
    ?>

    This means you can simply set a page to having this page template, and it will spit you to the first child page found.

    Didn't check out the redirector plugin - that looks like it may do slightly heavier lifting.

    -Jeremy

Topic Closed

This topic has been closed to new replies.

About this Topic