在调用‘attachment.php’文件中的媒体(图像)文件时,除了`<?php the_content();?>`,还有什么替代方法?

时间:2017-03-01 作者:Craig

我正在整理我自己的WordPress主题。到目前为止,我一直在用单曲。php文件作为我的博客和媒体文件的模板。关于我的媒体文件,我一直在使用<?php the_content(); ?> 标记以调用此类图像。

我现在已经到了创建“附件”的阶段。各种媒体文件的php文件。我注意到<?php the_content(); ?> 在所述文件中不起作用,不像在“single”中。php文件。

相关替代方案是什么<?php the_content(); ?> 当涉及到调用“附件”中的图像时。php文件?

2 个回复
SO网友:filipecsweb

使命感the_content 从附件中。php将为您提供附件描述。

将下面的代码放在您的页面上,看看您可以从附件中获得什么。php文件:


while ( have_posts() ) : the_post();

    /**
     * @link    https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata#Related
     */
    $data = array(
        get_the_ID(), // Gets att ID.
        get_permalink(), // Gets att page link.
        get_the_title(), // Gets att title.
        get_the_content(), // Gets att description.
        wp_get_attachment_metadata(), // Gets att metadata.
    );

    var_dump( $data );

endwhile;

另外,您可能想看看https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata#Related 并查看其他可能对您有用的功能。

SO网友:ajie

这里有一种方法:

<?php if ( wp_attachment_is_image( $post->id ) ) : $att_image = wp_get_attachment_image_src( $post->id, "full"); ?>

<a href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php the_title(); ?>" rel="attachment">
<img src="<?php echo $att_image[0];?>" width="<?php echo $att_image[1];?>" height="<?php echo $att_image[2];?>" alt="" />
</a>

<?php endif; ?>
在循环中使用它。上面的示例首先检查附件是否为图像。

或者您也可以只使用wp\\u get\\u attachment\\u image()输出整个图像标记:

https://developer.wordpress.org/reference/functions/wp_get_attachment_image/

还可以使用get\\u atached\\u媒体。

https://codex.wordpress.org/Function_Reference/get_attached_media

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请