How to display Multiple Post Thumbnails plugin custom post thumbnail image in custom template

北城以北 提交于 2019-12-11 15:01:31

问题


I have used wordpress Multiple Post Thumbnails plugin to add custom post meta box image field.

if (class_exists('MultiPostThumbnails')) { new MultiPostThumbnails( array( 'label' => 'Homepage Full-Width', 'id' => 'homepage-full-width', 'post_type' => 'post' ) ); }

I want to display this new featured image with full size in my site Home page big banner default featured image (http://nimb.ws/YelErQ).

Right now theme above post featured image display using following code in theme front-page.php file.

<?php $lead_article = ot_get_option('lead_article'); if (has_post_thumbnail($lead_article)) : $lead_article_img_horizontal = wp_get_attachment_image_src(get_post_thumbnail_id($lead_article), "tol-horizontal"); $lead_article_img_vertical = wp_get_attachment_image_src(get_post_thumbnail_id($lead_article), "tol-vertical"); $lead_article_img_horizontal_src = esc_url( $lead_article_img_horizontal[0]); $lead_article_img_vertical_src = esc_url( $lead_article_img_vertical[0]); ?>

<div id="lead-article" class="post featured-style10 post-header">

<div data-interchange="[<?php echo $lead_article_img_horizontal_src ?>, landscape], [<?php echo $lead_article_img_vertical_src ?>, portrait]" class="parallax_bg skrollable skrollable-between" data-bottom-top="transform: translate3d(0px, -20%, 0px);" data-top-bottom="transform: translate3d(0px, 20%, 0px);" style="transform: translate3d(0px, 0.382158%, 0px);background-image: url(<?php echo $lead_article_img_horizontal_src ?>)"></div>

</div>

<?php endif; ?>

I have follow plugin instruction https://github.com/voceconnect/multi-post-thumbnails/wiki but that can't work in my front-page.php file.

So any one know solutions for this then please help me.

Thanks.


回答1:


According to the documentation, all you need to do is add this code to your template to display your custom thumbnail:

<?php if (class_exists('MultiPostThumbnails')) :
MultiPostThumbnails::the_post_thumbnail(
    get_post_type(),
    'homepage-full-width'
);
endif; ?>

Note that for this code to work as is you need to put it somewhere inside The Loop.

Update:

Assuming ot_get_option('lead_article') returns an ID, try this:

<?php
$lead_article = ot_get_option('lead_article');
$lead_article_img_horizontal = null;
$lead_article_img_vertical = null;

if (
    class_exists('MultiPostThumbnails') 
    && MultiPostThumbnails::has_post_thumbnail( get_post_type($lead_article), 'homepage-full-width', $lead_article )
):
    $lead_article_img_horizontal = MultiPostThumbnails::get_post_thumbnail_url( get_post_type($lead_article), 'homepage-full-width', $lead_article );
    $lead_article_img_vertical = esc_url( wp_get_attachment_image_src(get_post_thumbnail_id($lead_article), "tol-vertical")[0] );
// Fallback to featured image if available
elseif ( has_post_thumbnail($lead_article) ):
    $lead_article_img_horizontal = esc_url( wp_get_attachment_image_src(get_post_thumbnail_id($lead_article), "tol-horizontal")[0] );
    $lead_article_img_vertical = esc_url( wp_get_attachment_image_src(get_post_thumbnail_id($lead_article), "tol-vertical")[0] );
endif;

if ( $lead_article_img_horizontal && $lead_article_img_vertical ):
?>
<div id="lead-article" class="post home-featured-post featured-style10 post-header">
    <div data-interchange="[<?php echo $lead_article_img_horizontal ?>, landscape], [<?php echo $lead_article_img_vertical ?>, portrait]" class="parallax_bg skrollable skrollable-between" data-bottom-top="transform: translate3d(0px, -20%, 0px);" data-top-bottom="transform: translate3d(0px, 20%, 0px);" style="transform: translate3d(0px, 0.382158%, 0px);background-image: url(<?php echo $lead_article_img_horizontal ?>)"></div>
</div>
<?php
endif;
?>


来源:https://stackoverflow.com/questions/48805901/how-to-display-multiple-post-thumbnails-plugin-custom-post-thumbnail-image-in-cu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!