0

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(); ?>
4

2 に答える 2

1

wp_attachment_is_imageを使用して、画像と、添付ファイルIDと「full」をパラメーターとして持つwp_get_attachment_image_srcのみを取得していることを確認する、返された配列の最初の要素からフルサイズの添付画像のURLを取得できます(これはテストされていません)しかし、それは実行可能のようです)。

于 2012-10-17T14:17:41.607 に答える
0

@ "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(); ?>
于 2012-10-17T14:51:50.807 に答える