Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Post formats in WP and Excerpts

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #164947
    Jason Penezich
    Participant

    So I have been building a theme for awhile now and the excerpts, archives, and search pages are quite different then the main guts of the page. It will display the excerpt depending on the post format type.. Normal stuff. However what I am having difficulty with is getting the information I need from the post.

    Example:

    // Custom Excerpt
    function new_trim_excerpt($text) { // Fakes an excerpt if needed
      global $post;
      if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace('\]\]\>', ']]>', $text);
        $text = strip_tags($text, '<p> <iframe> <a>');
        $excerpt_length = 35;
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words)> $excerpt_length) {
          array_pop($words);
          array_push($words, '...');
          $text = implode(' ', $words);
        }
      }
    return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'new_trim_excerpt');
    

    I’m using that to filter the <iframe>, <a>, and <p> tags to stay in the excerpt. Problem is this method will show all iframes on any type of post format. I want to be able to grab the information I need and display it, instead of it being so random. So say we have a embeded video in the post. The excerpt will grab the video, which I want, but on any type of post format. I’d like to find a way to have the excerpt look for a video/iframe in the post, use it for the excerpt instead of any text that may come before it.

    I couldn’t find anything on the index about it, or I just didn’t know what to look for. I’ve tried searching about oEmbed, but haven’t been able to come up with a solution besides the code above. Something like:

    elseif ( has_post_format( 'video' ) ) {
                the_video();
            }
    
    #165238
    Jason Penezich
    Participant
    function improved_trim_excerpt($text) {
            global $post;
            if ( '' == $text ) {
                    $text = get_the_content('');
                    $text = apply_filters('the_content', $text);
                    $text = str_replace('\]\]\&gt;', ']]&gt;', $text);
                    $text = preg_replace('@&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;@si', '', $text);
                    if ( has_post_format('video') ) {
                        $text = strip_tags($text, '&lt;iframe&gt;');
                    }
                    else {
                        $text = strip_tags($text, '<p> <a>');
                    }
                    $excerpt_length = 35;
                    $words = explode(' ', $text, $excerpt_length + 1);
                    if (count($words)&gt; $excerpt_length) {
                            array_pop($words);
                            array_push($words, '<a>ID) . '"&gt; ....</a>' );
                            $text = implode(' ', $words);
                    }
            }
            return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');
    

    So I have it somewhat working now using the above code. However now I can’t get it to strip the words from the excerpt. Is there anyway I can just write a new function that I can call the <iframe>, or even use the excerpt with some type of variable that will only allow the $text = strip_tags($text, '<iframe>'); if I call it? I’m unsure how to go about it, but I have been searching.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.