• Resolved kristinachilds

    (@kristinachilds)


    i’m using a custom function to control excerpt lenghts in multiple places site-wide. on my archive pages there’s a word limit of 55 ex: <?php echo content(55); ?>

    this is great except for that it also includes video embeds and/or images within the body text if the embedded content is within the first 55 words and it throws off the whole structure of the archive pages. is there an easy way to restrict the inclusion of any <object> tag or image in the body <p> tag? here is the function

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

    great function, btw. i didn’t write it but it works like a charm and allows for a TON of flexibility.

Viewing 4 replies - 1 through 4 (of 4 total)
  • you might want to look at some examples on the PHP strip_tags() function reference, which will allow you to remove any HTML from your $content – there are examples there which strip embed code reliably.

    https://php.net/manual/en/function.strip-tags.php

    you might want to apply that before you trim the length to make sure you get an accurate word count and to make sure you get it all.

    ALSO it’s probably improtant to note here that you’re not actually using the_excerpt() function – your custom func employs the_content() to do this job, which doesn’t strip any of the stuff the_excerpt usually would strip for you automatically.

    Thread Starter kristinachilds

    (@kristinachilds)

    nevermind, i just did it with css *smacks forehead*

    .blog-list-info object, .blog-list-info img {
            display: none;
            }

    since this style ONLY applies to the various excerpt sizes i have around the site, the descendant selectors work like a charm (and is super light-weight).

    i love css

    Thread Starter kristinachilds

    (@kristinachilds)

    sending the broken tags to the browser, having the browser interpret them, and then having the browser hide code that it’s interpreted isn’t light weight, and doesn’t really fix anything.

    The browser will still pull the image or attempt to reach the 3rd party site to do the embed, and then not display it. That’s a hell of a long way from light-weight.

    swept under the rug certainly, but not exactly fixed.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to restrict object embeds from excerpt’ is closed to new replies.