• Resolved Warren

    (@rabbitwordpress)


    Hi there,

    How can I set a character limit for the post content?

    I tried:
    {@echo substr(strip_tags($post->post_content), 0, 150);}

    I also tried this in functions:

    add_filter("the_content", "plugin_myContentFilter");
    		  function plugin_myContentFilter($content)
    		  {
    		    // Take the existing content and return a subset of it
    		    return substr($content, 0, 150);
    		  }

    …but it affects everything including the main blog posts.

    My custom post type is called Case Studies

    Do you have any suggestions please?

    Many thanks
    Warren

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Paul Clark

    (@pdclark)

    This will run the function plugin_myContentFilter on post_content:
    {@post_content,plugin_myContentFilter}

    With this defined in functions:

    function plugin_myContentFilter( $content ) {
    	// Take the existing content and return a subset of it
    	return substr( $content, 0, 150 );
    }
    Thread Starter Warren

    (@rabbitwordpress)

    Hi Paul ( @pdclark )

    Thanks for getting back to me.

    I am trying to set a max character count of 150 for my Case Studies custom post type only, example here. So if a user types in more than 150 characters anything beyond this won’t be displayed on the font end. Hope this makes sense.

    • This reply was modified 2 years, 6 months ago by Warren.
    Thread Starter Warren

    (@rabbitwordpress)

    Hi Paul ( @pdclark ),

    How can I limit the character count to 150 for the main content on the case_studies custom post type?

    Not sure if you missed my last message.

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @rabbitwordpress

    This could be a bit tricky as the_content doesn’t provide any context.
    However, in most cases this filter is called from the function the_content so you might “assume” the current post is the current object of that content.
    Again, this is NOT definite and might cause errors of other plugins or software don’t properly set the current (global) post object in loops etc.

    Example:

    
    add_filter( 'the_content', 'plugin_myContentFilter' );
    function plugin_myContentFilter( $content )
    {
    	// Get the current (global) post.
    	$post = get_post();
    	if ( 'case_studies' !== $post->post_Type ) {
    		return $content;
    	}
    	// Take the existing content and return a subset of it
    	return substr( $content, 0, 150 );
    }
    

    In addition to the above. Please also check wp_trim_words for this. It’s similar but might be a bit more neat: https://developer.wordpress.org/reference/functions/wp_trim_words/

    Let me know if this helps!

    • This reply was modified 2 years, 6 months ago by Jory Hogeveen. Reason: get_the_content is wrong
    • This reply was modified 2 years, 6 months ago by Jory Hogeveen. Reason: Format
    Thread Starter Warren

    (@rabbitwordpress)

    Hi Jory (@keraweb),

    Thanks for getting back to me, that’s much appreciated.

    Sadly I couldn’t get it to work. Not to worry, we’ll just have to leave it how it is and advise users not to input too much content.

    Many thanks
    Warren

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Content Maximum Character Limit’ is closed to new replies.