简体   繁体   English

在侧边栏中显示最近的帖子,并在 wordpress 中显示帖子缩略图

[英]display recent post in sidebar with post thumbnail in wordpress

I am trying to display recent post with thumbnail,title and author name on by website's blog details page's sidebar .我试图通过网站的博客详细信息页面的侧边栏显示最近的帖子,其中包含缩略图、标题和作者姓名。 title and author is proper but somehow thumbnail image is not showing proper and also when I check in view source, it's not taking thumbnail size 150*150, it's taking image's original size.标题和作者是正确的,但不知何故缩略图显示不正确,而且当我检查查看源时,它没有采用 150 * 150 的缩略图尺寸,而是采用图像的原始尺寸。

check url:- https://stageserver.co/officework/dev/6-lead-generation-techniques-to-immediately-turn-your-business-into-a-sales-magnet-9-3/检查网址:- https://stageserver.co/officework/dev/6-lead-generation-techniques-to-immediately-turn-your-business-into-a-sales-magnet-9-3/

This is the code that I tried:-这是我尝试过的代码:-

<!-- Latest News -->
                    <?php $orig_post = $post;
   global $post;
   $tags = wp_get_post_tags($post->ID);
  if ($tags) {
  $tag_ids = array();
  foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  $args=array(
  'tag__in' => $tag_ids,
  'post__not_in' => array($post->ID),
  'posts_per_page'=>5, // Number of related posts that will be shown.
  'caller_get_posts'=>1
  );
  $my_query = new wp_query( $args );
  if( $my_query->have_posts() ) {

   echo '<div class="sidebar-widget latest-news"><div class="sidebar-title">
                            <h2>Recent Post</h2>
                        </div>';

  while( $my_query->have_posts() ) {
  $my_query->the_post(); ?>

 <div class="widget-content">
 <article class="post">
 <div class="post-thumb">
    <a href="<?php the_permalink(); ?>" ><img src="<?php 
     the_post_thumbnail(); ?>" ></img></a>
</div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="post-info"><?php the_author(); ?></div>
 </article>
 </div>
 <? }
echo '</div>';
 }
 }
 $post = $orig_post;
 wp_reset_query(); ?>

thanks in advance pls help as soon as possible I am new in php提前致谢,请尽快帮助我 我是 php 新手

You can pass the size and other attribute to the the_post_thumbnail( $size, $attr );您可以将大小和其他属性传递给 the_post_thumbnail( $size, $attr ); function, Default value: 'post-thumbnail'.函数,默认值:'post-thumbnail'。

Try:尝试:

the_post_thumbnail('thumbnail');


the_post_thumbnail('thumbnail'); // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium'); // Medium resolution (default 300px x 300px max)
the_post_thumbnail('medium_large'); // Medium Large resolution (default 768px x 0px max)
the_post_thumbnail('large'); // Large resolution (default 1024px x 1024px max)
the_post_thumbnail('full'); // Original image resolution (unmodified)

the_post_thumbnail( array(100,100) ); // Other resolutions

Ok so also the_post_thumbnail() return the image not just the url, so try:好的,the_post_thumbnail() 也返回图像而不仅仅是 url,所以尝试:

<div class="post-thumb">
    <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail('thumbnail'); ?>" >
    </a>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM