我目前正在使用以下脚本在WordPress中显示我最近的帖子,但我无法将ASCII符号与超链接分开。
如果我将鼠标悬停在最近的帖子上,那么我会看到ASCII符号也是超链接的一部分。
<ul>
<?php
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a>></li> ';}
?>
</ul>
我怎么能这样:
Recent post 1&gt; Recent post 2&gt;
答案 0 :(得分:1)
只是遵循KISS原则:试试这个
<ul>
<?php
$args = array( 'numberposts' => '2' );
// get two most recent posts using wp_get_recent_posts()
$recent_posts = wp_get_recent_posts( $args );
// loop through both the posts
foreach( $recent_posts as $recent ){
// get permanent link from id using get_permalink()
$permalink = get_permalink($recent["ID"]);
// esc_attr is basically used for escaping the output
$title = esc_attr($recent["post_title"]);
// finally, echo the output
echo "<li><a href='$permalink' title='$title'>".$recent['post_title']."</a>></li>";
}
?>
</ul>
有关详细信息,请参阅官方文档网站上的wp_get_recent_posts
,get_permalink
和esc_attr
。
编辑:在Ofir Baruch的评论之后修改了答案。