2016-09-07 39 views
1

我正在使用的主題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; 

提前非常感謝。 :)

回答

0

看來你的問題是與條件。不能使用條件來包裝你的函數裏

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

而是使用:

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

做好以上應該工作。

+0

不幸的是,這將無法工作,因爲的封堵功能的一點是,如果有另一個同名的函數已經存在,它們不運行:'如果(function_exists(「coral_dark_post_thumbnail」)!):'所以,如果我把功能,無論我提出的條件內內的條件都無關緊要了,因爲原來的功能將只是一個事實,即我聲明瞭一個又一個具有相同名稱的停止。這就是爲什麼我希望僅在個別帖子中宣佈它(並因此被覆蓋)。 – Stealth

+0

我的另一個選項是複製/粘貼整個原有的功能,並把它的狀態中,但是這不是我要找的。 – Stealth

+0

你有沒有試過按照上述方式做? – DpEN