Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 仅从wordpress帖子获取图像_Image_Wordpress_Post_Tags_Categories - Fatal编程技术网

Image 仅从wordpress帖子获取图像

Image 仅从wordpress帖子获取图像,image,wordpress,post,tags,categories,Image,Wordpress,Post,Tags,Categories,在wordpress中,由于 在我的例子中,每个帖子都有一个图像,那么我怎么能只在点击一个类别(或标签)时才获取图像呢?我不熟悉我需要使用的代码 更新:我尽量不使用插件。我很抱歉没有早些提到这一点。我试图做到的是——所有帖子都有图片,点击与任何帖子相关的任何类别(或标签),只获取图片 更新2:我尝试了以下方法: <?php $args = array( 'post_type' => 'attachment', 'numberposts' => -1,

在wordpress中,由于

在我的例子中,每个帖子都有一个图像,那么我怎么能只在点击一个类别(或标签)时才获取图像呢?我不熟悉我需要使用的代码

更新:我尽量不使用插件。我很抱歉没有早些提到这一点。我试图做到的是——所有帖子都有图片,点击与任何帖子相关的任何类别(或标签),只获取图片

更新2:我尝试了以下方法:

 <?php   

 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           echo wp_get_attachment_image( $attachment->ID, 'full' );
           echo '</li>';
          }
     }

 ?>

唯一奇怪的是,我想弄清楚,媒体库中的另一张图片也出现了,而我的任何帖子中都没有


我还发现它与我想要的非常接近,但不幸的是,它必须在单独的页面中,而不是在类别页面中。

请查看这可能是您要查找的内容。

您可以通过以下方式实现这一点:


谢谢@Mark。我没有提到我试图避免使用插件。我看了一下插件并阅读了常见问题解答,它看起来不像我要找的。我用一个例子更新了我的问题。@phptinker制作插件的人也有,但我没有试过,但当我点击一个类别时,什么也没有显示。这一页完全是空白的。请参阅上面的更新2。这是为了获得1篇文章。为了运行整个类别,您可能需要对其进行一些修改,以更加集中地完成此任务。它成功了。与此同时,我发现这与@Mark一开始指出的是一样的。谢谢你们。我很感激你们把它挖出来了,但我也要公平地对待@Mark。投票了,没问题,谢谢。
<?php

function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; }
$more = 0;
}

?>