• How I can tell HTML to cut words by words, not by symbols?
    Here is a part of my slider code to make myself more clear ??

    // display only first 85 characters in the title.
    $short_title = substr(the_title('','',FALSE),0,85);
    	echo $short_title;
    	if (strlen($short_title) >84){
    	echo '...';

Viewing 7 replies - 1 through 7 (of 7 total)
  • Get Better Excerpts ( https://thisismyurl.com/plugins/get-better-excerpts/ ) will do it for you, or you can grab the code from there and use it in your code.

    Something like…

    $words = 10;
    
    $short_title = implode (' ', array_slice (explode (' ', get_the_title ()), 0, $words));

    Of course that can be enhanced a fair bit to do different things, but that’s the base for it all.

    Thread Starter chicagonews

    (@chicagonews)

    I paste it like this

    // display only first 58 characters in the slide title.
    $words = 4;
    
    $short_title = implode (' ', array_slice (explode (' ', get_the_title ()), 0, $words));
    
    											$short_title = substr(the_title('','',FALSE),0,58);
    											echo $short_title;
    											if (strlen($short_title) >57){
    												echo '...';

    But nothing changes..
    @christopherross, I downloaded… installed… have no idea what to do next.
    Sorry. I’m new with HTML and coding…

    Here is the whole slider code…

    ?>
    <?php
    	$slider_cat_id = max_magazine_get_option( 'slider_category');
    	//if no category is selected for slider, show latest posts
    	if ( $slider_cat_id == 0 ) {
    		$post_query = 'posts_per_page=5';
    	} else {
    		$post_query = 'cat='.$slider_cat_id.'&posts_per_page=5';
    	}
    ?>
    
    <div id="slider">
    	<div class="lof-slidecontent">
    	<div class="preload"><div></div></div>
                <div class="main-slider-content">
                    <ul class="sliders-wrap-inner">
    					<?php query_posts( $post_query ); ?>
    					<?php if (have_posts()) : ?>
    						<?php while (have_posts()) : the_post(); ?>
    					        <li>
    								<?php if ( has_post_thumbnail()) : ?>
    									<a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'slider-image' ); ?></a>
    								<?php endif; ?>
    
    								<div class="lof-main-item-desc">
    									<div class="date"><?php the_time( get_option( 'date_format' ) ) ?></div>	
    
    									<h3>
    										<a href="<?php the_permalink(); ?>">
    
    										<?php
    											// display only first 58 characters in the slide title.
    $words = 4;
    
    $short_title = implode (' ', array_slice (explode (' ', get_the_title ()), 0, $words));
    
    											$short_title = substr(the_title('','',FALSE),0,58);
    											echo $short_title;
    											if (strlen($short_title) >57){
    												echo '...';
    											}
    										?>
    										</a>
    									</h3>
    
    									<div class="description">
    										<?php
    											// display only first 150 characters in the slide description.
    											$excerpt = get_the_excerpt();
    											echo substr($excerpt,0, 150);
    											if (strlen($excerpt) > 149){
    												echo '...';
    											}
    										?>
    									</div>
    								</div>
    							</li>
    						<?php endwhile; ?>
    					<?php endif; ?>
    					<?php wp_reset_query();?>
                    </ul>
                </div><!-- /main-slider-content --> 
    
    		   	<div class="navigator-content">
                      <div class="button-next">Next</div>
                      <div class="navigator-wrapper">
                            <ul class="navigator-wrap-inner">
    							<?php query_posts( $post_query ); ?>
    							<?php if (have_posts()) : ?>
    								<?php while (have_posts()) : the_post(); ?>
    									<?php if ( has_post_thumbnail()) : ?>
    										<li><?php the_post_thumbnail( 'small-thumb' ); ?></li>
    									<?php endif; ?>
    								<?php endwhile; ?>
    							<?php endif;?>
    							<?php wp_reset_query();?>
                            </ul>
                      </div>
                      <div  class="button-previous">Previous</div>
                 </div> <!-- /navigator-content --> 
    
                <div class="button-control"><span></span></div>
        </div>
     </div><!-- /slider -->

    You’ve done exactly what I thought htat you migh thave. ?? But don’t worry, we’ve all made the same mistake many (many…) times over.

    $words = 4;
    $short_title = implode (' ', array_slice (explode (' ', get_the_title ()), 0, $words));
    $short_title = substr(the_title('','',FALSE),0,58);

    What you’ve done there is set $short_title with what I’ve suggested,and on the very next line you’re setting it again which over-writes what you had before so you get the same value. You can either take the second line out, or you can comment it out by adding // right at the start of the line like this:

    $words = 4;
    $short_title = implode (' ', array_slice (explode (' ', get_the_title ()), 0, $words));
    // $short_title = substr(the_title('','',FALSE),0,58);
    Thread Starter chicagonews

    (@chicagonews)

    @catacaustic Works!
    But.. Only one thing.. We have lost “…”
    Usually on topic (and sub topic) we have more than 5 words. How I can say to HTML “cut after 4th word and than insert …”?
    Thank you.

    echo $short_title."..."

    That’s doing it for everything. There’s more that you can do to check if it’s more then 4 words, but that involves moving things around a fair bit. It’s not hard, but this might be a time to get a bit of practice with PHP to get it to do what you want it to. ??

    Thread Starter chicagonews

    (@chicagonews)

    @catacaustic Thank you so much.
    If I could say “Thanks” to you through Facebook, let me put you a like:)

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘My sliders cut words after a certain amount of symbols.’ is closed to new replies.