2

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>
2
  • Any progress? Could you solve the problem?
    – fuxia
    Commented Oct 31, 2012 at 2:54
  • thanks tosco - will be trying this next week so will let you know. Looks good!
    – v3nt
    Commented Nov 1, 2012 at 11:23

1 Answer 1

3

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'
        // https://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.

1
  • Thanks for that.. Was looking for it from quite some time.. :) Commented Feb 7, 2022 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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