WordPress.org

Ready to get started?Download WordPress

Forums

Display Page Excerpt Before WordPress Loop (5 posts)

  1. likemindsdesigns
    Member
    Posted 1 year ago #

    Hello Fellow WordPress Lovers

    I want to use one of my pages as an introduction before my WordPress Loop on the index.php home page. (My settings have the blog feed as my home page).

    I put this above my Loop:

    $page_id = 95;
    $page_data = get_page( $page_id );
    echo apply_filters('the_content', $page_data->post_content);

    It works great, but it shows the entire article. Is there a way to show only the content of that page above the "read more" tag, along with a "read more" link?

    By the way I tried this with no luck:

    echo apply_filters('the_excerpt', $page_data->post_excerpt);

    I also tried this with no luck:

    $page_id = 95; // substitute to your page id
    $my_page = get_page($page_id);
    echo "<p>$my_page->post_excerpt</p>";
    echo '<a href="'.get_permalink( $page_id ).'">read more</a>';

    Is this a situation for a multiple loop? Those confuse me so I would love to get some help from an experienced WP user. Thank you!

  2. hoosoft
    Member
    Posted 1 year ago #

    $temp = $wp_query;
    $wp_query = NULL;
    $page_id = 95; // substitute to your page id
    $my_page = get_page($page_id);
    echo "<p>$my_page->post_excerpt</p>";
    echo '<a href="'.get_permalink( $page_id ).'">read more</a>';
    wp_reset_query();
    $wp_query = NULL;
    $wp_query = $temp ;
  3. likemindsdesigns
    Member
    Posted 1 year ago #

    @hoosoft

    Thank you for your effort. Even with your changes, all I am getting back is the "read more" and no excerpt. Do Pages not allow excerpts (only posts)?

  4. likemindsdesigns
    Member
    Posted 1 year ago #

    This is my home page (set to a blog feed):

    http://quigleycreativegroup.com/mcra/

    I am using this code before my loop:

    $page_id = 95;
    	$page_data = get_page( $page_id );
    	echo apply_filters('the_content', $page_data->post_content);

    There is a <!--more--> tag after the first paragraph. How do I get that long page to cut off at that tag?

  5. likemindsdesigns
    Member
    Posted 1 year ago #

    Any body who can help me, I am still having trouble with this. Here's what I have so far:

    <?php // set the $paged variable
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    ?>
    
    <?php if (is_front_page() && !is_paged()) : //If we are on first page
    
    // The 1st Query (Loop 1)
    $query1 = new WP_Query('page_id=95' );  //Show only Post ID 472
    
    // The 1st Loop
    while ($query1->have_posts()) : $query1->the_post();?>  
    
    <?php the_content('Read More'); //Show home page content up until read more tag ?>
    
    <hr />
    
    <?php endwhile ?>
    <?php wp_reset_postdata(); // reset post data ?>
    <?php endif?>
    
    <?php
    
    //The 2nd Query (Loop 2)
    $query2 = new WP_Query(array(
    'cat' => '2',//Show only the posts in Category 2 or "MCRA News"
    'paged' => $paged // use pagination
    ));
    
    // The 2nd Loop
    if($query2->have_posts()) : while($query2->have_posts()) : $query2->the_post(); ?>  
    
    <?php the_content('Read More'); ?>
    
    <?php endwhile; ?>
    
    <?php next_posts_link('Older Posts') ?>
    <?php previous_posts_link('Newer Posts') ?>
    
    <?php else : //There are no more posts in this category?>
    //I DO NOT WANT THIS HERE, BUT IT IS DISPLAYING A BLANK PAGE WHEN I GET TO THE LAST PAGE.
    <p>No more news to display.</p>
    
    <?php endif; ?>
    <?php wp_reset_postdata(); // reset post data ?>

    WHAT WORKS:
    • I am displaying the home page content up to the Read More tag as I intended
    • The 2nd query shows posts from the category I wanted

    WHAT I NEED HELP WITH:
    • Pagination: for the 2nd query, it still shows a "Get Previous Posts" link even when there are NO previous posts to show. So it displays a blank page. I worked around this with a lame "No more posts to show message" but I would rather just have it work correctly.

    Here's my page:
    http://quigleycreativegroup.com/mcra/

    Any takers? Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic