如果当前 post 中的附件图像数量多于 "X",如何仅显示来自 post(在分类页面上)的 "X" 图像?
How to shows only "X" images from a post (on a taxonomy page), if the number of the attachment images in the current post more than like "X"?
我有一个多站点网络,用户上传 posts,当用户将超过 10 张图像附加到 post 时,这太可怕了,因为这会很长,并且延伸到4.-5。砌体布局上的页面...所以我想要,如果 post 有超过 3 个附件(图像),在我的网络主页(和分类页面)上 post 只显示第一个3张图片,一张link到完整的post。
所以我有这个代码:
$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();
} ?>
我正在网上寻找解决方案,但目前我没有找到任何东西。如果你能帮助我,请写信。
这适用于基于子域的多站点,具有重新记录(广播或任何)子帖子,没有原始内容(因为它是从它们的父级获取的)。
添加到您的 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');
以及您的内容页面部分:
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:
还有@jgraup:
https://wordpress.stackexchange.com/a/246063/134567
我有一个多站点网络,用户上传 posts,当用户将超过 10 张图像附加到 post 时,这太可怕了,因为这会很长,并且延伸到4.-5。砌体布局上的页面...所以我想要,如果 post 有超过 3 个附件(图像),在我的网络主页(和分类页面)上 post 只显示第一个3张图片,一张link到完整的post。 所以我有这个代码:
$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();
} ?>
我正在网上寻找解决方案,但目前我没有找到任何东西。如果你能帮助我,请写信。
这适用于基于子域的多站点,具有重新记录(广播或任何)子帖子,没有原始内容(因为它是从它们的父级获取的)。
添加到您的 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');
以及您的内容页面部分:
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:
还有@jgraup: https://wordpress.stackexchange.com/a/246063/134567