0

I have the following two functions in my theme:

function content($limit) {
     global $post;
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
      } else {
        $content = implode(" ",$content);
      } 
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]>', $content);
      return $content;
    }

function contentnoimg($limit) {
     global $post;
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
      } else {
        $content = implode(" ",$content);
      }   
      $content = preg_replace('/(<img.+?>)/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]&gt;', $content);
      return $content;
    }

And in the loop, it is called this way:

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full', array('class'=>'post_thumbnail_common', 'alt' => get_the_title() , 'title' => get_the_title(), 'itemprop'=>'image' ));
echo contentnoimg(41);} else { echo content(41); } ?>

I would like the site to show the_excerpt(); only if it exists. If has_excerpt() is negative, then it should work as it is right now showing the content of get_the_content()

How should the code look like? I tried different ways, but my site stops working, so I am doing something wrong.

3
  • 1
    Have you tried if( has_excerpt() ) { the_excertp(); }? It seems you know those functions but I don't understand where do you have the problem to implement them.
    – cybmeta
    Commented Sep 22, 2015 at 10:06
  • I have tried. I know what those functions do, and I have used them in other themes. But in this case I have no idea, whether I should use it in the function, or where the function is called. And I tried both, but I am sure I am disrupting php syntax. Not sure where I should put them and how.
    – Pikk
    Commented Sep 22, 2015 at 10:14
  • Related: stackoverflow.com/questions/19935351/… Commented Nov 26, 2022 at 11:32

1 Answer 1

3

This works. You probably just had a syntax error.

function content( $limit ) {
    global $post;

    if( has_excerpt() ){
        $content = the_excerpt();
    } else {
      $content = explode( ' ', get_the_content(), $limit );
      if ( count($content) >= $limit ) {
        array_pop( $content );
        $content = implode( " ", $content );
        $content = wp_strip_all_tags( $content, true );
      } else {
        $content = implode( " ", $content );
      }

      $content = preg_replace( '/\[.+\]/','', $content );
      $content = apply_filters( 'the_content', $content ); 
      $content = str_replace( ']]>', ']]&gt;', $content );
    }

    return $content;
}
5
  • I see you added some spaces, for example here >= $limit and here ( ' ', are the spaces ok, or they are not relevant in the syntax?
    – Pikk
    Commented Sep 22, 2015 at 15:23
  • I didn't add any spaces inside empty strings like ( ' ', but I did add some spacing. It's just a mixture of preference and WordPress PHP coding standards and doesn't really affect anything besides readability. Commented Sep 22, 2015 at 15:32
  • My answer probably doesn't follow this strictly, but it's always good to know the standards the core team is using. make.wordpress.org/core/handbook/best-practices/… Commented Sep 22, 2015 at 15:36
  • strange, now no syntax errors. But the excerpt is not showing up. In the homepage nothing changed. It's still showing get_the_content(). But I did add to few posts a manual excerpt in the apposite field. Any idea?
    – Pikk
    Commented Sep 22, 2015 at 16:32
  • sorry, it works. I just had to apply the same to the $contentnoimg function :) thanks
    – Pikk
    Commented Sep 22, 2015 at 16:42

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.