代码之家  ›  专栏  ›  技术社区  ›  Galgóczi Levente

如果当前文章中的附件图像数超过“x”,如何仅显示文章中的“x”图像(在分类页面上)?

  •  0
  • Galgóczi Levente  · 技术社区  · 7 年前

    所以我有这个密码:

       $attachments = get_children(
           array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'post_parent' => $post->ID
           ));
       if(count($attachments) > 3) { ?>
           <!-- Code need to here, what shows only the first 3 images -->
       <?php } else {
           the_content();
             } ?>
    

    我正在网上寻找解决办法,但暂时什么也找不到。如果你能帮我,请写信。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Galgóczi Levente    7 年前

    这是在基于子域的多站点上工作的,其中包含重新记录(广播或任何)的子帖子,这些子帖子没有原始内容(因为它从其父站点获取)。

    1. 添加到function.php:

        // ADD IMAGES TO the_expert
        function improved_trim_excerpt($text) {
           global $post;
           if ( '' == $text ) {
                   $text = get_the_content('');
                   $text = apply_filters('the_content', $text);
                   $text = strip_tags($text, '<a><img>');
      
                  $delim = '</a>'; // I think you also want add links to your images
                  $start = 0; // starter image
                  $items = 3; // number of images
                  $the_excerpt = implode($delim, array_slice(explode($delim, $text), $start,$items)) . $delim; // $delim closes your excerpt
           }
           return $the_excerpt;
        }
        remove_filter('get_the_excerpt', 'wp_trim_excerpt');
        add_filter('get_the_excerpt', 'improved_trim_excerpt');
      
    2. 内容页的部分:

        global $post;
        $postid = ($id == $post->ID) ? $post->ID : $id;
        $content = $post->post_content;
        // match all urls
        preg_match_all( '/(http:|https:)?\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/', $content, $matches );
        $count = 0;
        if ( ! empty( $matches ) && ! empty( $matches[ 0 ] ) ) {
            foreach ( $matches[ 0 ] as $url ) {
                $split    = explode( '#', $url );
                $split    = explode( '?', $split[ 0 ] );
                $split    = explode( '&', $split[ 0 ] );
                $filename = basename( $split[ 0 ] );
                $file_type = wp_check_filetype( $filename, wp_get_mime_types() );
                if ( ! empty ( $file_type[ 'ext' ] ) ) {
                    // (optional) limit inclusion based on file type
                    if( ! in_array( $file_type[ 'ext' ], array('jpg', 'png', 'gif', 'webp', 'bmp')) ) continue;
                    $files[ $url ] = $file_type;
                    $urls[]=$url;
                    $count ++;
                }
            }
        }
        $no_of_photos = $count / 2; // Gets the number of photo's attached in $no_of_photos variable -> you need use / 2, if your image's links shows to the file, because in this case, in every <img> have two 'ext'.
        $limit_photos = 3; // set your limit
      
                    if( $no_of_photos <= $limit_photos ) {
                    the_content(); ?>
                    } else {
                    the_excerpt(); ?>
                    } 
      

    特别感谢@andreas: https://stackoverflow.com/a/50609326/9565969

    以及@jgraup: https://wordpress.stackexchange.com/a/246063/134567