我尝试按类别在帖子中添加横幅。我得到了以下代码,显示1000个单词后的横幅。但是,例如,我想展示一个横幅,如果是一个类别3,另一个横幅是一个类别1.有人可以告诉我我需要添加什么,请吗?
问候。
这是代码。
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1000;
// insert after the first </p> after 160 characters
$after_character = 160;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
$text = '
<center><a href="http://www.misite.net/descargar.php?go=1" target="_blank"> <img src="http://www.misite.net/reproductor.jpg" alt="http://www.misite.net/reproductor.jpg" /></a></center> 
';
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
我试试这个,但是给我看了一个字符串错误:
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1000;
// insert after the first </p> after 160 characters
$after_character = 160;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
$text = '
if ( in_category( 1,3 ) ) {
$text = '<a href="http://www.misite.com/enlace" target="_blank"><img src="http://www.misite.com/banner1.jpg" alt="http://www.misite.com/banner1.jpg" /></a>';
} elseif ( in_category( 38158 ) ) {
$text = '<a href="http://www.misite.com/enlace" target="_blank"><img src="http://www.misite.com/banner2.jpg" alt="http://www.misite.com/banner2.jpg" /></a>';
} else {
$text = '<a href="http://www.misite.com/enlace" target="_blank"><img src="http://www.misite.com/banner3.jpg" alt="http://www.misite.com/banner3.jpg" /></a>';
}
';
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
再次问好!工作正常。谢谢你们!这是工作代码:
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1000;
// insert after the first </p> after 160 characters
$after_character = 160;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
if ( in_category( 1 ) ) {
$text = '<center><a href="http://www.misite.com/enlace" target="_blank"><img src="http://www.misite.com/banner1.jpg" alt="http://www.misite.com/banner1.jpg" /></a></center>';
} elseif ( in_category( 3 ) ) {
$text = '<center><a href="http://www.misite.com/enlace" target="_blank"><img src="http://www.misite.com/banner2.jpg" alt="http://www.misite.com/banner2.jpg" /></a></center>';
} elseif ( in_category( 4 ) ) {
$text = '<center><a href="http://www.misite.com/enlace" target="_blank"><img src="http://www.misite.com/banner3.jpg" alt="http://www.misite.com/banner3.jpg" /></a></center>';
} else {
$text = '<center><a href="http://www.misite.com/enlace" target="_blank"><img src="http://www.misite.com/bydefault.jpg" alt="http://www.misite.com/bydefault.jpg" /></a></center>';
}
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
add_filter('the_content', 'inject_ad_text_after_n_chars');
答案 0 :(得分:0)
使用in_category
条件标记,例如:
if ( in_category( 1 ) ) {
$text = '<img src="http://example.com/banner-1.png">';
} elseif ( in_category( 3 ) ) {
$text = '<img src="http://example.com/banner-3.png">';
} else {
$text = '<img src="http://example.com/banner-default.png">';
}