WordPress.org

Ready to get started?Download WordPress

Forums

Simply using an excerpt on a category page (5 posts)

  1. csimpson
    Member
    Posted 1 year ago #

    Hi there, ive been searching for hours just for a simple example of a category.php that shows each entry post in an excerpt, ideally with the featured image. My theme is showing the entire post on the category page which is rather stupid - any help much appreciated please, as i keep reading the same advice elsewhere that i am having no luck with...

    e.g. use the more tag (my friend wont remember to do this everytime plus it will still show any image at the top of the post, secondly, replace the_content() and change it to the_excerpt() - my category.php file doesnt contain the_content()!

    help.. help...
    here is my code:

    <?php get_header(); ?>
    
    <div class="container">
    	<div class="container_top"></div>
    	<div class="container_bg">
    		<div class="middle">
    			<div class="page-title">
    				<h1>
    					<?php
    					printf( __( '%s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );
    				?>
    				</h1>
    			</div>
    			<div class="temp_loop">
    				<?php
    					$category_description = category_description();
    					if ( ! empty( $category_description ) )
    						echo '<div class="archive-meta">' . $category_description . '</div>';
    
    				/* Run the loop for the category page to output the posts.
    				 * If you want to overload this in a child theme then include a file
    				 * called loop-category.php and that will be used instead.
    				 */
    				get_template_part( 'loop', 'category' );
    				?>
    			</div>
    		</div>
    		<div class="right">
    			<div class="page-title">
    				<h1>Featured</h1>
    			</div>
    			<ul class="featured_posts">
    				<?php $index_query = new WP_Query('post_type=feature&showposts=10'); ?>
    				<?php $index=0; ?>
    				<?php while ($index_query->have_posts()) : $index_query->the_post(); ?>
    				<li> <a href="<?php the_permalink(); ?>">
    					<?php the_title(); ?></a> | <?php the_time('l M, Y'); ?>
    				</li>
    				<?php $index++ ?>
    				<?php endwhile; ?>
    			</ul>
    			<?php dynamic_sidebar('Recent comments category'); ?>
    		</div>
    		<div class="clear"></div>
    	</div>
    	<div class="container_bot"></div>
    </div>
    <?php get_footer(); ?>
  2. csimpson
    Member
    Posted 1 year ago #

    Here is a url too - top post showing as excerpt as was using MORE feature only... http://www.fitnessteamipswich.co.uk/category/workouts/studio-cycling/

  3. Fabiana Simões
    Member
    Posted 1 year ago #

    Do you have a file called "loop-category.php"? If so, try replacing the_content() for the_excerpt() there.

    Take a look at what's happening within <div class="temp_loop">...</div>. You will see that it's using the get_template_part() function, which will load a template part into your template. Most likely, the code for each post is in that template part (in your case, I believe the template part is defined in "loop-category.php"), so that's where you'd replace the content() for the excerpt().

    See https://codex.wordpress.org/get_template_part for more information on get_template_part().

  4. csimpson
    Member
    Posted 1 year ago #

    affraid i dont, ive been reading about using the loop-category.php file as well, but this template doesnt have it either. i think the get_template_part is just refering to the main loop file but then i cannot see any reference in that script which particularly is being used for the category pages.

    For the love of god.. i copy the whole loop file!

    <?php /* If there are no posts to display, such as an empty archive page */ ?>
    <?php if ( ! have_posts() ) : ?>
    
    <div id="post-0" class="post error404 not-found">
    	<h1 class="entry-title">
    		<?php _e( 'Not Found', 'twentyten' ); ?>
    	</h1>
    </div>
    <!-- #post-0 -->
    <?php endif; ?>
    <?php
    	/* Start the Loop.
    	 *
    	 * In Twenty Ten we use the same loop in multiple contexts.
    	 * It is broken into three main parts: when we're displaying
    	 * posts that are in the gallery category, when we're displaying
    	 * posts in the asides category, and finally all other posts.
    	 *
    	 * Additionally, we sometimes check for whether we are on an
    	 * archive page, a search page, etc., allowing for small differences
    	 * in the loop on each template without actually duplicating
    	 * the rest of the loop that is shared.
    	 *
    	 * Without further ado, the loop:
    	 */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php /* How to display posts in the Gallery category. */ ?>
    <?php if ( in_category( _x('gallery', 'gallery category slug', 'twentyten') ) ) : ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
    		<?php the_title(); ?>
    		</a></h2>
    	<div class="entry-meta">
    		<?php twentyten_posted_on(); ?>
    	</div>
    	<!-- .entry-meta -->
    
    	<div class="entry-content">
    		<?php if ( post_password_required() ) : ?>
    		<?php the_content(); ?>
    		<?php else : ?>
    		<?php
    					$images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    					if ( $images ) :
    						$total_images = count( $images );
    						$image = array_shift( $images );
    						$image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
    				?>
    		<div class="gallery-thumb"> <a class="size-thumbnail" href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a> </div>
    		<!-- .gallery-thumb -->
    		<p><em><?php printf( __( 'This gallery contains <a %1$s>%2$s photos</a>.', 'twentyten' ),
    								'href="' . get_permalink() . '" title="' . sprintf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) . '" rel="bookmark"',
    								$total_images
    							); ?></em></p>
    		<?php endif; ?>
    		<?php the_excerpt(); ?>
    		<?php endif; ?>
    	</div>
    	<!-- .entry-content -->
    
    	<div class="entry-utility"> <a href="<?php echo get_term_link( _x('gallery', 'gallery category slug', 'twentyten'), 'category' ); ?>" title="<?php esc_attr_e( 'View posts in the Gallery category', 'twentyten' ); ?>">
    		<?php _e( 'More Galleries', 'twentyten' ); ?>
    		</a> <span class="meta-sep">|</span> <span class="comments-link">
    		<?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
    		</span>
    		<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    	</div>
    	<!-- .entry-utility -->
    </div>
    <!-- #post-## -->
    
    <?php /* How to display posts in the asides category */ ?>
    <?php elseif ( in_category( _x('asides', 'asides category slug', 'twentyten') ) ) : ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<?php if (is_search() ) : // Display excerpts for archives and search. ?>
    	<div class="entry-summary">
    		<?php the_excerpt(); ?>
    	</div>
    	<!-- .entry-summary -->
    	<?php else : ?>
    	<div class="entry-content">
    		<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?>
    	</div>
    	<!-- .entry-content -->
    	<?php endif; ?>
    	<div class="entry-utility">
    		<?php twentyten_posted_on(); ?>
    		<span class="meta-sep">|</span> <span class="comments-link">
    		<?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
    		</span>
    		<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    	</div>
    	<!-- .entry-utility -->
    </div>
    <!-- #post-## -->
    
    <?php /* How to display all other posts. */ ?>
    <?php else : ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    <?php  if(has_post_thumbnail()) { ?>
    <div class="entry-meta">
    		<?php the_time('l M, Y'); ?>
    		<?php the_time('g:i A'); ?>
    		| by
    		<?php the_author_posts_link(); ?>
    		| <span class="comments-link">
    		<?php comments_popup_link( __( 'NO COMMENTS', 'twentyten' ), __( '1 COMMENT', 'twentyten' ), __( '% COMMENTS', 'twentyten' ) ); ?>
    		</span> </div>
    	<!-- .entry-meta -->
    <div class="post_left"><?php
    $thsrc = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail', false, '' );
    $fullsrc = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full', false, '' );
    echo ('<a href="'.$fullsrc[0].'"><img src="'.$thsrc[0].'"/></a>'); ?></div>
    <div class="post_right">
    
    	<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
    		<?php the_title(); ?>
    		</a></h2>
    	<?php if (is_search() ) : // Only display excerpts for archives and search. ?>
    	<div class="entry-summary">
    		<div class="style">
    			<?php the_excerpt(); ?>
    		</div>
    	</div>
    	<!-- .entry-summary -->
    	<?php else : ?>
    	<div class="entry-content">
    		<div class="style">
    			<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?>
    		</div>
    	</div>
    	<!-- .entry-content -->
    	<?php endif; ?>
    	<?php /* <div class="entry-utility">
    				<?php if ( count( get_the_category() ) ) : ?>
    					<span class="cat-links">
    						<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
    					</span>
    					<span class="meta-sep">|</span>
    				<?php endif; ?>
    				<?php
    					$tags_list = get_the_tag_list( '', ', ' );
    					if ( $tags_list ):
    				?>
    					<span class="tag-links">
    						<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
    					</span>
    					<span class="meta-sep">|</span>
    				<?php endif; ?>
    				<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span>
    				<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    			</div><!-- .entry-utility --> */ ?>
    </div>
    <div class="clear"></div>
    <?php } else { ?>
    <div class="entry-meta">
    		<?php the_time('l M, Y'); ?>
    		<?php the_time('g:i A'); ?>
    		| by
    		<?php the_author_posts_link(); ?>
    		| <span class="comments-link">
    		<?php comments_popup_link( __( 'NO COMMENTS', 'twentyten' ), __( '1 COMMENT', 'twentyten' ), __( '% COMMENTS', 'twentyten' ) ); ?>
    		</span> </div>
    	<!-- .entry-meta -->
    	<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
    		<?php the_title(); ?>
    		</a></h2>
    	<?php if (is_search() ) : // Only display excerpts for archives and search. ?>
    	<div class="entry-summary">
    		<div class="style">
    			<?php the_excerpt(); ?>
    		</div>
    	</div>
    	<!-- .entry-summary -->
    	<?php else : ?>
    	<div class="entry-content">
    		<div class="style">
    			<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?>
    		</div>
    	</div>
    	<!-- .entry-content -->
    	<?php endif; ?>
    	<?php /* <div class="entry-utility">
    				<?php if ( count( get_the_category() ) ) : ?>
    					<span class="cat-links">
    						<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
    					</span>
    					<span class="meta-sep">|</span>
    				<?php endif; ?>
    				<?php
    					$tags_list = get_the_tag_list( '', ', ' );
    					if ( $tags_list ):
    				?>
    					<span class="tag-links">
    						<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
    					</span>
    					<span class="meta-sep">|</span>
    				<?php endif; ?>
    				<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span>
    				<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    			</div><!-- .entry-utility --> */ ?>
    <?php } ?>
    
    </div>
    <!-- #post-## -->
    
    <?php comments_template( '', true ); ?>
    <?php endif; // This was the if statement that broke the loop into three parts based on categories. ?>
    <?php endwhile; // End the loop. Whew. ?>
    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if (  $wp_query->max_num_pages > 1 ) : ?>
    <div id="nav-below" class="navigation">
    	<div class="nav-previous">
    		<?php next_posts_link( __( '<span class="meta-nav">&laquo;</span> OLDER', 'twentyten' ) ); ?>
    	</div>
    	<div class="nav-next">
    		<?php previous_posts_link( __( 'NEWER <span class="meta-nav">&raquo;</span>', 'twentyten' ) ); ?>
    	</div>
    	<div class="clear"></div>
    </div>
    <!-- #nav-below -->
    <?php endif; ?>
  5. Fabiana Simões
    Member
    Posted 1 year ago #

    Just a disclaimer: You should not make changes to the theme files, since your changes will be gone whenever you update the theme itself.

    Back to the issue: Indeed, if it doesn't find a "loop-category.php" file, then it will fallback to "loop.php".

    Note the following piece of comment in the code: In Twenty Ten we use the same loop in multiple contexts. It is broken into three main parts: when we're displaying posts that are in the gallery category, when we're displaying posts in the asides category, and finally all other posts. So, by changing the_content() by the_excerpt() in the loop, you will not only change the category page, but also all other lists of posts (except for gallery and aside).

    I recommend you create a child theme and then create a "loop-category.php" template, so you can target only category pages.

Topic Closed

This topic has been closed to new replies.

About this Topic