嗨:)我正在使用“灵活的post小部件”在我的网站上工作,以显示相关内容或我有兴趣在侧边栏上显示的内容。
这个小部件工作得很好,我对继续使用它很感兴趣,特别是在我的网站的设计和适应性选项方面。
这个问题吗?它不包括一个用于“排除当前帖子”的函数。
我正在检查代码(我将它添加到这个线程中),我想我可以为这个函数(不包括我正在阅读的帖子)添加一些代码行。
不幸的是,我是编程新手,我对添加什么和在哪里添加它感到困惑。
你能帮我一下吗?
<?php
/**
* Flexible Posts Widget: Default widget template
*
* @since 3.4.0
*
* This template was added to overcome some often-requested changes
* to the old default template (widget.php).
*/
// Block direct requests
if ( !defined('ABSPATH') )
die('-1');
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
if ( $flexible_posts->have_posts() ):
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'widget_posts_args', 'be_exclude_current_post' );
?>
<ul class="dpe-flexible-posts">
<?php while ( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post; ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php echo the_permalink(); ?>">
<?php
if ( $thumbnail == true ) {
// If the post has a feature image, show it
if ( has_post_thumbnail() ) {
the_post_thumbnail( $thumbsize );
// Else if the post has a mime type that starts with "image/" then show the image directly.
} elseif ( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
echo wp_get_attachment_image( $post->ID, $thumbsize );
}
}
?>
<div class="title"><?php the_title(); ?></div>
</a>
</li>
<hr>
<?php endwhile; ?>
</ul><!-- .dpe-flexible-posts -->
<?php
endif; // End have_posts()
echo $after_widget;
发布于 2018-08-12 14:51:00
将其添加到主题的'functions.php‘中,并将其从小部件模板中删除。
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'dpe_fpw_args', 'be_exclude_current_post' );
https://stackoverflow.com/questions/51805823
复制相似问题