Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a large amount of posts and some only have one sentence. This means for some posts all the content can be read in an archive without needing to go to the posts single page.

For these posts I want to disable the_permalink in the post's title. Is there a way I can detect if the excerpt length limit was reached?

Then I can use something like

<a href="<?=($if_has_more_link ? the_permalink() : '#' )?>" ><? the_title(); ?></a>
share|improve this question
    
Any progress? Could you solve the problem? –  toscho Oct 31 '12 at 2:54
    
thanks tosco - will be trying this next week so will let you know. Looks good! –  daniel Crabbe Nov 1 '12 at 11:23

1 Answer 1

We need two functions in the theme’s functions.php:

  1. One function checking the length of the content to adjust the title.
  2. Another one to print the formatted post content in case the first function returns TRUE.

Let’s start with the first function. This is rather simple because we solved that already. :)

if ( ! function_exists( 'is_short_content' ) )
{
    /**
     * Is the passed string not longer than an excerpt?
     *
     * @param  string $content
     * @return boolean
     */
    function is_short_content( $content = NULL )
    {
        NULL === $content && $content = get_the_content();
        // Get maximal excerpt length for this theme
        $max_word_length = apply_filters( 'excerpt_length', 55 );

        // see 'Counting words in a post'
        // http://wordpress.stackexchange.com/a/52460/73
        $content_text    = trim( strip_tags( $content ) );
        $content_words   = preg_match_all( '~\s+~', "$content_text ", $m );

        return $content_words <= $max_word_length;
    }
}

The second function will replace the excerpt with the full content conditionally:

if ( ! function_exists( 't5_show_short_content' ) )
{
    add_filter( 'the_excerpt', 't5_show_short_content' );

    /**
     * Print the whole content if the visible words are less than the excerpt length.
     *
     * @param  string $excerpt
     * @return string
     */
    function t5_show_short_content( $excerpt )
    {
        $content = apply_filters( 'the_content', get_the_content() );
        $content = str_replace( ']]>', ']]&gt;', $content );
        return is_short_content( $content ) ? $content : $excerpt;
    }
}

Whenever you call the_excerpt() in your theme this function will filter it and return the full formatted content if this is not longer than the excerpt would be.

Now to your initial question: changing the title. We will just reuse the function is_short_content() for that:

if ( is_short_content() )
{
    the_title( '<h2>', '</h2>' );
}
else
{
    the_title( '<h2><a href="' . get_permalink() . '">', '</a></h2>' );
}

Note how I put the markup into the the_title() function: If a post has no title, we don’t want to see an empty <h2>. And I removed the link completely, because <a href="#"> is a terrible user experience. :)

As a last note: Do not use just <? to open a PHP context. Short open tags are disabled in many environments because they break XML output.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.