在帖子中的所有图片上生成链接--有挂钩吗?

时间:2018-09-14 作者:Joschi

我正在寻找一种方法来替换图像上的所有链接。当前,当用户单击图像时,它会链接到源文件。

我想让它做的是:举例说明。com/编辑器=12(&P);image=文件名。jpgSo中包含的每个都应该使用图像文件名链接到该站点,以便可以将其加载到编辑器中。

首先,我尝试用ajax替换链接,但这不适用于延迟加载,而且似乎不太实用。我尝试的第二件事是过滤帖子的$内容。但有了它,我只能替换/wp内容/上传/部分。我无法过滤第一部分(http://),这也导致所有src都被替换,并且根本没有显示任何图像。

也许有什么办法可以用钩子做这个吗?

你好Patrick

更新:所以这适用于作为单个图像添加的图像,但是它似乎不适用于库短代码,即使优先级为12并且短代码已经执行。

add_filter(\'the_content\', \'my_content_filter\', 12);
function my_content_filter($content) {

if ( is_single() )

   $content = str_replace(\'<a href="https://example.com/wp-content/uploads/\',\'<a href="https://example.com/?editor=82&image=\',$content);
  return $content;
}

2 个回复
SO网友:Rick Hellewell

我一直在研究一个类似的问题,所以找到了一个可能的解决方案。

问题是“内联”图像,与作为附件的图像(如图库)不同,没有针对the_content() 它专门处理图像标签。(至少,我还没有找到。)

因此,您需要使用一些正则表达式来搜索the_content() ,并将每个图像放入一个数组中,然后可以根据需要在图像数组中循环。

我在此处找到此代码:https://gist.github.com/jlord/3680879 . 我尚未对其进行测试,但它可能会给您一个开始:

 // get the content from the post
  $posttext = $post->post_content;
  // next line added to process any shortcodes in the content string
  $posttext = do_shortcode($posttext);
  // make a variable for the string you\'re looking to find in all of the content
  $regex = \'~<img [^\\>]*\\ />~\';
  // find the first things inside of the second thing and put them inside of the third thing
  preg_match_all($regex, $posttext, $images);
  // redefine posttext as itself minus all the things you found 
  $posttext = preg_replace($regex, \'\', $posttext);

  // now posttext has no images, and $images is an array of image tags
  // have fun, o lord of jellies ?> <!-- this part is from issac -->

<div id="thePostText">
  <?php 
  // now spit out all the text
  echo $posttext; ?>
</div>

<div id=\'thePostImages\'>
  <?php 
  // for each image, wrap it in the p tags and spit it out 
  foreach ( $images[0] as $image ) {
  echo \'<p class="aPostImage">\' . $image . \'</p>\'; } ?>
</div>
您可以将此代码放置在循环内的模板中。然后将该模板用于post输出。

如果这有帮助的话,我会很感兴趣的。这可能会帮你在谷歌上节省几个小时,这就是我发现它的原因。

Added

$posttext = $post->post_content; 未使用the_content() (它也处理短代码),则不能处理post内容中的短代码。特别是[gallery] 未处理短代码。我怀疑其他短代码也没有被处理。

因此,我在上述代码中添加了一行额外的代码:

$posttext = do_shortcode($posttext);
获取在内容字符串中处理的短代码。

Added

上面的代码不一定允许您连接到gallery图像的输出。所以我在这里找到了这段代码,可以作为起点:How to get post attachments in gallery post format template

if ( get_post_gallery() ) :
        $gallery = get_post_gallery( get_the_ID(), false );

        /* Loop through all the image and output them one by one */
        foreach( $gallery[\'src\'] as $src ) : ?>
          <li> <img src="<?php echo $src; ?>" class="gallery-slider" alt="Gallery image" /> </li>
          <?php
        endforeach;
    endif;
请注意,以上所有内容都是代码片段,而不是端到端解决方案。但它们可能有助于制定出满足您需求的解决方案。

SO网友:Joschi

我不知道为什么,但这也适用于画廊:D@RickHellewell,非常感谢您在这个话题上的帮助!

add_filter(\'the_content\', \'my_content_filter\', 12);

function my_content_filter($content) {

    global $post;

    if ( is_single() ) {

        // For gallery
        $content = str_replace("<a href=\'https://example.com/wp-content/uploads/","<a href=\'https://example.com/?editor=82&image=",$content);

        $content = str_replace(\'<a href="https://example.com//wp-content/uploads/\',\'<a href="https://example.com/?editor=82&image=\',$content);


    }

    return $content;

}

结束

相关推荐

使用wp-Migrate-db-proMedia Files Add-on迁移后映像路径错误

从现有生产站点创建临时站点时遇到问题。使用WP Migrate DB Pro 1.8.1和media files add-on v1提取媒体文件时。4.9,我可以使用原始站点的媒体成功地填充媒体库,但无论出于何种原因,所有路径都是错误的-因此,带有指向/wordpress/wp-content/uploads/.../ 找不到,因为图像已全部拉入/app/uploads/2018/.../.如果有人遇到这个问题并找到了解决方案,请让我知道(除了重新上传每个图像…这个特定的网站有大约3000个图像)。谢谢