2016-09-04 80 views
1

我是wordpress插件開發的新手。我正在嘗試訪問發佈信息,並基於它是否具有精選圖像,我正在創建幻燈片。但圖片網址和帖子標題將從我的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標記中。如果我使用回聲get_permalink(),同樣工作正常。 – Madie

+0

謝謝。更改爲:$ imgurl = wp_get_attachment_url(get_post_thumbnail_id()); $ posturl = get_permalink(); $ title = the_title(); \t $ html。='

'; \t $html .= ''. $title .'
';該網址工作正常,但標題仍然來到相同的地方。 – Madie

回答

1

您使用直接輸出的圖像和標題,而不是將其附加到你的$html變量函數。您需要切換到返回值的函數。

改變圖像來源:

$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

嘗試

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

謝謝哈林,它的工作。你能否看到我的上面的評論,並幫助我的標題以及。 – Madie