首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >“排除当前帖子”Widget wordpress《PHP帮助》

“排除当前帖子”Widget wordpress《PHP帮助》
EN

Stack Overflow用户
提问于 2018-08-12 12:57:18
回答 1查看 214关注 0票数 0

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

这个小部件工作得很好,我对继续使用它很感兴趣,特别是在我的网站的设计和适应性选项方面。

这个问题吗?它不包括一个用于“排除当前帖子”的函数。

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

不幸的是,我是编程新手,我对添加什么和在哪里添加它感到困惑。

你能帮我一下吗?

代码语言:javascript
复制
<?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;
EN

回答 1

Stack Overflow用户

发布于 2018-08-12 14:51:00

将其添加到主题的'functions.php‘中,并将其从小部件模板中删除。

代码语言:javascript
复制
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' );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51805823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档