WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Auto Excerpt first post only (11 posts)

  1. crisw
    Member
    Posted 1 year ago #

    I'm hoping someone out there might be know of a way to auto-excerpt posts, except the latest (top) post on the page.

    Basically, we want the first post a reader sees to be full text, and everything below that is automatically excerpted.

    Any help would be appreciated!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    normal loop -
    principle:

    if( $wp_query->current_post == 0 && !is_paged() ) :
    global $more; $more = 1; the_content();
    else :
    the_excerpt();
    endif;

    http://codex.wordpress.org/Function_Reference/the_content
    http://codex.wordpress.org/Function_Reference/the_excerpt
    http://codex.wordpress.org/Function_Reference/is_paged

  3. crisw
    Member
    Posted 1 year ago #

    Thank you!

    I assume this would go in the theme function code? Sorry, I'm not a programmer.

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    that would possibly go into the template for the main page, typically index.php - although details vary with the used theme.

  5. crisw
    Member
    Posted 1 year ago #

    Here is the index.php...I've tried putting it in different places, but it just ends up hosing up the site. Again, I appreciate the help!

    <?php
    /**
     * The template for starting The Loop and rendering general content features such as the breadcrumbs, pagination, and sidebars. Uses
     * get_template_part to render the appropriate template based on the current post's format.
     *
     * @package Standard
     * @since 3.0
     */
    ?>
    
    <?php get_header(); ?>
    <?php $presentation_options = get_option( 'standard_theme_presentation_options' ); ?>
    
    <div id="wrapper">
    	<div class="container">
     		<div class="row">
    
    			<?php if ( 'left_sidebar_layout' == $presentation_options['layout'] ) { ?>
    				<?php get_sidebar(); ?>
    			<?php } // end if ?>
    
    			<div id="main" class="<?php echo 'full_width_layout' == $presentation_options['layout'] ? 'span12 fullwidth' : 'span8'; ?> clearfix" role="main">
    
    				<?php get_template_part( 'breadcrumbs' ); ?>
    
    				<?php if ( is_archive() ) { ?>
    	                <div id="archive-page-title">
    	                    <h3>
    	                        <?php _e( 'Archives For ', 'standard' ); ?>
    	                        <?php if( standard_is_date_archive() ) { ?>
    	                        	<?php echo standard_get_date_archive_label(); ?>
    	                    	<?php } elseif ( is_author() ) { ?>
    
    	                    		<?php
    	                    			$author_data = standard_is_using_pretty_permalinks() ?
    	                    				get_userdata( get_query_var( 'author' ) )  :
    	                    				get_userdata( user_trailingslashit( get_query_var( 'author' ) ) );
    	                    			echo $author_data->display_name;
    	                    		?>
    
    	                        <?php } elseif ( '' == single_tag_title( '', false ) ) { ?>
    	                            <?php echo get_cat_name( get_query_var( 'cat' ) ); ?>
    	                        <?php } else { ?>
    	                            <?php echo single_tag_title() ?>
    	                        <?php } // end if/else ?>
    	                    </h3>
    	            <?php if( '' != category_description() ) { ?>
    	                       <?php echo category_description(); ?>
    	                    <?php } // end if ?>
    	                </div>
    	            <?php } // end if ?> 
    
    				<?php if ( have_posts() ) { ?>
    
    					<?php while ( have_posts() ) { ?>
    						<?php the_post(); ?>
    						<?php get_template_part( 'loop', get_post_format() ); ?>
    					<?php } // end while ?>
    
    					<?php get_template_part( 'pagination' ); ?>
    
    				<?php } else { ?>
    
    					<article id="post-0" class="post no-results not-found">
    						<header class="entry-header">
    							<h1 class="entry-title"><?php _e( 'Page or resource not found', 'standard' ); ?></h1>
    						</header><!-- .entry-header -->
    						<div class="entry-content">
    							<p><?php _e( 'No results were found.', 'standard' ); ?></p>
    							<?php get_search_form(); ?>
    						</div><!-- .entry-content -->
    					</article><!-- #post-0 -->
    
    				<?php } // end if/else ?>
    			</div><!-- /#main -->
    
    			<?php if ( 'right_sidebar_layout' == $presentation_options['layout'] ) { ?>
    				<?php get_sidebar(); ?>
    			<?php } // end if ?>
    
    		</div><!-- /row -->
    	</div><!-- /container -->
    </div> <!-- /#wrapper -->
    
    <?php get_footer(); ?>

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. Or better still - use a pastebin. As it stands, your posted code may now have been permanently damaged/corrupted by the forum's parser.]

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    this line:

    <?php get_template_part( 'loop', get_post_format() ); ?>

    calls loop.php where you likely need to enter the code (or possibly replace either 'the_content()' or 'the_excerpt()').

  7. crisw
    Member
    Posted 1 year ago #

    I'm pretty sure this is the line of code to replace, but I can't seem to get the syntax correct.

    <div id="content-<?php the_ID(); ?>" class="entry-content clearfix">
    		<?php if( ( is_category() || is_archive() || is_home() ) && has_excerpt() ) { ?>
    			<?php the_excerpt( ); ?>
    			<a href="<?php echo get_permalink(); ?>"><?php _e( 'Continue Reading...', 'standard' ); ?></a>
    		<?php } else { ?>
    			<?php the_content( __( 'Continue Reading...', 'standard' ) ); ?>
    		<?php } // end if/else ?>
  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    possibly integrate the checks into the existing conditional statement:

    <div id="content-<?php the_ID(); ?>" class="entry-content clearfix">
    		<?php if( is_archive() || is_paged() || ( is_home() && $wp_query->current_post > 0 ) ) { ?>
    			<?php the_excerpt( ); ?>
    			<a href="<?php echo get_permalink(); ?>"><?php _e( 'Continue Reading...', 'standard' ); ?></a>
    		<?php } else { ?>
    			<?php the_content( __( 'Continue Reading...', 'standard' ) ); ?>
    		<?php } // end if/else ?>

    this part: && has_excerpt() which was checking for a handwritten excerpt, got removed.

    if you want to force the first post in full regardless of any 'more tags', you will additionally need to change this line:

    <?php the_content( __( 'Continue Reading...', 'standard' ) ); ?>

    to:

    <?php global $more; $more = 1; the_content( __( 'Continue Reading...', 'standard' ) ); ?>
  9. crisw
    Member
    Posted 1 year ago #

    Ah, brilliant! Thank you very much! Is there a way to get the same behavior on the archive pages? CW

  10. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    Is there a way to get the same behavior on the archive pages?

    change this line:

    <?php if( is_archive() || is_paged() || ( is_home() && $wp_query->current_post > 0 ) ) { ?>

    to:

    <?php if( is_paged() || ( ( is_home() || is_archive() ) && $wp_query->current_post > 0 ) ) { ?>
  11. crisw
    Member
    Posted 1 year ago #

    Thanks a ton! Appreciate the timely support!

Topic Closed

This topic has been closed to new replies.

About this Topic