• I want to remove all the content from the excerpt except for video embeds for a specific query.

    So that I can display all posts in the video category, on a media page, and have only the video show up without the copy that accompanies the post.

    Any thoughts? Maybe some sort of filter function?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You may want to use custom fields.

    Just create a custom field called “video_url”[or whatever] and then use this code to display the archive which will show only the video:

    $video = get_post_meta($post->ID, 'video_url', $single = true);
    // wp loop starts here
    echo $video;
    // end loop

    If you still require regular archive pages then you can create a custom category archive file and title it “category-videos.php” to handle these queries. This will list all posts in that category using the code above. (assuming the category name is videos)

    I would keep the number of videos displayed on each page somewhat low since that could take a while to load for some users.

    Thread Starter John Leschinski

    (@picard102)

    Ya, ideally that’s how I’d handle it, but I’m trying to make it as easy for the client as I can. Not sure how well they’d take to using custom fields.

    But what I’m trying to do is only show the video sans any other text and formatting in the post. What you propose looks like it’s just to call up the video posts.

    I have a partial answer, I think.

    To do the actual replacing, you’d hook a function up to the the_excerpt filter. Then, get that function to only strip out the text when we’re on your “media” page (I don’t know how you’ve got that set up, but I’m assuming as a regular ol’ WP page). The actual stripping would probably have to be done with a regex (which, unfortunately I’m not good at so you’re on your own there).

    I’d start with something along the lines of:

    function my_filter_function($str_excerpt) {
    
      if(is_page('your-media-page-slug-or-id')) { // if we're on the right page
    
        preg_match('[wacky pattern to match]',$str_excerpt, $matches); // run the regex
    
        return $matches[0]; // return the important part of the match
    
      }
    
      return $str_excerpt; // otherwise, return the full, untouched excerpt
    
    }
    
    add_filter('the_excerpt', 'my_filter_function');

    Does this make sense?

    Got something working! Few caveats though:

    1) This only acts on content entered into the “Excerpt” field in the post editor, and only works if you’re using the_excerpt() in your template. By default, if you don’t enter an excerpt, WordPress returns the first 55 words of your post as the_excerpt(), and it won’t contain any embed codes. In that case, this function will fall back to those 55 words.

    2) This doesn’t account for multiple embeds in a single excerpt, and may choke on nested object tags (again, I’m not great at regex).

    3) Getting it to be page-sensitive is still up to you. This code assumes you’re using a WP page with the slug ‘your-media-page’.

    function my_filter_function($str_excerpt) {
    
      if(is_page('your-media-page')) { // if we're on the right page
    
        preg_match('/(<object[^>]*>)(.*?)(<\/object>)/si',$str_excerpt, $matches); // run the regex
    
    	if($matches[0]) { // If we found an embed code
    
        	return $matches[0]; // return the embed code
    
    	}
    
      }
    
      return $str_excerpt; // otherwise, return the full, untouched excerpt
    
    }
    
    add_filter('the_excerpt', 'my_filter_function');
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude content from excerpt.’ is closed to new replies.