php - “排除当前帖子”小部件 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;
解决方案
将此添加到主题的“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' );
推荐阅读
- ios - 为什么 numberOfRowsInComponent 函数运行了这么多次?
- c++ - Oracle 中 DBMS_ALERT 的 ODBC 等效项
- javascript - Google App 脚本 - 例外:您无权调用 UrlFetchApp.fetch
- php - 如何在 PHP 中将多个文本区域 $_POST 到 SQL 数据库中?
- python - 使用python从文本中拆分特定模式(例如:2公斤大米)
- c++ - 我可以以某种方式将枚举类型与字符串结构结合起来吗?
- testcontainers - Testcontainers execInContainer 不适用于 CockroachDB
- oracle - 使用 tnsname.ora 和 liquibase 连接到 oracle 数据库
- docker - 无法在 docker 容器内执行命令[Jenkins Pipeline]
- javascript - 如何根据对象属性在javascript中动态生成假数据对象?