Wordpress Can';t仅在POST中重写函数
我正在用这个主题写博客。这个主题支持帖子缩略图,我想把它们从单个帖子中删除,并把它们放在其他地方(家里、档案馆、分类等等)。缩略图是用一个可插拔的函数创建的,所以理论上我应该重写它。如果我将其添加到子主题的functions.php中:Wordpress Can';t仅在POST中重写函数,wordpress,Wordpress,我正在用这个主题写博客。这个主题支持帖子缩略图,我想把它们从单个帖子中删除,并把它们放在其他地方(家里、档案馆、分类等等)。缩略图是用一个可插拔的函数创建的,所以理论上我应该重写它。如果我将其添加到子主题的functions.php中: function coral_dark_post_thumbnail() {} 缩略图实际上消失了。但是,如果使用条件仅在POST中运行函数: if ( is_single() ) { function coral_dark_post_thumbn
function coral_dark_post_thumbnail() {}
缩略图实际上消失了。但是,如果使用条件仅在POST中运行函数:
if ( is_single() ) {
function coral_dark_post_thumbnail() {}
}
它什么也不做。缩略图仍然随处可见。我甚至可以用其他任何东西来替换它,因为WordPress只是忽略了它。如果我给条件加上一个否定,像这样:
if ( ! is_single() ) {
function coral_dark_post_thumbnail() {}
}
缩略图消失了,但到处都是。同样,我可以用任何东西替换is_single
,因为结果是一样的。我不明白发生了什么事
这是原始功能:
if ( ! function_exists( 'coral_dark_post_thumbnail' ) ) :
/**
* Displays an optional post thumbnail.
*
* Wraps the post thumbnail in an anchor element on index views, or a div
* element when on single views.
*
* Create your own coral_dark_post_thumbnail() function to override in a child theme.
*
*/
function coral_dark_post_thumbnail() {
if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
return;
}
if ( is_singular() ) :
?>
<div class="post-thumbnail">
<?php the_post_thumbnail('large'); ?>
</div><!-- .post-thumbnail -->
<?php else : ?>
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
<?php the_post_thumbnail( 'post-thumbnail', array( 'class' => 'alignleft smallpostthumb', 'alt' => the_title_attribute( 'echo=0' ), 'sizes' => '(max-width: 480px) 100vw, 210px' ) ); ?>
</a>
<?php endif; // End is_singular()
}
endif;
如果(!function\u存在('coral\u dark\u post\u缩略图'):
/**
*显示可选的文章缩略图。
*
*将文章缩略图包装到索引视图上的锚元素或div中
*在单个视图上显示时的元素。
*
*创建您自己的coral_dark_post_thumbnail()函数以在子主题中重写。
*
*/
函数coral_dark_post_缩略图(){
if(post_password_required()| | is_attachment()| |!has_post_缩略图()){
返回;
}
如果(是单数()):
?>
您的问题似乎与条件有关。您不能使用条件包装函数
if ( is_single() ) {
function coral_dark_post_thumbnail() {}
}
而是使用:
function coral_dark_post_thumbnail() {
if ( is_single() ) {
//TODO: Something
}
}
执行上述操作应该有效。似乎您的问题在于条件。您不能使用条件包装函数
if ( is_single() ) {
function coral_dark_post_thumbnail() {}
}
而是使用:
function coral_dark_post_thumbnail() {
if ( is_single() ) {
//TODO: Something
}
}
执行上述操作应该有效。不幸的是,这将不起作用,因为插入的函数的要点是,如果已经存在另一个同名函数,则它们不会运行:如果(!function_exists('coral_dark_post_缩略图')):
因此,如果我将条件放在函数中,我放在条件中的任何内容都将不相关,因为原始函数将被停止,因为我正在声明另一个同名函数。这就是为什么我要声明它(并因此被重写)仅在个别帖子中。我的另一个选择是复制/粘贴整个原始函数并将其放入条件中,但这不是我想要的。你是否尝试过上述方法?是的,这是我在发布问题之前尝试过的选项之一。:/不幸的是,这不会起作用,因为插头的点ged函数是指,如果已经存在另一个同名函数,则它们不会运行:if(!function_exists('coral_dark_post_缩略图')):
因此,如果我将条件放在函数中,我放在条件中的任何内容都将不相关,因为原始函数将被停止,因为我正在声明另一个同名函数。这就是为什么我要声明它(并因此被重写)仅在个别帖子中。我的另一个选择是复制/粘贴整个原始功能并将其置于条件内,但这不是我想要的。您是否尝试过上述方式?是的,这是我在发布问题之前尝试过的选项之一:/