代码之家  ›  专栏  ›  技术社区  ›  Matrym

wordpress:wp-get-u-attachment-u-thumb-url

  •  1
  • Matrym  · 技术社区  · 15 年前

    似乎没有办法在主题中提及“大尺寸”、“中尺寸”或“小尺寸”图像。使这一问题复杂化的是,它们的命名约定是100 x????文件名格式,防止对引用进行硬编码。有人知道任何解决方案吗?

    3 回复  |  直到 15 年前
        1
  •  4
  •   Doug Neiner    15 年前

    您要使用的函数是 wp_get_attachment_image_src ,但首先要传递一个 id 一个有效的附件ID。下面是一个例子,说明如何将文章中的第一个附件拉回,它试图按“菜单顺序”对其进行排序,这是在“添加媒体”弹出窗口的“库”选项卡中重新排序项目时设置的顺序:

    <?php
      function get_attached_images(){
        // This function runs in "the_loop", you could run this out of the loop but
        // you would need to change this to $post = $valid_post or something other than
        // using the global post declaration.
        global $post; 
        $args = array(
          'post_type' => 'attachment',
          'numberposts' => 1,
          'post_status' => null,
          'post_parent' => $post->ID,
          'order' => 'ASC',
          'orderby' => 'menu_order'
          ); 
        $attachment = get_posts($args); // Get attachment
        if ($attachment) {
          $img = wp_get_attachment_image_src($attachment[0]->ID, $size = 'full'); ?>
          <img alt="<?php the_title(); ?>" src="<?php echo $img[0] ; ?>" width="<?php echo $img[1] ?>" height="<?php echo $img[2] ?>"/>
      <?php }
      }
    ?>
    

    最重要的是你可以通过 "thumbnail" ,请 "medium" , "large" "full" 与“添加媒体”框中的大小相同。此外,它还返回一个数组:

    [0] => url
    [1] => width
    [2] => height
    

    编辑: 您可以在WordPress后端的“system->media”下自定义WordPress创建的大小。

        2
  •  2
  •   Doug Neiner    15 年前

    要使用wp_get_attachment_thumb_url:

    <?php
    echo wp_get_attachment_thumb_url( $post->ID );
    ?>
    

    注意:您必须在循环内部使用上面的代码片段,以便可以获得$post变量。

        3
  •  1
  •   Matrym    15 年前

    好吧,供大家将来参考…使用WordPress2.9的新缩略图功能,您可以指定不同的图像大小,如下所示:

    <?php the_post_thumbnail('thumbnail'); ?>
    <?php the_post_thumbnail('medium'); ?>
    

    等。

    叹息。这是我想象中的一个“该死”的时刻,每个搜索和找到这个页面的人都会出现。

    DougNeiner和Silent,非常感谢你们的想法和答案。我会为你的努力+1,但结果证明答案比我们想象的要简单。