WordPress.org

Ready to get started?Download WordPress

Forums

Impose word count on get_content (16 posts)

  1. vikkineal
    Member
    Posted 1 year ago #

    <?php $id=9; $post = get_page($id); $content = apply_filters('the_content', $post->post_content); echo $content; ?>

    I've the above php to display the content of the post id#9 on my 'home page' of my site, so that whenever the client updates the page, it's reflected on the homepage as well - rather than having to make the change twice.

    At the moment all the post content is being shown, I need to strip it down to about 100 words...

    Any ideas?

  2. dfunkydog
    Member
    Posted 1 year ago #

    Is there a reason why you're not using the_excerpt()?

    If not you could the_excerpt and limit the length to 100 words instead of the default 55 limit http://codex.wordpress.org/Function_Reference/the_excerpt#Control_Excerpt_Length_using_Filters

    Otherwise you can trry this

    http://pastebin.com/c1XgPakW

  3. vikkineal
    Member
    Posted 1 year ago #

    I'm not using the_excerpt as that strips out the HTML formatting - i'll try the above, thank you

  4. vikkineal
    Member
    Posted 1 year ago #

    To confirm - the function goes in functions.php, is that right? I'm getting an undefined error when i do so... http://www.pjephysiotherapy.co.uk/home/

  5. dfunkydog
    Member
    Posted 1 year ago #

    It should work in the functions file but you can try putting the function just before calling it.

  6. vikkineal
    Member
    Posted 1 year ago #

    Hiya - tried that as well and get the error on the link above....

  7. vikkineal
    Member
    Posted 1 year ago #

    Do i need to register the function? I'm new to WordPress so could be missing something quite simple

  8. edpittol
    Member
    Posted 1 year ago #

    You must put yours functions in the functions.php file. Attention with strip a html in words. Maybe a tag can be opened and not closed.

  9. vikkineal
    Member
    Posted 1 year ago #

    Hiya - yes, I tried initially to have the function in functions.php - but it still errors, please see link above :(

  10. edpittol
    Member
    Posted 1 year ago #

    You are calling the function of wrong way. Can you post the php code of the homePageTemplate.php until the line 7?

  11. vikkineal
    Member
    Posted 1 year ago #

    here you go...

    <?php /* Template Name: Home Page */ ?>
    <?php get_header(); ?>
    
    <div class="wrapper clearfix">
      <div class="leftColumn halfColumn">
        <h2>About PJE Physiotherapy</h2>
        <?php $id=9; $post = get_page($id); $content = apply_filters('the_content', $post->post_content); echo limit_words($content(),100); ?>
        <p><a href="/about-me/">Read more</a></p>
      </div>
      <div class="rightColumn halfColumn">
        <h2>Testimonials</h2>
        <?php $id=12; $post = get_page($id); $content = apply_filters('the_content', $post->post_content); echo $content;  ?>
        <p><a href="/testimonials/">Read more</a></p>
      </div>
    </div>
    <?php get_footer(); ?>
  12. edpittol
    Member
    Posted 1 year ago #

    Remove the () after $content.

  13. vikkineal
    Member
    Posted 1 year ago #

    YOU ARE AMAZING! Thank you so much!

  14. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    just a warning about cutting a content string after any number of 'words':
    any basic function is likely to cut through html tags and therefore cause validation and display errors; also the length restriction is dependant on what and how many html tags are used in the content.

    possibly related or useful (untested):

    http://codex.wordpress.org/Function_Reference/force_balance_tags

    http://stackoverflow.com/questions/1193500/php-truncate-html-ignoring-tags
    http://www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags/
    http://www.codebank.perpos.net/recursively-locate-and-close-all-open-html-tags-automatically/
    http://dodona.wordpress.com/2009/04/05/how-do-i-truncate-an-html-string-without-breaking-the-html-code/

  15. vikkineal
    Member
    Posted 1 year ago #

    Hiya

    Thank you for the tips.

    To use the force_balance_tags would I add that to the functions.php or the formatting?

  16. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    To use the force_balance_tags would I add that to the functions.php or the formatting?

    I would add it to the function.

Topic Closed

This topic has been closed to new replies.

About this Topic