WordPress get YouTube image thumbnail - php

I'm trying to let users post a video on one our sites with text beneath it. Is there a way display the YouTube thumbnail image as a user's post thumbnail.
Example:
I post a YouTube video on the site:
IMAGE LINK
Using HTML/PHP I pull in the posts on to my website using the below code:
<?php
$posts = get_posts('cat=3');
foreach ($posts as $post) : setup_postdata( $post );
?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php endforeach; ?>
I want to load the image/video and then the title of the post. Not the date or any other content. How can this be done?
Then how can I make that thumbnail a link to go to the actual post?

The above answer got me to this part. I figured Id share my corrected code.
<?php
$posts = get_posts('cat=3');
foreach ($posts as $post) : setup_postdata( $post );
?>
<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID, 'video', true); ?>/0.jpg" alt="<?php the_title(); ?>" width="200" height="150">
<br />
<?php the_title(); ?>
<?php endforeach; ?>

You could grab the video id by using a custom field and place it inside the Video Thumbnail URL to get the video thumbnail.

Related

How can I embed a background video into my Wordpress theme using HTML5 and without using a plugin?

I'm attempting to add a background video to a wordpress website in place of the large image that is currently there.
I understand how to set up the video with HTML5 and CSS, but am not very familiar with php and am kind of lost on where to begin when it comes to writing a function to pull the video/s (wmp and mp4) from the media library and display them.
Any guidance on how to go about this would be much appreciated. Thanks!
I know that this is the part of the code that needs to be modified:
<div class="intro-block">
<?php if ( has_post_thumbnail() ):?>
<?php the_post_thumbnail('full'); ?>
<?php else: ?>
<?php
$image = get_field('global_intro_image', 'option');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" >
<?php endif; ?>
<?php endif; ?>
<div class="text-holder">
<div class="container">
<?php if( $title = get_field( 'title' )) : ?>
<h1><?php echo $title; ?></h1>
<?php endif; ?>
<?php if( $find_btn_link = get_field( 'find_btn_link' )) : ?>
<?php echo get_field( 'find_btn_text' ); ?>
<?php endif; ?>
</div>
</div>
</div>
It currently pulls the featured image for the page and displays it.
Any suggestions on where to begin?
Thanks in advance!
First you need to find out which part is calling the image. Looks like this part:
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" >
<?php endif; ?>
Then comment out this part or just remove this part of the code and replace if with video tag and style it so it looks just as you need.
Then if you need to be able to indicate videos from wordpress back end, than you need to create options page / or only add fields if you have theme options page.
https://codex.wordpress.org/Creating_Options_Pages
Even easier is to create options pages with ACF pro

issue in displaying blog image on blog page in wordpress

i am having a blog page in my website... There are different posts ... on left side blog image is displayed , on right side text is written..when i click on particular blog, the main page of that post appears with image on the top and text down the image.
The issue is that on my blog page the left side images are not displayed rather default image is displayed.. bt when i click on post.. the main page of that particular post is having image...
<div class="blog-img mainimg" style="">
<?php $blogmainimg = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );?>
<?php if($blogmainimg[0] == '') : ?>
<img src="<?php bloginfo('template_url')?>/images/Noimg.png" />
<?php else : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
</div>
only "if" is working.... else is not working
on blog page, all the posts are having NOIMG.png.. which is wrong
???
Let's try a more simplified solution. This uses the has_post_thumbnail() to check if a featured image is specified, since you really do not need to utilize the wp_get_attachment_image_src() in the provided code.
<div class="blog-img mainimg" style="">
<?php if(has_post_thumbnail($post->ID)) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php bloginfo('template_url')?>/images/Noimg.png" />
<?php endif; ?>
</div>
This uses has_post_thumbnail() to see if the post has one specified, if it does, it will display it uses the_post_thumbnail(). If not, it will revert to the default.
If this doesn't work, than it could be an issue of using the_post_thumbnail(), this would mostly depend on your loop that's displaying the blog posts. If that is the case, try this:
<div class="blog-img mainimg" style="">
<?php if(has_post_thumbnail($post->ID)) : ?>
<?php echo get_the_post_thumbnail( $post->ID, 'single-post-thumbnail' ); ?>
<?php else : ?>
<img src="<?php bloginfo('template_url')?>/images/Noimg.png" />
<?php endif; ?>
</div>
If this still does not solve the issue, please post the loop for your page as well, and I can further troubleshoot it.

Get links and featured image from a specific category

I am trying to create a custom slider in wordpress and add my own links but I am not able to get the featured image of a post to display. Here is my code that I am working with right now.
<?php $posts = get_posts('category=20&orderby=rand&numberposts=6'); foreach($posts as $post) { ?>
<?php echo '<li>'; ?>
<a href="<?php the_permalink() ?>" target="_parent"><?php the_title();?>
<?php wp_get_attachment_image( $attachment->ID, 'small' );?>
</a>
<?php echo '</li>'; ?>
This code works by displaying the links randomly but they never display the featured image of a post. Can someone please help out and let me know what i am doing wrong here...
Thanks
It is because the $attachment variable is null. I'd suggest to replace:
<?php wp_get_attachment_image( $attachment->ID, 'small' );?>
with the following instead:
<?php the_post_thumbnail( 'small' );?>

Wordpress - Trying to show user profile images

I have the Custom User Profile Photo plugin and I was trying to call the profile images to a loop on the mainpage I am creating. If the user has a post, it should display the post and their profile image. I know I am probably doing something really simple wrong, but for the life of me I cannot figure it out. Right now it will show the same image and name, but the post itself is going to the users posts. Forgive me if it's something stupid. It has been quite a while since I have been in php.
<?php
$posts = get_posts('orderby=author&numberposts=10&category_name=userstories');
foreach($posts as $post){
?>
<ul class="random_user_stories">
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$imgURL = get_the_author_meta('cupp_upload_meta',$author->ID);
$name = get_the_author_meta('first_name'). ' ' .get_the_author_meta('last_name');
echo '<img src="'. $imgURL .'" alt=""/><br/>';
echo "$name's Story";
?>
</a>
</li>
</ul>
<?php endwhile; endif; ?>

How to Create Nextgen Custom gallery with album name

i am trying so long to making one normaly gallery useing by Nextgen gallery. but i can't make it yet. i found some way to show gallery thumbnail for speciy gallery and show album name but can't make that dynamicly and show together. bellow some code may be help
for show custom template file.
<?php
/*
Template Name: Gallery
*/
get_header(); ?>
<?php
$NextG = new NextGEN_Shortcodes;
echo $NextG->show_gallery( array("id"=>2,"template"=>"myshow") );
?>
<?php the_post_thumbnail(); ?>
<?php
if ( have_posts() ) while ( have_posts() )
{
the_post();
the_content();
}
?>
<?php get_footer(); ?>
for make custom nextgen gallery file
gallery-mytemplate.php
<ul id="slideshow">
<!-- Thumbnails -->
<?php foreach ( $images as $image ) : ?>
<li>
<a href="<?php echo $image->imageURL ?>" title="<?php echo $image->description ?>" <?php echo $image->thumbcode ?> >
<?php if ( !$image->hidden ) { ?>
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
<?php } ?>
</a>
</li>
<?php endforeach; ?>
</ul>
what i want ?
actuly i want my gallery page will have all gallery name top always and if i click any gallery name then show that gallery. see http://www.evokephotoandvideo.com/2013-photo-gallery/ this url something like that. if can be possible also ajax load like ref. line then would be more nicer...
Simplest way for retrieving data from NextGen is to use NextGEN Gallery Database Class stored in ngg-db.php file. Open that file and there are all functions and explanations for taking the data directly from the database.
Example:
$NextG = new nggdb();
// get all galleries
$NextG->find_all_galleries();
// get single gallery
$NextG->get_gallery( 2 );
Results are arrays, to examine them use print_r or print_r2:
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
That way you can take id or slug from all galleries and create custom url's like http://www.evokephotoandvideo.com/gallery/slug.
Additional info:
WP Rewrite

Categories