Support » Fixing WordPress » Limit output text searchresults

  • Resolved quinox

    (@quinox)


    Hi,

    I want to limit the text in the searchresults which is displayed when performing a search in WordPress.

    Say for example the searchresults shows the first 200 characters of my post. I want to limit this to say 100 characters.

    I looked on Google for hours, but couldn’t find a working solution for my search.php which is showed below.

    <h1 class="page-title"><?php printf( __( 'Zoekresultaten voor: %s', '_s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
    
    					<h2 class="items"><?php echo 'Gevonden artikelen: ' . $wp_query->found_posts; ?></h2>
    
    				<?php if(have_posts()): ?> <?php while(have_posts()) : the_post(); ?>
    
        			<div class="s-res">
        				<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
        					<?php if ( has_post_thumbnail() ): ?>
            		<div class="search-thumb">
                	<?php the_post_thumbnail('post-thumbnail', array( 'class'   => "search-thumb attachment-post-thumbnail")); ?>
            		</div>
        					<?php endif ?>
        						<?php echo str_replace('[...]', '', get_the_excerpt()); ?>
    				</div>    			
    
        				<?php endwhile; ?>
        					<?php else : ?>
        					<div class="s-res">
        					<i><?php _e('Sorry, maar we hebben niets gevonden wat aan uw zoekcriteria voldoet.<br> Probeer opnieuw met andere zoekwoorden..'); ?></i>
    					<div class="s-bar">
        					<?php get_search_form(); ?>
        				</div>
        				<?php endif; ?>
    
    				</div>

    Any help is appriciated.

    Roland

Viewing 3 replies - 1 through 3 (of 3 total)
  • Can use substring. it will return a some part of a string…. Take a look on documentation…

    You can use like this…

    <?php
    	$excerpt = get_the_excerpt();
    	$excerpt = substr($excerpt, 0, 100);
    	echo '<p>' . $excerpt . '</p>';
    ?>

    As you see, we save the excerpt into a variable, and then, we apply a substring to asave only the chars from 0 to 100. Or with less code…

    <?php echo '<p>' . substr(get_the_excerpt(), 0, 100) . '</p>'; ?>

    Integrated in your code, should look like this…

    <h1 class="page-title"><?php printf( __( 'Zoekresultaten voor: %s', '_s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
    
    					<h2 class="items"><?php echo 'Gevonden artikelen: ' . $wp_query->found_posts; ?></h2>
    
    				<?php if(have_posts()): ?> <?php while(have_posts()) : the_post(); ?>
    
        			<div class="s-res">
        				<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
        					<?php if ( has_post_thumbnail() ): ?>
            		<div class="search-thumb">
                	<?php the_post_thumbnail('post-thumbnail', array( 'class'   => "search-thumb attachment-post-thumbnail")); ?>
            		</div>
        					<?php endif ?>
    
                            	<?php
    								$excerpt = get_the_excerpt();
    								$excerpt = substr($excerpt, 0, 100);
    								echo '<p>' . $excerpt . '</p>';
    							?>
    				</div>    			
    
        				<?php endwhile; ?>
        					<?php else : ?>
        					<div class="s-res">
        					<i><?php _e('Sorry, maar we hebben niets gevonden wat aan uw zoekcriteria voldoet.<br> Probeer opnieuw met andere zoekwoorden..'); ?></i>
    					<div class="s-bar">
        					<?php get_search_form(); ?>
        				</div>
        				<?php endif; ?>
    
    				</div>

    I hope it be usefull to you 🙂

    Hi aritzalvarez,

    its not quit the excerpt output. My searchresults also output part of the content.
    Your suggestion only limit the output of the excerpt string.

    I “borrowed” this search.php file and this is the only thing i have to customize to have it perfect as I want.

    Roland

    Problem solved.

    It was a setting in Relavanssi plugin.

    Feel sooo stupid.

    Roland

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Limit output text searchresults’ is closed to new replies.