カスタムフィールドを介して追加されたURLを含むいくつかの画像を読み込むのが面倒です。
私が使用している遅延ロードプラグインはsrc
属性にプレースホルダーイメージを、data-originalに実際のイメージを要求します。
http://www.appelsiini.net/projects/lazyload
画像の高さと幅も必要なので、wp_get_attachment_image_src()
を使っています。
私の問題は、場所を保持する画像を取得するためにbloginfo('template_directory')
を使うことです。
ここの最初の画像はプレースホルダ画像を表示していませんが、ページへのURLを出力しています。
<?php
$attch_id_1 = pn_get_attachment_id_from_url(get_post_meta($post->ID, 'img1', true));
$image_attributes_1 = wp_get_attachment_image_src( $attch_id_1, 'full');
$attch_id_2 = pn_get_attachment_id_from_url(get_post_meta($post->ID, 'img2', true));
$image_attributes_2 = wp_get_attachment_image_src( $attch_id_2, 'full');
$attch_id_3 = pn_get_attachment_id_from_url(get_post_meta($post->ID, 'img3', true));
$image_attributes_3 = wp_get_attachment_image_src( $attch_id_3, 'full');
echo '<img src="'.bloginfo('template_directory').'"/images/img-BG.png" data-original="'.$image_attributes_1[0].'">';
echo '<img src="http://localhost/wordpress-cd/wp-content/themes/cd/images/img-BG.png" data-original="'.$image_attributes_2[0].'">';
echo '<img src="http://localhost/wordpress-cd/wp-content/themes/cd/images/img-BG.png" data-original="'.$image_attributes_3[0].'">';
?>
ページのソースはこんな感じです。
http://localhost/wordpress-cd/wp-content/themes/cd<img src="/images/img-BG.png"
なぜここでbloginfo('template_directory')
を使えないのですか?
どうやって画像を正しく出力できますか?
Bloginfoそれ自身がecho
を使って文字列を出力するので、あなたがecho
を使って出力している間、あなたはbloginfo()
を使うことができません。以下はあなたのために動作します、あなたは私が削除した余分な二重引用符もあります....
<?php
$attch_id_1 = pn_get_attachment_id_from_url(get_post_meta($post->ID, 'img1', true));
$image_attributes_1 = wp_get_attachment_image_src( $attch_id_1, 'full');
$attch_id_2 = pn_get_attachment_id_from_url(get_post_meta($post->ID, 'img2', true));
$image_attributes_2 = wp_get_attachment_image_src( $attch_id_2, 'full');
$attch_id_3 = pn_get_attachment_id_from_url(get_post_meta($post->ID, 'img3', true));
$image_attributes_3 = wp_get_attachment_image_src( $attch_id_3, 'full');
echo '<img src="'.get_bloginfo('template_directory').'/images/img-BG.png" data-original="'.$image_attributes_1[0].'">';
echo '<img src="http://localhost/wordpress-cd/wp-content/themes/cd/images/img-BG.png" data-original="'.$image_attributes_2[0].'">';
echo '<img src="http://localhost/wordpress-cd/wp-content/themes/cd/images/img-BG.png" data-original="'.$image_attributes_3[0].'">';
?>
これはうまくいくはずです
$so97086_template_directory = get_bloginfo('template_directory');
そして取り替えなさい
bloginfo('template_directory') with $so97086_template_directory;
Get_template_directory_uri()がget_bloginfo( 'template_directory')よりも優先されることに注意してください。
詳細はこちらを参照してください。 get_template_directory()とbloginfo( 'template_directory')とTEMPLATEPATH