簡體   English   中英

覆蓋不可插入的功能wordpress兒童主題

[英]Override unpluggable function wordpress child theme

在嘗試編輯我當前的主題(稱為Carrie)時,我一直在努力。 我正在使用兒童主題。 所以我想要實現的是編輯/設置一個名為' carrie_header_post_show '的主題函數。 此函數位於名為theme-functions.php的父文件(位於inc目錄中)中。 代碼如下所示:

function carrie_header_post_show() {
    $carrie_theme_options = carrie_get_theme_options();

    $header_post_html = '';

    if(isset($carrie_theme_options['header_post_id']) && $carrie_theme_options['header_post_id']<>'') {

      $header_post_id = $carrie_theme_options['header_post_id'];

      $header_post = get_post($header_post_id);

      if($header_post) {
        if(has_post_thumbnail( $header_post_id )) {

          $header_post_thumb_id = get_post_thumbnail_id($header_post_id);

          $header_post_image_url = wp_get_attachment_image_src( $header_post_thumb_id, 'carrie-blog-thumb-widget');

          $header_post_image = '<a href="'.get_permalink($header_post_id).'"><img src="'.esc_url($header_post_image_url[0]).'" alt="'.esc_attr($header_post->post_title).'"/></a>';

        } else {

          $header_post_image = '';

        }

        $header_post_categories_list = get_the_category_list(', ', 0, $header_post_id);

        $header_post_html .= '<div class="header-post-image hover-effect-img">'.wp_kses_post($header_post_image).'</div>';
        $header_post_html .= '<div class="header-post-details">';
        $header_post_html .= '<div class="header-post-category">';

        $title = wp_kses_post($header_post_categories_list);

        $header_post_html .= '</div>';
        $header_post_html .= '<div class="header-post-title"><a href="'.get_permalink($header_post_id).'">'.wp_kses_post($header_post->post_title).'</a></div>';
        $header_post_html .= '</div>';

        echo '<div class="header-post-content clearfix">'.wp_kses_post($header_post_html).'</div>';
      }

    }

}

我想要實現的是添加幾行代碼。 我試過以下行動:

  1. 在子主題中創建inc文件夾,目標文件(theme-functions.php)也可以編輯它。 不起作用,得到一個錯誤。
  2. 將編輯過的函數放入子函數中.php。 同樣在這里 - 不起作用。
  3. 將函數放在IF語句中 - 相同,不起作用。

那我該怎么辦呢?

你應該做的是讓主題作者使用function_exists()使他們的函數有條件,然后允許你在子主題中聲明你自己的版本。 首先加載子主題,如果函數的父版本是有條件的(它應該是),那么如果您的子主題中有一個版本,它將永遠不會被執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM