2016-10-11 4 views
0

결정한 게시물을 표시하는 위젯을 만들고 img (그 나던 일하지 않습니다)를 표시하고 제목, 발췌 부분 및 링크가 잘 작동합니다.get_the_post_thumbnail 자료 Wordpress가 작동하지 않습니다.

$ post-> ID가 정상적으로 작동하는 것을 볼 수 있습니다. "add_theme_support ('post-thumbnails');" 추가되었습니다. WP 버전이 마지막 버전입니다.

echo '<a href="' . get_permalink($post->ID) . '" title="' . apply_filters('widget_title', $post->post_title). '">'; 
    echo get_the_post_thumbnail($post->ID, 'full'); 
    echo '</a>'; 

내가 사진을 볼 수 없습니다 위젯을 실행하고 이것은 HTML 인 경우 :

if (!defined('ABSPATH')) 
die('-1'); 


add_action('widgets_init', function(){ 
    register_widget('Latest_News_Widget'); 
}); 

/** 
* Adds Latest News Widget. 

*/ 
class Latest_News_Widget extends WP_Widget { 

/** 
* Register widget with WordPress. 
*/ 
function __construct() { 
    parent::__construct(
     'Latest_News_Widget', // Base ID 
     __('Latest News Widget', 'text_domain'), // Name 
     array('description' => __('Latest News Widget!', 'text_domain'),) // Args 
    ); 
} 

/** 
* Front-end display of widget. 
* 
* @see WP_Widget::widget() 
* 
* @param array $args  Widget arguments. 
* @param array $instance Saved values from database. 
*/ 
public function widget($args, $instance) { 

    // get the excerpt of the required story 
    if ($instance['story_id'] == 0) { 

     $gp_args = array(
      'posts_per_page' => -1, 
      'post_type' => 'post', 
      'orderby' => 'post_date', 
      'order' => 'desc', 
      'post_status' => 'publish' 
     ); 

     $posts = get_posts($gp_args); 

     if ($posts) { 
      $post = $post[0]; 
     } else { 
      $post = null; 
     } 

    } else { 

     $post = get_post($instance['story_id']); 

    } 

    if (array_key_exists('before_widget', $args)) echo $args['before_widget']; 

    if ($post) { 

     if (has_post_thumbnail($post->ID)) {   
    echo '<div class="latest_news-image">'; 
    echo '<a href="' . get_permalink($post->ID) . '" title="' . apply_filters('widget_title', $post->post_title). '">'; 
    echo get_the_post_thumbnail($post->ID, 'full'); 
    echo '</a>'; 
    echo '</div>'; 
    } 

    echo '<div class="latest_news-text">'; 
     echo '<h3 class="story_widget_title">' . apply_filters('widget_title', $post->post_title). '</h3>'; 
     echo '<p class="story_widget_excerpt">' . $post->post_excerpt . '</p>'; 
     echo '<a href="' . get_permalink($post->ID) . '" class="story_widget_readmore" title="Read the post, ' . $post->post_title . '">READ MORE</a>'; 
     echo '</div>'; 

    } else { 

     echo __('No recent post found.', 'text_domain'); 
    } 

    if (array_key_exists('after_widget', $args)) echo $args['after_widget']; 
} 

/** 
* Back-end widget form. 
* 
* @see WP_Widget::form() 
* 
* @param array $instance Previously saved values from database. 
*/ 
public function form($instance) { 

    if (isset($instance[ 'story_id' ])) { 
     $story_id = $instance[ 'story_id' ]; 
    } 
    else { 
     $story_id = 0; 
    } 
    ?> 

    <p> 
     <label for="<?php echo $this->get_field_id('story_id'); ?>"><?php _e('Story:'); ?></label> 

     <select id="<?php echo $this->get_field_id('story_id'); ?>" name="<?php echo $this->get_field_name('story_id'); ?>"> 
      <option value="0">Most recent</option> 
    <?php 
    // get the exceprt of the most recent story 
    $gp_args = array(
     'posts_per_page' => -1, 
     'post_type' => 'post', 
     'orderby' => 'post_date', 
     'order' => 'desc', 
     'post_status' => 'publish' 
    ); 

    $posts = get_posts($gp_args); 

     foreach($posts as $post) { 

      $selected = ($post->ID == $story_id) ? 'selected' : ''; 

      if (strlen($post->post_title) > 30) { 
       $title = substr($post->post_title, 0, 27) . '...'; 
      } else { 
       $title = $post->post_title; 
      } 

      echo '<option value="' . $post->ID . '" ' . $selected . '>' . $title . '</option>'; 

     } 

    ?> 
     </select> 
    </p> 
    <?php 
} 

/** 
* Sanitize widget form values as they are saved. 
* 
* @see WP_Widget::update() 
* 
* @param array $new_instance Values just sent to be saved. 
* @param array $old_instance Previously saved values from database. 
* 
* @return array Updated safe values to be saved. 
*/ 
public function update($new_instance, $old_instance) { 

    $instance = array(); 
    $instance['story_id'] = (! empty($new_instance['story_id'])) ? strip_tags($new_instance['story_id']) : ''; 
    return $instance; 
} 

} // class Latest News Widget 

