您的代码将导致致命错误,因为您在循环中定义了一个函数,因此在此后的每次迭代中都会重新定义该函数。这是问题的一部分。查看清理后的代码:
function pw_show_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, \'gallery\' ) )
return $content;
// Retrieve the first gallery in the post
$gallery = get_post_gallery_images( $post );
$image_list = \'<ul>\';
// Loop through each image in each gallery
foreach( $gallery as $image_url ) {
$image_list .= \'<li>\' . \'<img src="\' . $image_url . \'">\' . \'</li>\';
}
$image_list .= \'</ul>\';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( \'the_content\', \'pw_show_gallery_image_urls\' );
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
} // end while
} // end if
注意底部的循环。您已将筛选器添加到
the_content
但循环中没有任何内容可以应用该过滤器,甚至根本无法打印任何内容。您需要:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_content(); // minimum
} // end while
} // end if