【问题标题】:Cannot get the attachments/media from a custom Wordpress post type?无法从自定义 Wordpress 帖子类型中获取附件/媒体?
【发布时间】:2015-08-31 04:24:38
【问题描述】:

我正在尝试在我的 Wordpress php 代码中获取附加/插入的图像,该代码已插入到管理界面的测试帖子中。我点击了“添加媒体”按钮并上传了我的图片并更新了帖子(front-page 是自定义帖子类型)。

(我点击了这个按钮):

但是,我似乎无法终生检索与该帖子相关的图片。如果我设置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(post 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 );

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

【问题讨论】:

    标签: php html wordpress image underscores-wp


    【解决方案1】:

    我认为您缺少带有回显(如下所示)或打印或输出的结果代码。 不久前我设法用这段代码做到了..希望它有所帮助-(在这个中使用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;
    }
    }
    ?>
    

    【讨论】:

      【解决方案2】:
      <form id="form1" name="form1" method="post" enctype="multipart/form-data" action="">
           <input name="name" type="image" src="resimler/azalt.gif" />
      </form>
      

      试试这个。

      打印_r($_Request);你想看的地方。它会起作用的。

      【讨论】:

        【解决方案3】:

        您可能是这个意思(法典说在这种情况下您应该使用 get_posts()):

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

        【讨论】:

          【解决方案4】:

          这就是我最终修复它的方式: (评论是为了解释我做了什么)。

          <?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";
                  }
              ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-10-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多