편집 : 1 :

$image_id = get_post_thumbnail_id($post->ID); 
    echo $image_id; 
    echo the_post_thumbnail($image_id, 'full'); 

<div class="latest_news-image"><a href="http://localhost/whatever/springs-is-in-the-headline/" title="Springs is in the headline"><img src="" alt="Home" class="thumbnail"></a></div> 

이 위젯 코드입니다

번호 (79)를 볼 수 있지만 여전히 비었다! :(

편집 2 :

은 내가해야 "수동"

$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); 
    echo '<img src="'.$thumbnail_src[0].'" alt="' . apply_filters('widget_title', $post->post_title). '" height="200" width="200">'; 

하지만이 방법처럼 해달라고 작동하는지 확인하려면

+0

https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/ –

+0

코드가 작동해야합니다. 게시 이미지의 설정을 해제하고 새 이미지를 설정하십시오. 어쩌면 이상한 버그 일 수도 있습니다 ... –

답변

0

을 코드 아래

으로 봅니다.
`$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size");` 

if ($thumbnail_src) : ?> 
    <img src="<?php echo $thumbnail_src[0]; ?>" width="<?php echo $thumbnail_src[1]; ?>" height="<?php echo $thumbnail_src[2]; ?>" /> 
<?php endif; ?> 

홈 페이지, 카테고리 페이지 또는 기타와 같이 루프 내에서 미리보기 이미지를 가져 오려고하므로 용어 페이지. 당신은 the_post_thumbnail();

단일 페이지와 같은 특정 페이지에 표시하려면 get_post_thumbnail_id ($ postId); 또한

당신이 그렇지 않으면 당신은 워드 프레스 템플릿 태그 기능을위한 POSTDATA이 작동하도록 설정 할 필요가

+0

업데이트가 있습니까? –

+0

@adrianVazquez 나는 확실히 작동 할 답변을 업데이트했습니다. –

+0

이 코드를 넣었습니다.이 html 검색입니다. Array

0

빈 돌아갑니다 이미지를 추가했는지 확인하십시오. 때로는 사용하기에 충분하지 않은 경우도 있습니다. global $post;

setup_postdata($post);을 입력하고 완료하면 wp_reset_postdata();을 사용하여 원래의 게시물이나 페이지로 재설정합니다.

위젯 코드에 WordPress 루프가 없으므로 전역 게시 개체 만 사용하려고합니다.위젯 코드의

예, 당신의 if($post){...} 부분이 교체 :

if ($post) { 

    // Setup the $post so you can use template functions like: the_permalink(); etc... 
    setup_postdata($post); 

    if (has_post_thumbnail()) {   
    echo '<div class="latest_news-image">'; 
    echo '<a href="' . get_permalink() . '" title="' . apply_filters('widget_title', get_the_title()). '">'; 
    echo get_the_post_thumbnail(get_the_ID(), 'full'); 
    echo '</a>'; 
    echo '</div>'; 
    } 

    echo '<div class="latest_news-text">'; 
    echo '<h3 class="story_widget_title">' . apply_filters('widget_title', get_the_title()). '</h3>'; 
    echo '<p class="story_widget_excerpt">' . get_the_excerpt() . '</p>'; 
    echo '<a href="' . get_permalink() . '" class="story_widget_readmore" title="Read the post, ' . get_the_title() . '">READ MORE</a>'; 
    echo '</div>'; 


    // Reset the postdata so we avoid conflicts 
    wp_reset_postdata(); 
} 

관련 링크() 함수를 setup_postdata()와 wp_reset_postdata에 대한 자세한 내용은 :

문제가있는 경우 :

  • 게시물에 실제로 미리보기 이미지가 있음을 확인하십시오.
  • 이미지 경로를 확인하십시오. 브라우저에서 직접로드 할 수 있습니까?
  • 다른 이미지와 미리보기 이미지 테스트
  • 이미지 파일 이름에 멀티 바이트 문자를 사용하지 마십시오. 이로 인해 여러 번 이미지가 깨졌습니다.
관련 문제