WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic Excerpt length (7 posts)

  1. jmillspaysbills
    Member
    Posted 4 years ago #

    Howdy.

    I'm looking for something that I thought would be a lot simpler than it is apparently. I can't believe that I'm the first person who's done this, but here it goes.

    I'm working on a news blog. All of the items on the home page will be using the_excerpt() tag.

    Most articles would have 50 words. I was hoping for a way to make the most recent article a longer excerpt, lets say 120 words.

    I figured I could do some kind of if/else statement in the function file to change the excerpt filter length, but I can't seem to figure out how to target that top query and make it differently.

    The way I have it set up now is 2 queries. I have one query for the "featured section" which uses a jquery carousel that scrolls through the 5 most recent posts. Then below that are the rest of the posts.

    So, any ideas? Do you need to see the page code? I can supply if needed.

    Thanks in advance for your help!

  2. jmillspaysbills
    Member
    Posted 4 years ago #

    i figured it would help if you had the code.

    Thanks!

    <?php
    /**
     * Template Name: Front Page
     *
     * Useful for sites that need a news-type front page.
     *
     * @package HybridNews
     * @subpackage Template
     */
    
    /* Get the Hybrid News theme settings. */
    $hybrid_news = get_option( 'hybrid_news_theme_settings' );
    
    get_header(); ?>
    
    	<div class="hfeed content">
    
    		<?php hybrid_before_content(); // Before content hook ?>
    
    		<!-- Begin feature slider. -->
    		<div id="slider-container">
    
    			<div id="slider">
    
    			<?php
    				if ( $hybrid_news['feature_category'] )
    					$feature_query = array(
    					'cat' => $hybrid_news['feature_category'],
    					'showposts' => $hybrid_news['feature_num_posts'],
    					'caller_get_posts' => 1 );
    				else
    					$feature_query = array(
    					'post__in' => get_option( 'sticky_posts' ),
    					'showposts' => $hybrid_news['feature_num_posts'] );
    			?>
    
    				<?php $loop = new WP_Query( $feature_query ); ?>
    
    				<?php while ( $loop->have_posts() ) : $loop->the_post(); $do_not_duplicate[] = $post->ID; ?>
    
    					<div class="<?php hybrid_entry_class( 'feature' ); ?>">
    
    						<?php get_the_image( array( 'custom_key' => array( 'Large', 'Feature Image' ), 'size' => 'large' ) ); ?>
    
    						<?php hybrid_before_entry(); ?>
    
    						<div class="entry-summary">
    
    							 <?php the_excerpt(); ?>
    						</div>
    
    						<?php hybrid_after_entry(); ?>
    
    					</div>
    
    				<?php endwhile; ?>
    
    			</div>
    
    		</div>
    		<!-- End feature slider. -->	<div class="slider-controls">
    				<a class="slider-prev" title="<?php esc_attr_e( 'Previous Post', 'hybrid-news' ); ?>"><?php _e( 'Previous', 'hybrid-news' ); ?></a>
    				<a class="slider-pause" title="<?php esc_attr_e( 'Pause', 'hybrid-news' ); ?>"><?php _e( 'Pause', 'hybrid-news' ); ?></a>
    				<a class="slider-next" title="<?php esc_attr_e( 'Next Post', 'hybrid-news' ); ?>"><?php _e( 'Next', 'hybrid-news' ); ?></a>
    			</div>
    
    		<!-- Begin excerpts section. -->
    		<div id="excerpts">
    
    			<?php $loop = new WP_Query( array(
    			'cat' => $hybrid_news['excerpt_category'],
    			'showposts' => $hybrid_news['excerpt_num_posts'],
    			'caller_get_posts' => 1 
    
    			) ); ?>
    
    			<?php while ( $loop->have_posts() ) : $loop->the_post(); $do_not_duplicate[] = $post->ID; ?>
    
    				<div class="<?php hybrid_entry_class(); ?>">
    
    					<?php get_the_image( array( 'custom_key' => array( 'Thumbnail' ), 'size' => 'thumbnail' ) ); ?>
    
    					<?php hybrid_before_entry(); ?>
    
    					<div class="entry-summary">
    						<?php the_excerpt(); ?>
    					</div>
    
    					<?php hybrid_after_entry(); ?>
    
    				</div>
    
    			<?php endwhile; ?>
    
    		</div>
    		<!-- End excerpts section. -->
    
    		<?php if ( !empty( $hybrid_news['headlines_category'] ) ) : $alt = 'odd'; ?>
    
    			<!-- Begin category headlines section. -->
    			<div id="headlines">
    
    			<?php foreach ( $hybrid_news['headlines_category'] as $category ) : ?>
    
    				<?php $headlines = get_posts( array(
    					'numberposts' => $hybrid_news['headlines_num_posts'],
    					'category' => $category,
    					'post__not_in' => $do_not_duplicate
    				) ); ?>
    
    				<?php if ( !empty( $headlines ) ) : ?>
    
    					<div class="section <?php echo $alt; ?>">
    
    						<?php $cat = get_category( $category ); ?>
    
    						<h3 class="section-title"><a href="<?php echo get_category_link( $category ); ?>" title="<?php echo esc_attr( $cat->name ); ?>"><?php echo $cat->name; ?></a></h3>
    
    						<ul>
    						<?php foreach ( $headlines as $post ) : $do_not_duplicate[] = $post->ID; ?>
    							<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    						</ul>
    
    					</div>
    
    					<?php $alt = ( ( $i++ % 2 == 0 ) ? 'even' : 'odd' ); ?>
    
    				<?php endif; ?>
    
    			<?php endforeach; ?>
    
    			</div>
    			<!-- End category headlines section. -->
    
    		<?php endif; // End check if headline categories were selected. ?>
    
    		<?php hybrid_after_singular(); // After singular hook ?>
    
    		<?php hybrid_after_content(); // After content hook ?>
    
    	</div><!-- .content .hfeed -->
    
    <?php get_footer(); ?>
  3. jmillspaysbills
    Member
    Posted 4 years ago #

    What I'm trying to target is that "featured news" section up at the top.

    I tried some stuff with if ( $hybrid_news['feature_category'] ) but that didn't seem to do anything at all.

    It's quite possible I'm not doing something right, so any help would be appreciated.

    Thanks!

  4. jmillspaysbills
    Member
    Posted 4 years ago #

    bumpity bump bump!

  5. jmillspaysbills
    Member
    Posted 4 years ago #

    I've gotten this far. Still no luck.

    function new_excerpt_length( $length ) {
    	$hybrid_news = get_option( 'hybrid_news_theme_settings' );
    
    	if ( in_category( $hybrid_news['feature_category'] ) ) {
    		$length = 80;
    	} else {
    		$length = 250;
    	}
    	return $length;
    }
    
    add_filter( 'excerpt_length', 'new_excerpt_length' );
  6. jmillspaysbills
    Member
    Posted 4 years ago #

    Is it possible to target a div like this?

    Is it impossible no matter what?

    Can I do some kind of if/else statement to pull that content dynamically, then do the function call that way? Would that effect everything below it on the page?

    Thanks!

  7. Acts7
    Member
    Posted 3 years ago #

    source

    Don't know if you still need this but:

    function new_excerpt_length($length) {
    if(in_category(14)) {
    return 13;
    } else {
    return 60;
    }
    }
    add_filter('excerpt_length', 'new_excerpt_length');

Topic Closed

This topic has been closed to new replies.

About this Topic