• Resolved Bam Bam

    (@bam-bam)


    Hi guys,

    Still loving the plugin, got a small question I hope you can help me with, I am playing around with another Posts Grid block and I want to set the excerpt length and can’t quite figure it out.

    I have tried adding the following but it doesn’t work.

    $args = array(
    	'the_excerpt'    => array( 20 )
        );

    I also tried this, but again it didn’t work.

    <p><?php the_excerpt( 20 ); ?></p>

    I have no real understanding of php, it kind of does my head in.

    Sorry I can’t give you a link to the dev site.

    Kindest Regards
    Bam Bam

    • This topic was modified 5 years, 10 months ago by Bam Bam.
    • This topic was modified 5 years, 10 months ago by Bam Bam.
    • This topic was modified 5 years, 10 months ago by Bam Bam.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi, what does your Block Template look like?

    Ive limited the excerpt like this,
    in your functions.php add

    //limit length of Excerpt
    function limit_words($string, $word_limit) {
    	// creates an array of words from $string (this will be our excerpt)
    	// explode divides the excerpt up by using a space character
    	$words = explode(' ', $string);
    	// this next bit chops the $words array and sticks it back together
    	// starting at the first word '0' and ending at the $word_limit
    	// the $word_limit which is passed in the function will be the number
    	// of words we want to use
    	// implode glues the chopped up array back together using a space character
    	return implode(' ', array_slice($words, 0, $word_limit));
     }

    Im using the “post” block type to display a chose post, in the template im using the following to display & limit the exerpt..

    <?php echo limit_words(get_the_excerpt(block_sub_value( 'your-block-type-sub-value' )->ID), '55'); ?>

    mines a sub-value as its in a repeater field, so adjust that as necessary..

    hope that helps?

    Thread Starter Bam Bam

    (@bam-bam)

    This is the whole php for this block.

    <?php
        // Variables
    	
        $post_count = block_value( 'number-of-posts' );
        $post_type  = block_value( 'post-type' );
    	
    
        // WP_Query arguments
        $args = array(
            'post_type'      => array( $post_type ),
            'posts_per_page' => $post_count,
    	'category__in'   => array( 40 ),
        );
    
        // The Query
        $post_grid_query = new WP_Query( $args );	
    	
        // The Loop
        while ( $post_grid_query->have_posts() ) {
            $post_grid_query->the_post();
            ?>
    
            <div class="block-the-norwegian-posts--post">
                <div class="block-the-norwegian-posts--post-thumbnail" style="background-image: url('<?php the_post_thumbnail_url( 'large' ); ?>');">
                </div>
                <div class="block-the-norwegian-posts--post-content">
                    <a href="<?php the_permalink(); ?>">
    		    <h4 align="center"><?php the_title(); ?>
    		</a>
    		</h4>
                    <p><?php the_excerpt(); ?></p>
                    <a class="read-more" href="<?php the_permalink(); ?>">Read More</a>
                </div>
            </div>
    
            <?php
        }
    
        // Restore original Post Data
        wp_reset_postdata();
        ?>
    Thread Starter Bam Bam

    (@bam-bam)

    Never mind I’m an idiot sometimes, I hadn’t actually created an excerpt, all sorted.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘set excerpt length’ is closed to new replies.