无法覆盖帖子

时间:2016-09-07 02:31:25

标签: wordpress

我正在使用主题Coral Dark在博客上工作。这个主题支持发布缩略图,我希望将它们从各个帖子中删除,并将它们留在其他地方(家庭,档案,类别等)。缩略图是使用可插入功能创建的,所以理论上我应该只需要覆盖它。如果我将它添加到我的子主题的functions.php中:

   function coral_dark_post_thumbnail() {}

缩略图有效消失。但是,如果我使用条件仅在帖子中运行该函数:

if ( is_single() ) {
   function coral_dark_post_thumbnail() {}
}

它什么都不做。缩略图仍然出现在任何地方。我甚至可以用其他任何东西替换is_single,因为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;

非常感谢提前。 :)

1 个答案:

答案 0 :(得分:0)

似乎你的问题与病情有关。您不能使用条件来包装您的函数li

if ( is_single() ) {
   function coral_dark_post_thumbnail() {}
}

改为使用:

 function coral_dark_post_thumbnail() {
     if ( is_single() ) {
          //TODO: Something
     }
 }

执行上述操作应该有效。

相关问题