2012-06-24 89 views
1

我正在使用以下代碼將隨機發布數據拖放到我的博客的側欄上。我將如何添加從該帖子的自定義字段「athletethumbnail」中拉出的圖像? :從Wordpress帖子的自定義字段中顯示圖像

<?php 
global $wp_query; 
$current_id = $wp_query->get_queried_object_id(); 

    $my_query = new WP_Query(array(
     'post_type'=>'athletes', 
     'posts_per_page'=>'1', 
     'category' => '36' , 
     'orderby' =>'rand' 
     )); 

     if($my_query->have_posts()) { 
     while ($my_query->have_posts()) : $my_query->the_post(); 

?> 
<?php the_title(); ?> 

<?php 
endwhile; 
} 

wp_reset_query(); 
?> 

回答

2

docs found here,在PostMeta功能

These functions are intended for use inside The Loop, and all return arrays. 

get_post_custom() 
Get all key/value data for the current post. 
................ 

解決方法:應使用get_post_custom()

試試這個:

$custom = get_post_custom(the_ID()); 
echo $athletethumbnail = $custom["athletethumbnail"][0]; 

注意:您也應該能夠逃脫,而沒有經過POST ID,如get_post_custom調用不通過get_the_id ID後的ID。 source here

變化後:

<?php 
    global $wp_query; 
    $current_id = $wp_query->get_queried_object_id(); 

     $my_query = new WP_Query(array(
      'post_type'=>'athletes', 
      'posts_per_page'=>'1', 
      'category' => '36' , 
      'orderby' =>'rand' 
      )); 

      // The Loop 
while ($the_query->have_posts()) : $the_query->the_post(); ?> 

<?php 
$custom = get_post_custom(the_ID()); 
echo $athletethumbnail = $custom["athletethumbnail"][0]; 
the_title(); ?> 

<?php 
     endwhile; 

// Reset Post Data 
wp_reset_postdata(); 

?> 
+0

在哪裏可以放置數組? – Sam

+0

你有可能爲我發佈完整的代碼嗎?我似乎無法得到它的工作 – Sam

+0

讓我知道,如果這有幫助嗎? –

0
  1. 登錄到您的Facebook帳戶。

  2. 在您的個人資料頁面的搜索框中輸入「Facebook Developers」。點擊「Facebook.Developers.com」鏈接。

  3. 單擊Facebook Developers菜單欄上的「我的應用程序」鏈接。您可能需要點擊「允許」繼續。

  4. 單擊「開發人員」頁面右上角的「設置新應用程序」按鈕。

  5. 在「應用程序名稱」字段中輸入新Facebook應用程序的名稱。點擊「同意」單選按鈕接受「Facebook條款」,然後點擊「創建應用程序

  6. 鍵入顯示在」安全檢查「框中的文本 - 與屏幕上顯示的內容完全相同 - 輸入點擊「提交」按鈕

  7. 在「描述」字段中輸入您的新應用程序的描述在「語言」下拉框中選擇應用程序的默認語言爲您的「用戶支持地址」選擇「電子郵件」或「網址」選項。這是用戶用於與您的Facebook應用程序問題或支持問題聯繫的地址。

  8. 回車您的用戶支持URL地址或電子郵件地址。輸入您的隱私政策頁面的URL地址。所有Facebook應用程序開發者都必須在其網站上展示隱私政策,以解釋應用程序收集和使用的Facebook用戶信息類型。如果您在使用您的應用程序之前要求用戶接受「服務條款」,請在「服務條款URL」字段中輸入包含協議文本的URL地址。

  9. 單擊「保存更改」按鈕。 Facebook會保存新應用程序的信息更改,並在下一頁顯示摘要信息。摘要頁面在「應用程序ID」標題下顯示新應用程序的Facebook應用程序ID。然後,您就可以開始爲您的新Facebook應用程序編寫代碼。

相關問題