2015-11-03 53 views
0

我正在制作一个插件在每个页面的右下角添加图像。 Followin是功能正确的代码,并添加图像作为内容。wp_footer挂钩不工作

add_filter('the_content', 'scroll_to_top_data'); 

,但我想它在右下角页脚,我试过之后添加:

add_action('wp_footer', 'scroll_to_top_data'); 

也没有显示错误,这是没有在任何地方显示图像。我使用二十五个主题,但当然我希望这个插件可以在wordpress的所有主题上使用。请指导我为什么不能使用wp_footer钩子,并且如何在页脚之后放置它?下面是scroll_to_top_data功能

function scroll_to_top_data($content = NULL) { 
$post_id = get_the_ID(); 
global $wpdb; 
$table = $wpdb->prefix . 'scroll'; 

$myrows = $wpdb->get_results("SELECT * FROM $table WHERE id = 1"); 
$beforeafter = $myrows[0]->beforeafter; 
$where_like = $myrows[0]->where_like; 
$status = $myrows[0]->status; 
$image = $myrows[0]->image; 
$action = $myrows[0]->action; 
$color = $myrows[0]->color; 
$display = $myrows[0]->display; 
$except_ids = $myrows[0]->except_ids; 
$url = $myrows[0]->url; 
$width = $myrows[0]->width; 
$position = $myrows[0]->position; 
$str = $image; 

if ($status != 0) { 
    $scrollImage = '<img src="' . $image . '"'; 
    return $content . $scrollImage; 
} else { 
    return $content; 
} 
} 
+0

什么是'scroll_to_top_data'功能是什么样子? –

+0

#Jesse我已经更新了我的问题,请检查。谢谢! –

回答

1

wp_footer动作不传递任何参数或者期待任何返回值就像一个过滤器将(在这种情况下$content)。过滤器期望传递信息进行修改和返回,动作不会。为了让您的图像输出,你必须改变如下:

if ($status != 0) { 
    $scrollImage = '<img src="' . $image . '"'; 
    return $content . $scrollImage; 
} else { 
    return $content; 
} 

要真正呼应图像:

if ($status != 0) { 
    echo '<img src="' . $image . '" />'; //doesn't seem to have a closing bracket? 
} 

而不是试图将其追加到现有的内容。