0

我尝试在帖子中按类别添加横幅。我得到了以下代码,它在 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>&nbsp

';
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');  
4

1 回答 1

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">';
}
于 2013-05-12T20:01:11.460 回答