问题
I am trying to use external image as featured image in wordpress. All is done correctly but there is just one problem. The actual external image show only when there is any featured image set via the WP admin. I don't want to set any featured image but still want the featured image from that external URL which is set via a custom field.
I hope you guys got it. Here is the code of my content.php and the external featured image is triggered by get_post_meta($post->ID, "external_featured", true);
<article <?php post_class('single-entry clearfix'); ?>>
<?php if( has_post_thumbnail() ) { ?>
<div class="single-entry-thumbnail view view-first">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo get_post_meta($post->ID, "external_featured", true); aq_resize( wp_get_attachment_url( get_post_thumbnail_id(), 'full' ), wpex_img( 'blog_entry_width' ), wpex_img( 'blog_entry_height' ), wpex_img( 'blog_entry_crop' ) ); ?>" alt="<?php echo the_title(); ?>" /></a>
<div class="mask">
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p>
<?php
if( !empty($post->post_excerpt) ) {
the_excerpt();
} else {
echo wp_trim_words(get_the_content(), 20); }
?>
</p>
<a href="<?php the_permalink(); ?>" class="info">Download</a>
</div>
</div><!-- /single-entry-thumbnail -->
<?php } ?>
</article><!-- /single-entry -->
Thank you :)
回答1:
Try this:-
<article <?php post_class('single-entry clearfix'); ?>>
<div class="single-entry-thumbnail view view-first">
<?php $custom_featured_image_url = get_post_meta($post->ID, "external_featured", true); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $custom_featured_image_url; aq_resize( $custom_featured_image_url, wpex_img( 'blog_entry_width' ), wpex_img( 'blog_entry_height' ), wpex_img( 'blog_entry_crop' ) ); ?>" alt="<?php echo the_title(); ?>" /></a>
<div class="mask">
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p>
<?php
if( !empty($post->post_excerpt) ) {
the_excerpt();
} else {
echo wp_trim_words(get_the_content(), 20); }
?>
</p>
<a href="<?php the_permalink(); ?>" class="info">Download</a>
</div>
</div><!-- /single-entry-thumbnail -->
</article><!-- /single-entry -->
来源:https://stackoverflow.com/questions/21228014/featured-image-from-external-url-not-working-properly