【问题标题】:How to display "full" image URL so I can have a random background in Wordpress如何显示“完整”图像 URL,以便我可以在 Wordpress 中使用随机背景
【发布时间】:2012-10-17 12:51:44
【问题描述】:

我正在尝试编写一个自定义页面类型,它在 Wordpress 中为页面提供随机背景。 (即主页上的漂亮大图)。我已经成功了!但是我返回一张大图,有什么想法可以快速添加使它显示完整的图片大小的 URL?

帮助非常感谢!

(我已将完整代码放入其中,如果有帮助,任何人都可以在自己的网站上使用它?)

            <?php
            /**
             * Template Name: Fullscreen Random
            */

            get_header();

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

            <style media="screen" type="text/css">
            body {
            background: url(<?php
            $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'orderby' => 'rand', 'post_status' => null, 'post_parent' => 5 ); 
            $attachments = get_posts( $args );
            if ($attachments) {
                        foreach ( $attachments as $attachment ) {
                                echo $attachment->guid;
                }
            }
            ?>) no-repeat center center fixed;
            webkit-background-size: cover;
            moz-background-size: cover;
            o-background-size: cover;
            background-size: cover;
            }
            #menu {background:rgba(0, 0, 0, 0.2)!important;}
            * {color:white!important;}
            </style>

            <?php endwhile; ?>
            <? get_footer(); ?>

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    使用wp_attachment_is_image 确保您只获取您的图片,并使用wp_get_attachment_image_src 以附件ID 和“完整”作为参数,您可以从返回的第一个元素获取全尺寸附件图片的URL数组(尚未对此进行测试,但似乎可行)。

    【讨论】:

    • 是的。发现。 wp_get_attachment_image_src 解决了它。将在下面发布我的固定/最终脚本,以便其他人可以看到..
    【解决方案2】:

    对于那些对包含@“Edgar Allan Pwn”建议的最终脚本感兴趣的人,以下是:

    <?php
    /**
     * Template Name: Fullscreen Random
    */
    
    get_header();
    
    ?> 
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    <style media="screen" type="text/css">
    body {
    background: url(<?php
    
        $args = array(
        'orderby'        => 'rand',
        'post_type'      => 'attachment',
        'post_parent'    => $post->ID,
        'post_mime_type' => 'image',
        'post_status'    => null,
        'numberposts'    => 1,
        );
    
        $attachments = get_posts($args);
    
        if ($attachments) {
    
            foreach ($attachments as $attachment) { ?>
    
            <?php $full = wp_get_attachment_image_src( $attachment->ID, 'full', false, false );
    
            echo $full[0];
    
            }
        } 
    
    ?>) no-repeat center center fixed;
    webkit-background-size: cover;
    moz-background-size: cover;
    o-background-size: cover;
    background-size: cover;
    }
    #menu {background:rgba(0, 0, 0, 0.2)!important;}
    * {color:white!important;}
    </style>
    
    <?php endwhile; ?>
    <? get_footer(); ?>
    

    【讨论】:

      猜你喜欢
      • 2019-12-06
      • 2018-09-21
      • 2012-02-01
      • 1970-01-01
      • 2016-10-16
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      相关资源
      最近更新 更多