WordPress:wp_get_attachment_thumb_url

发布于 2024-08-16 10:37:49 字数 93 浏览 3 评论 0原文

似乎无法在主题中引用“大尺寸”、“中尺寸”或“小尺寸”图像。使问题更加复杂的是他们的命名约定 100 x ???文件名格式,防止对引用进行硬编码。有谁知道有什么解决办法吗?

There seems no way of referring to the "big size" "mid size" or "small size" image in a theme. Compounding that problem is their naming convention of 100 x ??? file name format, preventing hard coding a reference. Does anyone know of any solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

上课铃就是安魂曲 2024-08-23 10:37:49

您要使用的函数是 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 }
  }
?>

以下是如何拉回帖子上第一个附件的示例,它尝试按“menu_order”对其进行排序,这是您在“添加媒体”弹出窗口 需要注意的重要一点是,您可以传入 "thumbnail""medium""large""full" 对应于“添加媒体”框中的相同尺寸。此外,它还返回一个数组:

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

编辑:您可以通过在 WordPress 后端的“系统 -> 媒体”下自定义它们来编辑 WordPress 创建的尺寸。

The function you want to use is wp_get_attachment_image_src, but it starts with passing it an id of a valid attachment id. Here is an example of how to pull back the first attachment on a post, and it tries to order it by "menu_order" which is the order set when you reorder items in the "Gallery" tab of the "Add Media" popup:

<?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 }
  }
?>

The important thing to notice is you can pass in "thumbnail", "medium", "large" and "full" which correspond to the same sizes in the Add Media box. Also, it returns an array:

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

EDIT: You can edit which sizes are created by WordPress by customizing them under "System->Media" in the WordPress backend.

笑,眼淚并存 2024-08-23 10:37:49

使用 wp_get_attachment_thumb_url:

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

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

to use wp_get_attachment_thumb_url:

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

NOTE: You MUST use the snippet above, inside the loop, so that you can get $post variable.

昨迟人 2024-08-23 10:37:49

好吧,供大家以后参考...使用 WordPress 2.9 的新缩略图功能,您可以像这样指定不同的图像大小:

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

等等

。唉。我想这是每个搜索并找到此页面的人都会遇到的“呃”时刻之一。

Doug Neiner 和 Silent,非常感谢你们贡献的想法和答案。我会为你的努力+1,但事实证明答案比我们想象的要简单。

Ok, for everyone's future reference... using WordPress 2.9's new thumbnail feature, you can specify different image sizes like so:

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

etc.

Sigh. This is one of those "Duh" moments that I imagine everyone searching and finding this page will come to.

Doug Neiner and Silent, thank you both very much for your contributed thoughts and answers. I'll +1 for your efforts, but it turns out that the answer was simpler than we all thought.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文