首页 > 解决方案 > “排除当前帖子”小部件 wordpress(PHP 帮助)

问题描述

嗨 :) 我正在使用“灵活的帖子小部件”在我的网站上显示相关内容或我有兴趣在侧边栏上显示的内容。

该小部件运行良好,我有兴趣继续使用它,特别是对于我的网站的设计和适应性选项。

问题?它不包括“排除当前帖子”的功能。

我正在查看代码(我将它添加到这个线程),我想我可以为这个函数添加一些代码行(不包括我正在阅读的帖子)

不幸的是,我是编程新手,我不知道在哪里添加它。

你可以帮帮我吗?

<?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;

标签: phpwordpresswidget

解决方案


将此添加到主题的“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' );

推荐阅读