2015-11-03 54 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? 
} 

而不是試圖將其追加到現有的內容。