• Hi there,

    I am looking to have several excerpts of pages displayed on the homepage as a grid/small boxes, rather than “blog posts”.

    The current code i have is below. I was wondering how i make it display a “read more” link and just a short description as it just display all the content on that page. Also, how to show the featured image as a thumbnail?

    You can see what is happening here: https://s11.postimg.org/6994o9orn/posts.jpg

    <?php $args = array(
    	'sort_order' => 'ASC',
    	'sort_column' => 'post_title',
    	'hierarchical' => 1,
    	'exclude' => '',
    	'include' => '',
    	'meta_key' => '',
    	'meta_value' => '',
    	'authors' => '',
    	'child_of' => 0,
    	'parent' => -1,
    	'exclude_tree' => '',
    	'number' => '',
    	'offset' => 0,
    	'post_type' => 'page',
    	'post_status' => 'publish'
    );
    $pages = get_pages($args);
    foreach ( $pages as $page ) { $content = $page->post_content; ?>
    
    	<div onclick="location.href='<?php echo get_page_link( $page->ID ); ?>';" style="cursor: pointer;" class="homebox orange<?= rand(1,3) ?>" id="thickbox post-<?php the_ID(); ?>">
            <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
    		<div class="entry"><?php echo $content; ?></div>
        </div>
    
    <?php } ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You can use the_excerpt() if you use the function setup_postdata(). Here is a simplified example:

    <?php
    $pages = get_pages();
    if($pages) {
      // don't change $post or setup_postdata() doesn't work
      foreach ( $pages as $post ) {
        setup_postdata($post);
        the_excerpt();
      }
    }
    ?>
    <?php wp_reset_postdata(); ?>

    You can add a read more link to the excerpt with this:
    https://codex.www.ads-software.com/Customizing_the_Read_More#Displaying_a_.22more.E2.80.A6.22_link_when_using_the_the_excerpt.28.29

    Thread Starter reyna12

    (@reyna12)

    Thanks for your quick response!, without adding the read more link yet, i have managed what you suggested, although it seems to add the excerpt twice, once outwith the div container?

    As you can see here: https://postimg.org/image/ep78xvarb/

    $pages = get_pages($args);
    if($pages) {
    foreach ( $pages as $post ) {
        setup_postdata($post);
        the_excerpt(); ?>
    
    	<div onclick="location.href='<?php echo get_page_link( $page->ID ); ?>';" style="cursor: pointer;" class="homebox orange<?= rand(1,3) ?>" id="thickbox post-<?php the_ID(); ?>">
            <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
    		<div class="entry"><?php the_excerpt() ?></div>
        </div>
    
    <?php } } ?>
    <?php wp_reset_postdata(); ?>
    Thread Starter reyna12

    (@reyna12)

    infact i have changed the code to this and it seems to work fine!

    $pages = get_pages($args);
    if($pages) {
    foreach ( $pages as $post ) {
        setup_postdata($post);
    	global $more;
    	$more = 0;
    ?>
    
    	<div onclick="location.href='<?php echo get_page_link( $page->ID ); ?>';" style="cursor: pointer;" class="homebox orange<?= rand(1,3) ?>" id="post-<?php the_ID(); ?>">
            <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
    		<div class="entry"><?php the_content('Continue Reading'); ?></div>
        </div>
    
    <?php } } ?>
    <?php wp_reset_postdata(); ?>
    Thread Starter reyna12

    (@reyna12)

    Thanks for your help!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you’ve got it resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘page excerpts instead of post excerpts on homepage?’ is closed to new replies.