2014-01-09 121 views
0

爲我的WordPress的網站創建一個自定義函數,將在帖子內容下面添加一個評論部分,我需要從另一個文件插入這個函數到我添加到我的functions.php的自定義函數中。我需要從不同的文件得到這段代碼$buffy .= $this->get_review();在此功能工作:如何從另一個php文件插入php代碼?

function content_injector($content) { 
    global $wp_query; 
    $postid = $wp_query->post->ID; 

    if (is_single((get_post_meta($postid, 'top_ad', true) != 'off'))) { 
     $top_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]'); 
    } 

    if (is_single((get_post_meta($postid, 'bottom_ad', true) != 'off'))) { 
     $bottom_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]'); 
    } 

    if (is_single()) { 
     $review = //HOW DO I ADD THAT get_review CODE HERE? 
     $custom_share = '<div id="title-deel"><h4 class="block-title"><span>DEEL</span></h4></div>' . do_shortcode('[ssba]'); 
     $facebook_comments = '<div id="title-reageer"><h4 class="block-title"><span>REAGEER</span></h4></div>' . '<div class="fb-comments" data-href="' . get_permalink() . '" data-colorscheme="light" data-numposts="5" data-mobile="false" data-width="700"></div>'; 
    } 

    $content = $top_ad . $content . $bottom_ad . $custom_share . $facebook_comments; 
    return $content; 
} 

add_filter('the_content', 'content_injector'); 

正如你可以看到我需要的get_review功能添加到$review,但我不能讓我自己的工作。如何使這項工作?

+0

你有沒有考慮[包括()](http://www.php.net/manual/en/function.include.php)? –

+0

是無法獲得get_review函數的包含功能! – Rik

+0

你應該把所有的函數放在'functions.php'文件中,以便在WordPress上工作。但是,'include'還是'require'應該可以完成這項工作? – Maaz

回答

0

使用include可以在使用該文件中的任何方法之前包含該文件。

include("file_with_functions.php"); 

OR

創建類(具有文件名相同類名)。

包含該文件。

創建類的一個實例。

通過訪問該對象的類中的方法

+0

另外,如果您想確保文件只包含一次,請使用'include_once()'。另外,根據你想要拋出的錯誤類型,如果文件不存在,請看'require()'和'require_once()' –

+0

在我的功能中,我需要添加include函數嗎?我不能在$ review ='之後添加它,對吧? – Rik

+0

您通常將include添加到文件的頂部,但不一定。然後,您可以創建一個實例並從content_injector方法中調用該方法。 – raj

相關問題