设置帖子内容的格式以排除图库

时间:2015-07-04 作者:Adeerlike

我有这段代码,它为帖子的内容创建了第一个图库,并使用更多的代码对其进行格式化,以便使用css和js进行幻灯片演示。

  $gallery = get_post_gallery_images( $post );
  $image_list = \'<ul class="slideshow">\';

  // Loop through each image in each gallery
  foreach( $gallery as $image_url ) {   
    $image_list .= \'<li>\' . \'<img src="\' . $image_url . \'">\' . \'</li>\';
    }  
  $image_list .= \'</ul>\';

  // Prepend our image list to the content of our post
  $content = $image_list . $content;
  // or return only image list
  // $content = $image_list;

  return $content;
它完成了这项工作,但现在如何从内容中删除库代码?因此,我可以使用其余的内容并对其进行包装+样式化。

我在中尝试了preg\\u替换解决方案Split Content and Gallery 还有类似的问题,但我在数组到字符串的转换中遇到了一个php错误。

1 个回复
最合适的回答,由SO网友:Adeerlike 整理而成

我使用了@HOWDY\\u MCGEE的答案中的一个函数,再加上一些重写,附件是我的工作代码。

功能。php:

 function pw_show_gallery_image_urls() {

  global $post;

  // Make sure the post has a gallery in it
  if( ! has_shortcode( $post->post_content, \'gallery\' ) )
    // return $content;
    return \'nadagif\';
  // Retrieve the first gallery in the post
  $gallery = get_post_gallery_images( $post );

  $image_list = \'<ul class="slideshow">\';

  // Loop through each image in each gallery
  foreach( $gallery as $image_url ) {
    $image_list .= \'<li>\' . \'<img src="\' . $image_url . \'">\' . \'</li>\';
  }
  $image_list .= \'</ul>\';


  return $image_list;
 }

function  strip_shortcode_gallery( $content ) {
    preg_match_all( \'/\'. get_shortcode_regex() .\'/s\', $content, $matches, PREG_SET_ORDER );
    if ( ! empty( $matches ) ) {
        foreach ( $matches as $shortcode ) {
            if ( \'gallery\' === $shortcode[2] ) {
                $pos = strpos( $content, $shortcode[0] );
                if ($pos !== false)
                    return substr_replace( $content, \'\', $pos, strlen($shortcode[0]) );
            }
        }
    }
    return $content;
}

function pw_content_with_gallery_list() {

  global $post;

  // 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 class="slideshow">\';

  // 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 = strip_shortcode_gallery(get_the_content());
  $content = $image_list . $content;
  return $content;
}
然后是格式。php,我可以做以下事情:

        <section class="entry-images  cf" itemprop="articleBody">
          <?php echo pw_show_gallery_image_urls(); ?>

        </section> <?php // end images section ?>
以及

          <section class=\'entry-content\'>
            <?php the_content(); ?>
          </section>
如果有人想到更好的解决方案,我很乐意了解他们。

结束

相关推荐

Functions.php中的入队样式

这对我没用functions.php: if( is_page_template( \'template-flat.php\' ) ) { function flatsome_scripts() { wp_enqueue_style( \'flatsome-style\', get_template_directory_uri() .\'/flatash/css/foundation.css\', array(), \'2.1\', \'all\'); }