如何根据页面相关的条件标签有条件地添加过滤器 [英] How to add a filter conditionally based on page related Conditional Tags
本文介绍了如何根据页面相关的条件标签有条件地添加过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个 WordPress 插件,无法根据条件标签有条件地添加过滤器.
I am developing a WordPress plugin and can't conditionally add filters based on the conditional tags.
这是我的代码:
add_Action('init', 'determine_location');
function determine_location() {
$d = get_option('display_locations'); //this is a checkbox field in the plugins settings with 7 options
if (isset($d)) {
if ($d[0] == 'home') {
if (is_home()) {
add_filter('the_excerpt', 'insert_buttons_to_post_top');
add_filter('the_content', 'insert_buttons_to_post_top');
}
} else if ($d == 'post' || $d == 'post') {
if (is_single()) {
add_filter('the_content', 'insert_buttons_to_post_top');
}
} else if ($d[0] == 'page' || $d == 'page' || $d == 'page') {
if (is_page()) {
add_filter('the_content', 'insert_buttons_to_post_top');
}
} else if ($d[0] == 'category' || $d[1] == 'category' || $d[2] == 'category' || $d[3] == 'category') {
if (is_category()) {
add_filter('the_excerpt', 'insert_buttons_to_post_top');
add_filter('the_content', 'insert_buttons_to_post_top');
}
} else if ($d[0] == 'tag' || $d[1] == 'tag' || $d[2] == 'tag' || $d[3] == 'tag' || $d[4] == 'tag') {
if (is_tag()) {
add_filter('the_excerpt', 'insert_buttons_to_post_top');
add_filter('the_content', 'insert_buttons_to_post_top');
}
} else if ($d[0] == 'archive' || $d[1] == 'archive' || $d[2] == 'archive' || $d[3] == 'archive' || $d[4] == 'archive' || $d[5] == 'archive') {
if (is_archive()) {
add_filter('the_excerpt', 'insert_buttons_to_post_top');
add_filter('the_content', 'insert_buttons_to_post_top');
}
}
}
}
function insert_buttons_to_post_top(){
return "<div>Output</div>"
}
目前这段代码没有显示输出.这里有什么问题?
Currently this code shows no output. What is wrong here?
推荐答案
只需为每个自定义函数添加单独的操作即可:
Do it like this by simply adding separate actions for each of your custom functions:
add_action('wp', 'custom1');
function custom1() {
if ($d == 'post'){
if (is_single()) {
add_filter('the_content', 'insert_buttons_to_post_bottom');
}
}
}
add_action('wp', 'custom2');
function custom2(){
if ($d == 'page'){
if (is_page()) {
add_filter('the_content', 'insert_buttons_to_post_bottom');
}
}
}
add_action('wp', 'custom3');
function custom3(){
if ($d == 'archive'){
if (is_archive()) {
add_filter('the_content', 'insert_buttons_to_post_bottom');
}
}
}
function insert_buttons_to_post_bottom(){
return "something";
}
这篇关于如何根据页面相关的条件标签有条件地添加过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文