Site logo

Archived topic

Post Type Video excerpts on archive pages

9 replies · Started by wo on October 26, 2021

Viewing posts 1–10 of 10

I'm using GPP 2.0.3 and "Volume" from the Site Library. Everything is phenomenal as usual, except Youtube Videos as Featured Image.

It's actually pretty simple to have any Youtube video as featured image. All you have to do is, *share* the link, e.g.

https://youtu.be/ugiT_MoWa5I

and paste it into your post content, formatted as "Video", instead of "Standard".

Gutenberg will convert the link and show the video. You even can start its playback. Even better: it will be your featured image, as well.

Interesting, whyever, the corresponding URL

https://www.youtube.com/watch?v=ugiT_MoWa5I

does *not* work like this.

BUT: After switching from "Standard" to "Video", this post no longer has an excerpt. Instead, the archive page is showing it full length.

In order to "repair" it, I found this code snippet somewhere on wordpress.org:

add_filter( 'generate_show_excerpt', function( $show ) {
    if ( 'video' === get_post_format() ) {
        return true;
    }

    return $show;
} );

Unfortunately, this does not work. Activating it, does indeed show the excerpt again. But without the featured video. Deactivating it, I'm back again with my full length post on the archive list, including the featured video again.

So. What's still wrong with the snippet? Or do I have to "warp" my archive.php somehow?

Thanks in advance
Wolfram

Sorry, my mistake.

It's all about standard post excerpts in the format "Video".

Post type is "Standard".

Post format has to be switched to "Video", if you want the thumbnail of the first video URL in post content to be used as featured image, as described in my first post here.

And, as a matter of fact, youtube.com/watch or Youtu.be, doesn't make any difference.

I see this is a WP issue, not GP. So probably, I should go for their support. But maybe it's simply a forgotten "add support" issue with the GP theme itself or "Volume" from the GP Site Library.

Meanwhile I learned, how I *could* check that, e.g. according to

https://wordpress.org/support/article/post-formats/

https://knowthecode.io/docx/wordpress/add_post_type_support

But with my PHP "expertise", this looks like a dangerous jungle.

Any hints for an elegant workaround, using magic GP hooks or something? :)

Hi there,

The theme itself supports the basic things like any other theme - https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/functions.php#L24-L37

But add_post_type_support is completely optional and something that must be added by the user because theme developers have no way of guessing what CPTs do users add and what exact slug and/or name they use for their specific CPTs for the add_post_type_support.

But with my PHP “expertise”, this looks like a dangerous jungle.

If you're going to resort to PHP codes, do it on a child theme and add/edit the code through FTP so if you make mistakes that crashes the site, you can just undo it by deleting the codes you've added. :D

Personally, I'd go with Block Element - Content template and modify the post list layout to only display the things I need.

I think if you watch this, you'll get the idea - https://www.youtube.com/watch?v=-ZTQP_KA2xE

Basically, I'll just create the layout I want then add the dynamic value for everything I want.

I may need an ACF field that holds the embed code for the dynamic video to show on the post listing.

I will then create a a custom shortcode as a helper function to display this dynamic video.

Example: (assuming I have a youtube_url ACF field for posts which holds youtube URLs for each posts)

add_shortcode( 'video_excerpt', function() {
    ob_start();
    // Start your PHP below
    $youtube_url = get_field('youtube_url',get_the_ID());
    echo '<iframe width="560" height="315" src="'.$youtube_url.'" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
  
    // End your PHP above
    return ob_get_clean();
} );

With this, I can simply use [video_excerpt] shortcode on a shortcode block on my Block Element - Content template to display the video pulled from ACF associated to the post as the video exerpt.

Oh man, took me a while to wrap my head around it, thank you so much.

We're almost there. Except this ugly fatal error incl. Email from WordPress. Fortunately, the site is still standing :)

My Block Content Template is fine. It's actually replacing the standard post list, kind of like in the tutorial. Just a few design tweaks yet required.

But the shortcode snippet doesn't work.

As long as it doesn't throw this fatal error, my post archive list is empty, after the header.

As soon as I deactivate the snippet, the list is displayed as intended. Without the featured video, of course ...

Here's my version of the snippet:

add_shortcode( 'featured_youtube_video', function() {
    ob_start();
    // Start your PHP below
	if( get_field('featured_youtube_url') ):
    	$url = get_field('featured_youtube_url', get_the_ID());
    	echo '<iframe width="560" height="315" src="'.$url.'" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
	endif;
    // End your PHP above
    return ob_get_clean();
} );

The custom field name is "featured_youtube_url". Value is a valid Youtube URL or empty.

The shortcode block on the content template is "[featured_youtube_video]"

My first idea, why this code crashes, maybe I should check the custom field value <> empty. Tried. Failed.

What's still wrong?

(Don't hesitate. I'm prepared now for another heavy duty hit :)

Little add-on.

I replaced the shortcode block by an "embed" block. Pasted a Youtube URL. Gutenberg shows the player.

Now, the custom post archive page, that I built along the tutorial, see above, show just the Youtube URL.

I'd expected, all posts are listed with an embedded video "excerpt".

Nope.

The problem w/ the embed block is you can't set a dynamic link for it. hence, a shortcode.

I'd modify your shortcode a bit to this:

add_shortcode( 'featured_youtube_video', function() {
    ob_start();
    // Start your PHP below
    $url = get_field('featured_youtube_url', get_the_ID());
	if( $url ){
        echo '<iframe width="560" height="315" src="'.$url.'" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
    }

    // End your PHP above
    return ob_get_clean();
} );

Can you share the error you're getting when using the shortcode snippet?

Elvin, I've to apologize again. Your shortcode is fine.

Well.

Basically :)

First of all, I had no ACF plugin. Now I know, that's essential. Switching from standard WP Custom Fields to Advanced Custom Fields makes the entire difference.

Finally I just had to tweak the URL again. Here in Europe, GDPR requires a special "No Cookie" URL. So my final snippet is

add_shortcode( 'featured_youtube_video', function() {
    ob_start();
    // Start your PHP below
    $id = get_field('featured_youtube_id', get_the_ID());
	if( $id ) {
		$url = 'https://www.youtube-nocookie.com/embed/'.$id;
		echo '<iframe width="640" height="360" src="'.$url.'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
    }
    // End your PHP above
    return ob_get_clean();
} );  

And it works like a charm.

Of course, there is more to optimize. I've installed the plugin "WP YouTube Lyte" for lazy loading videos. Seems to work fine, in combination.

But: what about calling lyte directly from my shortcode?

The standard call via shortcode is [lyte id="YOUTUBE_ID"]

But ... I see ... that's definitely beyond GP :)

Thank you so much for getting me in the right direction!

I'm not exactly sure about the workings of lyte's shortcode but if you can get its function, you can modify it to create your own shortcode based on it. :)

For this, consider contacting lyte's support. Perhaps they've open sourced their codes to a public repository.

No problem. Glad to be of any help. :D

Not sure, if I am supposed to mark this resolved.

I just do it anyway ... Thanks again, until next!

Thanks. Feel free to open new topics if you need further help. :D

This archived topic is closed to new replies.