duanjing4667 2018-08-12 04:57
浏览 71

“排除当前帖子”Widget wordpress(PHP帮助)

Hi :) I am working on my site with the "flexible post widget" to show the related contents or the content that I am interested in showing on the sidebar.

The widget works very well and I'm interested in continuing to use it, especially for the design and adaptability options with my site.

The problem? It does not include a function for "exclude the current post".

I was reviewing the code (I add it to this thread) and I think I could add some lines of code for this function (exclude the post I'm reading).

Unfortunately I'm new to programming and I'm lost with what and where to add it.

Could you help me?

<?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;
  • 写回答

1条回答 默认 最新

  • dongming5444 2018-08-12 06:50
    关注

    Add this to your theme's 'functions.php' and remove it from the widget template.

    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' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)