WordPress.org

Ready to get started?Download WordPress

Forums

Add a 'Read More' link to the get_pages loop (14 posts)

  1. glenngeiger
    Member
    Posted 4 years ago #

    the Codex offers this great bit of code to loop through the child pages of the parent and show the page title and page contents:

    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    	$count = 0;
    	foreach($pages as $page)
    	{
    		$content = $page->post_content;
    		if(!$content)
    			continue;
    		$content = apply_filters('the_content', $content);
    	?>
    		<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    		<div class="entry"><?php echo $content ?></div>
    	<?php
    	}
    ?>

    I can't figure out how to add a 'Read More...' link to the_content, as the code above is adding the_content to the apply_filters function, and echoing out $content. Any ideas?

  2. glenngeiger
    Member
    Posted 4 years ago #

    Any chance this one slipped through the cracks?

  3. charlesque
    Member
    Posted 4 years ago #

    Hi

    I have the same problem. I will be very grateful if someone can answer that.

    Thanks.

  4. charlesque
    Member
    Posted 4 years ago #

    Ok I finally found the answer so I post here :

    <?php query_posts(array( 'post_type' => 'page',  'post_parent' => '2', 'orderby' => 'menu_order', 'order' => 'ASC' )); ?>
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Lien permanent vers <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    				<div class="entry">
    					<?php the_content('Lire la suite &raquo;'); ?>
    				</div>
    			</div>
    
    		<?php endwhile; ?>
    
    	<?php else : ?>
    
    		<h2 class="center">Introuvable</h2>
    		<p class="center">Désolé, mais vous cherchez quelque chose qui ne se trouve pas ici.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
  5. josef.lagunas
    Member
    Posted 3 years ago #

    looks like you forgot one thing. the global $more.

    <? global $more; ?>
    <? $more = 0; ?>
  6. gxlrygt
    Member
    Posted 3 years ago #

    Thanks both of you man!

    This is my final code after your help:

    <?php
    $i = 0;
    $count = 3;
    global $more;
    $more = 0;
    $parent_page_id = 1092;
    query_posts(array( 'post_type' => 'page',  'post_parent' => $parent_page_id, 'orderby' => 'menu_order', 'order' => 'DESC' ));
    if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    	<div class="grid_4 <?php if($i % $count == 0) echo 'alpha';else if(($i+1) % $count == 0) echo 'omega';?>">
    		<?php
    		if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())){ ?>
    		<div class="post-img">
    			<a title="<?php get_the_title(); ?>" href="<?php the_permalink(); ?>" rel="bookmark" ><?php the_post_thumbnail('service-img');?></a>
    		</div>
    		<?php } ?>
    		<h3 class="post-title"><?php the_title(); ?></h3>
    		<div class="post-content"><?php the_content('Read more &rarr;'); ?></div>
    	</div>
    	<?php
    	$i++;
    	endwhile;
    endif;
    ?>
  7. deathski
    Member
    Posted 3 years ago #

    Is the code above limited to display the subpages/children of the indicated parent_page_id (1092)?

    I wish to display all my current Parent pages.

  8. gxlrygt
    Member
    Posted 3 years ago #

    This code just displays subpages deathski

  9. deathski
    Member
    Posted 3 years ago #

    I see. How can I modify this query to get all the parent pages instead of posts? Newbie here.

    query_posts($query_string . "&showposts=$post_number&paged=$paged&cat=$cat");

  10. gxlrygt
    Member
    Posted 3 years ago #

    query_posts($query_string . "&post_parent=0&showposts=$post_number&paged=$paged&cat=$cat");

  11. deathski
    Member
    Posted 3 years ago #

    Thanks but it still returns my posts and not pages.

  12. gxlrygt
    Member
    Posted 3 years ago #

    did you try to add 'post_type' => 'page' ?

    query_posts($query_string . "&post_parent=0&post_type=page&showposts=$post_number&paged=$paged&cat=$cat");

    or

    query_posts(array( 'post_type' => 'page', 'post_parent' => 0, 'orderby' => 'menu_order', 'order' => 'DESC' ));

  13. deathski
    Member
    Posted 3 years ago #

    Thanks man. post_type did it. Thank you

  14. 123milliseconds
    Member
    Posted 3 years ago #

    Please mark thread as resolved so that

    - others with similar problem can see it as resolved and will read this thread for help if they have similar problem

    - people providing help see it as resolved and will not waste time reading this post.

Topic Closed

This topic has been closed to new replies.

About this Topic