在我的孩子主题中使用wordpress并编辑一些PHP代码,并且遇到了热门帖子的侧边栏小部件的问题 .
wordpress中的每个博客文章都会在流行的帖子边栏中准确显示,点击故事 Headers ,它将转到我博客上的单个故事页面 .
此小部件中的正确代码链接到widgets.php中我自己的博客帖子是:
if ( $popular_posts->have_posts() ) {
$result .= '<div class="post-popular"><ul class="list">';
while ( $popular_posts->have_posts() ) {
$popular_posts->the_post();
$comments_text = sprintf( _n( '%1$s comment', '%1$s comments', get_comments_number(), APP_TD ), get_comments_number() );
$result .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a> - ' . $comments_text . '</li>';
}
$result .= '</ul></div>';
}
由于这是热门文章的侧边栏小部件,我希望侧边栏点击直接转到每篇文章的 external site ,而不是我的博文 .
在每篇博文中都有一个直接 external 链接到每个博文的实际文章 . 这是为每个帖子标记为以下代码 article_out_url
我为get_permalink()尝试了以下代码编辑,使get_permalink($ url)转到每个外部,例如:
$url = get_post_meta( $post->ID, 'article_out_url', true );
$result .= '<li><a href="' . get_permalink($url) . '">' . get_the_title() . '</a> - ' . $comments_text . '</li>';
如下:
if ( $popular_posts->have_posts() ) {
$result .= '<div class="post-popular"><ul class="list">';
while ( $popular_posts->have_posts() ) {
$url = get_post_meta( $post->ID, 'article_out_url', true );
$popular_posts->the_post();
$comments_text = sprintf( _n( '%1$s comment', '%1$s comments', get_comments_number(), APP_TD ), get_comments_number() );
$result .= '<li><a href="' . get_permalink($url) . '">' . get_the_title() . '</a> - ' . $comments_text . '</li>';
}
$result .= '</ul></div>';
}
但是,这不起作用 . 点击链接只是转到我的根域,而不是转到外部链接 . 甚至没有关于该主题的个人帖子 .
我很难过 . 有关如何将每篇文章链接转到每篇文章的article_out_url网址的任何想法?谢谢 .
2 回答
阅读http://codex.wordpress.org/Function_Reference/get_permalink
内部循环它是当前的帖子 . 外部循环你需要一个帖子ID .
但这是你博客上的一篇文章 .
我认为解决方案比你想象的要容易 . grab 网址,并将其粘贴在href中 . 你可能想修剪它 . (post meta在垃圾邮件中有点垃圾 . )
get_permalink()仅适用于WordPress帖子和页面,而不适用于外部链接 .