2012-06-24 99 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应用程序编写代码。

相关问题