我一直在研究一个类似的问题,所以找到了一个可能的解决方案。
问题是“内联”图像,与作为附件的图像(如图库)不同,没有针对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 templateif ( 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;
请注意,以上所有内容都是代码片段,而不是端到端解决方案。但它们可能有助于制定出满足您需求的解决方案。