WP_QUERY获取较大尺寸的附件

时间:2015-06-10 作者:Emir Dupovac

我正在使用wp\\u query获取所有附件,它可以正常工作,但我想知道如何获取完整大小的附件,因为此查询返回的是中等大小的图像。

<?php 
    $args = array(\'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'posts_per_page\' => 5 );
    $wp_query = new WP_Query($args);
    while (have_posts() ) : the_post(); ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php
                the_title( \'<h2>\', \'</h2>\' );
                the_content();
            ?>
        </article>

    <?php endwhile; ?>

3 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

如果您查看原始post数据。。。

while (have_posts() ) { 
  the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php
      var_dump($post); ?>
  </article><?php 
}
你会看到里面没有什么可以指定“中等”,所以我去看了看。There is a filter on the_content...

add_filter( \'the_content\', \'prepend_attachment\' );
。。。那个juggles data for "attachment" post types.

1576    function prepend_attachment($content) {
1577            $post = get_post();
1578    
1579            if ( empty($post->post_type) || $post->post_type != \'attachment\' )
1580                    return $content;
1581    
1582            if ( wp_attachment_is( \'video\', $post ) ) {
1583                    $meta = wp_get_attachment_metadata( get_the_ID() );
1584                    $atts = array( \'src\' => wp_get_attachment_url() );
1585                    if ( ! empty( $meta[\'width\'] ) && ! empty( $meta[\'height\'] ) ) {
1586                            $atts[\'width\'] = (int) $meta[\'width\'];
1587                            $atts[\'height\'] = (int) $meta[\'height\'];
1588                    }
1589                    if ( has_post_thumbnail() ) {
1590                            $atts[\'poster\'] = wp_get_attachment_url( get_post_thumbnail_id() );
1591                    }
1592                    $p = wp_video_shortcode( $atts );
1593            } elseif ( wp_attachment_is( \'audio\', $post ) ) {
1594                    $p = wp_audio_shortcode( array( \'src\' => wp_get_attachment_url() ) );
1595            } else {
1596                    $p = \'<p class="attachment">\';
1597                    // show the medium sized image representation of the attachment if available, and link to the raw file
1598                    $p .= wp_get_attachment_link(0, \'medium\', false);
1599                    $p .= \'</p>\';
1600            }
1601    
1602            /**
1603             * Filter the attachment markup to be prepended to the post content.
1604             *
1605             * @since 2.0.0
1606             *
1607             * @see prepend_attachment()
1608             *
1609             * @param string $p The attachment HTML output.
1610             */
1611            $p = apply_filters( \'prepend_attachment\', $p );
1612    
1613            return "$p\\n$content";
1614    }
相关行为1597和1598。wp_get_attachment_link(), 反过来使用wp_get_attachment_image() (第1545行),其中使用image_downsize()... 还有一个很有前途的过滤器:

167         if ( $out = apply_filters( \'image_downsize\', false, $id, $size ) ) {
168                 return $out;
169         }
等一下中提琴

function image_size_hack() {
  $img_url = wp_get_attachment_url();
  $meta = wp_get_attachment_metadata();
  $width= $height = \'\';
  if (!empty($meta[\'width\'])) {
    $width = $meta[\'width\'];
  }
  if (!empty($meta[\'height\'])) {
    $width = $meta[\'height\'];
  }
  return array( $img_url, $width, $height, false );
}
add_filter( \'image_downsize\',  \'image_size_hack\');

SO网友:Chinmay Chiranjeeb

您可以根据需要自定义wp\\u get\\u attachment\\u image功能。传递要显示的图像大小。

wp_get_attachment_image( $attachment->ID, \'full\' );
更多信息,请访问:https://codex.wordpress.org/Function_Reference/wp_get_attachment_image

SO网友:iridian

您应该能够设置自己的图像标记。您可以替换_content();用这个。

<img src="<?= wp_get_attachment_url(get_the_ID()); ?>"/>

结束

相关推荐

delete post also attachments

我正在尝试删除包含所有附件的帖子。这就是我现在想到的函数;function remove_post(){ if(isset($_POST[\'post_id\']) && is_numeric($_POST[\'post_id\'])){ $post = get_post($_POST[\'post_id\']); if(get_current_user_id() == $pos