显示WordPress帖子中的每个图像

时间:2019-04-26 17:48:37

标签: php wordpress

目标是让Wordpress吐出或回显我创建的自定义帖子格式的所有图像。我是PHP的新手,我不希望使用任何第三方插件,请修复下面提供的代码。

我从这里(stackoverflow)或Google遇到的所有解决方案都无法正常工作,或者吐出我从媒体库上传的所有图像,这些都是我不想要的。提供的大多数解决方案都是用于“显示第一张图片”的,我知道并可以使用,但是我希望发布中的所有图片。

这是最接近的代码,可以工作几次,但是它会使我的布局变形,然后返回显示一个图像:

    function displayPostImg() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_img = $matches[0][0];
      return $first_img;

}

我用来粘贴到自定义帖子php中的呼叫:

<?php echo displayPostImg(); ?>

如上所述,现有解决方案不起作用。明确说明我在做什么的任何帮助将是巨大的帮助。

编辑

@corvidism感谢您的进一步解释。我确实看过开发工具,“返回爆破”似乎使它退出了循环,从而导致布局问题。您的解决方案确实显示了我想要的帖子图像/附件,但不幸的是我似乎无法解决此问题。似乎有解决该问题的意图,那就是要做同样的事情。可悲的是,这回荡了媒体库中的每个图像。将您的代码与下面的代码结合起来使我很接近,但是如果我遇到一个解决方案,我将在这里为其他人发布:

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

$attachments = get_posts( $args );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( 'the_title' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );
    }
}

编辑#2:解决方案

弄乱各种解决方案后,我发现了一些可行的方法。试试看:

function grab_those_images( $post ) {

    $content_post = $post->post_content;

    $search_post = '~src="[^"]*"~';
    preg_match_all( $search_post, $content_post, $imgs );
    $no_of_pics = count($imgs[0]);

    if ( $no_of_pics > 0 ) {
            for ( $i=0; $i < $no_of_pics ; $i++ ) {
                $string=$imgs[0][$i];
                $string=trim( $string );
                $length=strlen( $string );
                $image_path=substr_replace( substr( $string, 5, $length ),"",-1 );
                echo '<img src="';
                echo $image_path;
                echo '" />';
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您使用的函数包含缓冲区操作函数(ob_start()ob_end_clean()),它们很可能与模板中的其他内容(可能也是)冲突>使用它们,通常是插件)。

除非您还正在搜索作为帖子元插入的图像(我认为不是),否则您将在$post->post_content属性内的帖子中找到所有图像。您可以在其上使用preg_match_all来查找图像。

您应该将发布内容作为参数传递给函数,而不是访问全局$ post对象。这将使事情变得更可靠并且更容易调试。 (全局对象通常在PHP开发中不受欢迎,如果您打算更广泛地使用PHP而不是仅仅用于WordPress,则要牢记这一点。)

这是函数的外观:

function getAllImagesInPost($postContent) {
    preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $postContent, $matches); 
    // This regex function searches for <img> tags and uses a capture group
    // for the image source url. It will search the whole supplied string,
    // in this case post content. It returns results into the $matches variable.
    $image_tags = $matches[0]; 
    // This is an array containing all the regex matches.
    $image_urls = $matches[1]; 
    // This is an array containing all the capture group matches.

    return implode('',$image_tags); 
    // implode() creates a string from an array of strings/numbers. 
}

在模板文件中,用法取决于您是否在循环中。在循环中,您可以使用$ post对象,并使用$post->post_contentget_the_content()(两者都会使您将帖子内容作为字符串):

echo getAllImagesInPost(get_the_content());

在循环之外,您首先需要获取帖子(例如get_post($somePostID);)。

添加项1:
您必须将变量传递给该函数,以便不能使用the_content()之类的WordPress模板函数。它们直接回显到页面,并返回true或其他无用的东西(在这种情况下,无论如何)。通用WP规则是,如果以the_开头,则回显,如果以get_the_开头,则返回。

添加2:
要仅显示帖子中的图像,而不显示其他内容,只需在模板文件中不使用任何其他内容模板标签即可。 (例如,否the_content())。

添加3:
我怀疑您的问题不是由函数本身引起的,而是由您调用它的方式/位置引起的。但是,如果此函数确实导致布局发生变化,则有99%的正则表达式返回损坏的HTML(根据您的输入,可能性不大,但可能)。

调试此方法的方法是使用浏览器开发工具检查页面,并查找损坏的标签或其他怪异现象。如果看到了这一点,则需要通过测试各种输入上的功能来找到确切的结果。为其输入一个硬编码的字符串,并仅使用文本/图像/其他内容进行测试,直到您知道发生了什么。如果您确定正则表达式是罪魁祸首,则可以尝试自己修复它或在互联网上搜索更好的东西(没有判断力,正则表达式很痛苦)。

相关问题