因此,我创建了一个包含帖子的博客,并使用此代码在帖子内容下显示了一个快捷码库。
问题是;“所有帖子”;页面中没有帖子。我认为这是因为我使用的$content变量。
因此,我使用is\\u single()只在单个贴子页面中执行add\\u content\\u after函数。但现在没有画廊显示。
有人知道我如何解决这个问题吗?谢谢
功能。php:
if ( is_single() ) {
add_filter(\'the_content\', \'add_content_after\');
function add_content_after($content) {
global $post;
if ( $post->post_status == \'publish\' ) {
$attachments = get_posts( array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_parent\' => $post->ID,
\'exclude\' => get_post_thumbnail_id()
) );
if ( $attachments ) {
$atts = array();
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, \'thumbnail-size\', true );
$atts[] = $attachment->ID;
}
$shortcode = do_shortcode(\'[av_gallery ids="\' . implode(",", $atts) . \'" type="slideshow" preview_size="large" crop_big_preview_thumbnail="avia-gallery-big-crop-thumb" thumb_size="portfolio" columns="4" imagelink="lightbox" lazyload="avia_lazyload" av_uid="av-jgesnq4m" custom_class="av-gallery-style-"]\');
$fullcontent = $content . $shortcode;
return $fullcontent;
}
}
}
}
最合适的回答,由SO网友:Nour Edin Al-Habal 整理而成
移动is_single()
功能内部的条件,以及you have to return 如果不满足条件,则为原始内容。
您的代码如下所示:
add_filter(\'the_content\', \'add_content_after\');
function add_content_after($content) {
global $post;
if ( is_single() ) {
if ( $post->post_status == \'publish\' ) {
$attachments = get_posts( array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_parent\' => $post->ID,
\'exclude\' => get_post_thumbnail_id()
) );
if ( !empty($attachments) ) {
$atts = array();
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, \'thumbnail-size\', true );
$atts[] = $attachment->ID;
}
$shortcode = do_shortcode(\'[av_gallery ids="\' . implode(",", $atts) . \'" type="slideshow" preview_size="large" crop_big_preview_thumbnail="avia-gallery-big-crop-thumb" thumb_size="portfolio" columns="4" imagelink="lightbox" lazyload="avia_lazyload" av_uid="av-jgesnq4m" custom_class="av-gallery-style-"]\');
$fullcontent = $content . $shortcode;
return $fullcontent;
}
}
}
return $content;
}