2016-09-04 13 views
1

私は初心者です。投稿情報にアクセスしようとしていて、それが特集された画像を持っている場合は、スライドショーを作成しています。しかし、画像のURLと投稿のタイトルはdivタグから外れています。以下は私のコードです。wordpressプラグインアクセス投稿情報

//function to display post info 
    function postsinfo_collage_display($attr, $content) { 

    $plugins_url = plugins_url(); 

    $html = '<div id="pscanvas" class="row"> 
        <div class="row__inner mThumbnailScroller">'; 

    // The Query 
    query_posts(array ('orderby' => 'date')); 

    // The Loop 
    while (have_posts()) : the_post(); 
    if (has_post_thumbnail()) { 
     $html .= '<div class="tile"> 
          <div class="tile__media">'; 
     $html .='<a href="'.esc_url(get_permalink()).'">'; 
     $html .='<img class="tile__img"'; 
     $html .="src='".esc_url(the_post_thumbnail_url()). "'/>"; 
     $html .='</a></div><div class="tile__details"> 
       <div class="tile__title">'; 
     $html .= the_title(); 
     $html .='</div></div></div>';   
    } 
    endwhile; 
    // Reset Query 
    wp_reset_query(); 
    $html .= '</div> 
        </div> 
        <script> 
        window.jQuery || document.write(\'<script src="minified/jquery-1.11.0.min.js"><\/script>\') 
      (function($){ 
       $(window).load(function(){    
       }); 
      })(jQuery);</script>'; 

     return $html; 

    } 

私は取得しています出力は次のようになります。

<div class="entry-content"> 
      http://localhost:9085/wordpress/wp-content/uploads/2016/04/call-of-duty-ghosts-20517-1366x768-1200x675.jpgpscanvashttp://localhost:9085/wordpress/wp-content/uploads/2016/04/Call-Of-Duty-Ghost-Background-For-Wallpaper-652-1200x675.jpgkwofkwc 
     <div id="pscanvas" class="row"> 
     <div class="row__inner mThumbnailScroller "> 
      <div id="mTS_1" class="mTSWrapper mTS_horizontal"> 
       <div class="mTSAutoContainer mTSContainer" "> 
        <div class="tile"> 
        <div class="tile__media"> 
        <a href="http://localhost:9085/wordpress/2016/09/04/pscanvas/"> 
        <img class="tile__img" src=""></a> 
        </div><div class="tile__details"> 
        <div class="tile__title"></div></div></div> 
        <div class="tile"><div class="tile__media"> 
        <a href="http://localhost:9085/wordpress/2016/05/03/kwofkwc/"> 
        <img class="tile__img" src=""></a></div><div class="tile__details"> 
        <div class="tile__title"></div></div></div> 
        </div> 
       </div> 
      </div> 
     </div> 
      <script> 
      window.jQuery || document.write('<script src="minified/jquery-1.11.0.min.js"><\/script>') 
      (function($){ 
       $(window).load(function(){    
       }); 
      })(jQuery);</script> 
    <strong>Album Rating:</strong> <div class="genericon genericon-star"></div><br> </div> 

私は画像のURLは、imgタグ内にある必要があります見ることができますが、その全体のdivタグの出てきます。

+2

使用:https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/ – huhu

+0

を私は初めに、同様ことを試してみました。しかし、同じ出力。なぜ私のメインのpscanvas divタグからURLが出てくるのか理解できません。 echo get_permalink()を使っても同じことがうまくいきます。 – Madie

+0

ありがとうございます。これに変更されました:$ imgurl = wp_get_attachment_url(get_post_thumbnail_id()); $ posturl = get_permalink(); $ title = the_title(); \t $ html。= '

'; \t $html .= ''. $title .'
'; URLは正常に動作していますが、タイトルはまだ同じ場所に来ています。 – Madie

答えて

1

を試してみてください。代わりに値を返す関数に切り替える必要があります。

変更画像から:

$html .="src='".esc_url(the_post_thumbnail_url()). "'/>"; 

へ:

$html .= "src='" . esc_url(get_the_post_thumbnail_url()) . "'/>"; 

とタイトルから:

$html .= the_title(); 

へ:

$html .= get_the_title(); 

ドキュメント:

https://developer.wordpress.org/reference/functions/get_the_title/ https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

+0

理由を説明してくれたNathanに感謝します。あなたはアンカーを手伝ってください。クリックすると何もしません。 – Madie

+1

喜んで私は助けることができました:)アンカーについては、私はあなたに明確な方向を与えるには余りにも多くの可能性があることを恐れています。 JavaScriptを使って何かしていますか?コンソールのエラー?要素インスペクタでリンクが正しく表示されていることを確認しましたか?別の要素をその上に配置するなど、クリックされないようにスタイリングしていますか?あなたのコードでjQueryをロードするために使用されるコードは、途中で進む必要があります。 WordPressはそれをあなたに提供します。 –

+0

アンカータグはdivタグ全体をカバーすると考えられています。ちょうどpscanvas divの上にアンカーを移動しました。どうもありがとう :)。 – Madie

1

は、あなたがあなたの$html変数に追加直接出力画像とタイトルではなく機能を使用している

$url = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
<img src="<?php echo $url; ?>"/> 
+0

ありがとうHaring、それは働いた。あなたは私の上記のコメントを見て、タイトルにも私を助けてくれますか? – Madie