【问题标题】:How can I check for a thumbnail in WordPress?如何在 WordPress 中检查缩略图?
【发布时间】:2012-03-07 11:35:12
【问题描述】:

我如何检查帖子是否有缩略图以及是否有什么作用?如果不做其他事情。这就是我所拥有的:

        <?php if(have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>

                <?php if ( has_post_thumbnail() ) { ?>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php 
                }else{ 
                ?>
                    <?php the_post_thumbnail(); ?> 
                <?php
                } 
                ?>  

            <?php endwhile; ?>

        <?php endif; ?>

任何帮助将不胜感激。

【问题讨论】:

  • 我运行它时没有任何显示。我尝试了不同的东西,比如 the_thumbnail,但仍然没有显示
  • 你确定它在循环中吗?
  • 用循环发布整个代码集。
  • 好的,我刚刚也发布了带有循环的代码。

标签: php wordpress wordpress-theming


【解决方案1】:

你已经有了这个,在行中

if ( has_post_thumbnail() )

您正在检查帖子是否有缩略图,问题是您在 else 语句中输入了错误的代码,您必须输入以下内容:

  <?php if ( has_post_thumbnail() ) { ?>
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      <?php the_post_thumbnail(); ?> 
      HAVE THUMBNAIL DO SOMETHING
  <?php 
      }else{ 
  ?>
      DOESN'T HAVE THUMBNAIL : DO SOMETHING ELSE
      <?php
  } 
  ?>  

【讨论】:

    【解决方案2】:

    试试下面这行代码:

        <?php if(has_post_thumbnail())
            { 
            ?>
                <img src="<?php the_post_thumbnail_url(); ?>" id="contextual" class="contextual" alt="" />
    
            <?php 
            }
    else{       
            ?>
            <img src="<?php echo get_template_directory_uri(); ?>/design/images/i-default.jpg" id="contextual" class="contextual" alt="" />
    <?php } ?>
    

    【讨论】:

      【解决方案3】:

      要将帖子缩略图链接到特定循环中的帖子固定链接,请在主题的模板文件中使用以下内容:

      <?php if ( has_post_thumbnail() ) : ?>
          <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
              <?php the_post_thumbnail(); ?>
          </a>
      <?php endif; ?>
      

      【讨论】:

        【解决方案4】:

        首先检查你的functions.php文件

        if (function_exists('add_theme_support')) {
          add_theme_support('post-thumbnails');
        }
        

        如果它不在那里,请将其复制并粘贴到您的文件中..

        其次将其添加到您的functions.php 这使您可以返回 Image src,而不仅仅是打印整个 img 标签

        function get_the_post_thumbnail_url( $post_id = NULL ) {
            global $id;
            $post_id = ( NULL === $post_id ) ? $id : $post_id;
            $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
            $src = $src[0];
            return $src;
        }
        

        然后在您的模板页面上将您的代码更改为: 这被用作背景图片

        <?php if ( has_post_thumbnail() ) { ?>
            <div id="slider" style="background-image:url(<?php echo get_the_post_thumbnail_url($post->ID, 'large'); ?>); background-position: center center;">  
            </div>                
        <?php 
        }else{ 
        ?>
            <img src="<?php bloginfo('template_directory');?>/images/blank.jpg" alt="" /> 
        <?php
        } 
        ?> 
        

        这应该产生一个应用了背景图像的 div,

        如果您想打印完整的 img 标签代码,只需使用以下方法之一。

        if (has_post_thumbnail()) { 
        ?>
            <?php the_post_thumbnail();            // just the image        ?>
            <?php the_post_thumbnail('thumbnail'); // just the thumbnail    ?>
            <?php the_post_thumbnail('medium');    // just the Medium Image ?>
            <?php the_post_thumbnail('large');     // just the Medium Image ?>
            <?php 
            // adding a 200x200 height and width along with a class to it.
                the_post_thumbnail(array( 200,200 ), array( 'class' => 'alignleft' )); 
            ?>
            <?php 
            // Adding a few classes to the medium image
                the_post_thumbnail('medium', array('class' => 'alignleft another_class')); 
            ?>
        
        <?php
        }
        

        马蒂..

        【讨论】:

          猜你喜欢
          • 2011-04-06
          • 2015-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-23
          • 1970-01-01
          • 2012-10-14
          相关资源
          最近更新 更多