2014-01-09 62 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

相关问题