无法从自定义Wordpress帖子类型获取附件/媒体?

问题描述:

我试图在我插入到管理界面的测试文章中的Wordpress php代码中获取附加/插入的图像。我点击了“添加媒体”按钮并上传了我的照片并更新了帖子(其中front-page是自定义帖子类型)。无法从自定义Wordpress帖子类型获取附件/媒体?

(我点击此按钮):

enter image description here

不过,我似乎无法检索与该职位为我的生活相关联的图片。如果我设置了Feature Image(缩略图),我可以获取该图像,但不能插入图像。以下是我已经试过:

循环通过附件(不走运):

$attachments = query_posts(
       array(
       'post_type' => 'front-page', // only get "attachment" type posts 
       'post_parent' => $post->ID, // only get attachments for current post/page 
       'posts_per_page' => -1  // get all attachments 
      ) 
      ); 
foreach($attachments as $attachment) { 
    echo get_the_ID(); 
    echo wp_get_attachment_image($attachment->ID, 'full'); 
} 

wp_attached_image(文章ID)(不走运):

wp_get_attachment_image($post->ID); 

让所有帖子(没运气):

<?php 
$args = array( 
    'post_type' => 'front-page', 
    'post_mime_type' => 'image', 
    'numberposts' => -1, 
    'post_status' => null, 
    'post_parent' => $post->ID 
); 
$attached_images = get_posts($args); 
echo $attached_images[0]; 
?> 

获得这个职位画廊图像(不走运)

get_post_gallery_images($post->ID); 

我真的失去了,为什么我不能检索图像。我已经用尽了几乎所有可以在网上找到的建议,并且想知道它是否与我的自定义帖子类型有关或者是什么?任何帮助,将不胜感激。

我想你错过了带有回声的结果代码(正如我在下面所做的那样)或打印或输出。 我设法与此代码做前一阵子..希望它helps-(在这一个使用的fancybox)

<?php 
$attachments = get_posts(array( 
'post_type' => 'attachment', 
'post_mime_type'=>'image', 
'posts_per_page' => -1, 
'post_status' => 'any', 
'post_parent' => $post->ID) 
); 

if ($attachments) { 
foreach ($attachments as $attachment) { 
$src = wp_get_attachment_image_src($attachment->ID, full);  
$html = '<a class="fancybox" href="'.$src[0].'">'; 
$html .= wp_get_attachment_image($attachment->ID, 'gallery-thumb') .'</a>'; 
echo $html; 
} 
} 
?> 

<form id="form1" name="form1" method="post" enctype="multipart/form-data" action=""> 
    <input name="name" type="image" src="resimler/azalt.gif" /> 
</form> 

尝试。

Print_r($ _ Request);你想看的地方。它会工作。

你大概意思是这样(食品法典委员会说,你应该使用get_posts()在这种情况下):

http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

这是我结束了修复它: (评论,以解释我所做的那样)。

<?php 
     // Argument array to hold parameters for get_post() method. 
     $args = array(
      'post_type' => 'attachment', 
      'posts_per_page' => -1, 
      'post_status' => 'any', 
      'post_parent' => null 
      ); 

     // Holds the URL of the image. 
     $image_url = ''; 

     // If we have a thumbnail in the post, use that as the main image. 
     if (has_post_thumbnail($post->ID)) { 
      $image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID))[0]; 
     } 
     // Otherwise, if we have an attachment image, use that as the main image. 
     else if (get_posts($args)) { 
      // Simple test to see if we can display attachments from any post. 
      $attachments = get_posts($args); 
      if ($attachments) { 
       $first_photo = $attachments[0]; 
       $image_url = wp_get_attachment_url($first_photo->ID , false); 
      } 
     } 
     // If there are no images, just display the text. 
     else { 
      echo "No images"; 
     } 
    ?>