嗨我对php很新,并开始搞乱一些短代码。我创建了一个快速短代码,它返回一个包含html的字符串。我希望它由浏览器执行。几乎所有的都被正确执行,除了实际打印出来的链接,如
<a href="#" target="_blank">Example</a>
这是我写的短代码
add_shortcode('booklist_jumbo','generate_booklist_jumbo');
//[booklist_jumbo]
function generate_booklist_jumbo($atts)
{
$a = shortcode_atts( array(
'category' => 'Upcoming',
'name' => '',
'list_order' => 'DESC'
), $atts );
$bookOrder = $a[book_order];
$listOrder = $a[list_order];
if($a[name] !== '') {
$name = $a[name];
$second_db = new wpdb(DB_USER, DB_PASSWORD, "saic3_LibraryIndex", DB_HOST);
$query = "SELECT * FROM `LibraryList` WHERE `list_name` = %s ORDER BY date_created $listOrder LIMIT 1";
$booklist = $second_db->get_results( $second_db->prepare( $query, $name) );
}
else if($a[category] !== "") {
$cat = $a[category];
$second_db = new wpdb(DB_USER, DB_PASSWORD, "saic3_LibraryIndex", DB_HOST);
$query = "SELECT ll.* FROM LibraryListCategory llc INNER JOIN LibraryListCategoryList llcl ON llc.category_id = llcl.fk_category_id INNER JOIN LibraryList ll ON llcl.fk_list_id = ll.list_id WHERE llc.name= %s ORDER BY ll.date_created $listOrder LIMIT 1";
$booklist = $second_db->get_results( $second_db->prepare($query, $cat) );
}
if ($booklist!=null) {
$booklist[0]->list_name = stripslashes($booklist[0]->list_name);
$code = strval($booklist[0]->list_type_code);
$msg = stripslashes($booklist[0]->banner_msg);
$msg = str_replace("\\n", "<br>", $msg);
switch($code) {
case "1": $link = "/booklist-details/".$booklist[0]->fk_list_id; break;
case "2": $link = "/booklist-dvd-details/".$booklist[0]->fk_list_id; break;
case "3": $link = "/booklist-audio-details/".$booklist[0]->fk_list_id; break;
}
$content = '<div class="bootstrap-container"><div class="row"><div class="col-md-12"><div class="jumbotron container-fluid" style="height: 600px; width: 100%; background: url('.$booklist[0]->image_url.') no-repeat center center; background-size: cover;">';
$content.= '<a href="'.$link.'" target="_blank" ><h2 class="book-feat-title center-block">'.$booklist[0]->list_name.'</h2></a><p class="book-feat-msg">'.$msg.'</p>';
$content.= '</div></div></div></div>';
}
else {
$content="<h4>no booklist records</h4>";
}
$second_db->close();
return $content;
}
除了包含在p标签中的横幅消息之外,一切都按预期工作...我尝试查找类似的东西,但似乎找不到任何东西。我猜这与它被认为是一个字符串而不是html有关,但我不知道如何解决它。
答案 0 :(得分:0)
请为$msg
$msg = str_replace("\\n", "<br>", $msg);
$msg = add_filter( 'the_content', $msg );