【问题标题】:Wordpress get_permalink() PHP Link not working for external links in postsWordpress get_permalink()PHP链接不适用于帖子中的外部链接
【发布时间】:2014-02-17 16:50:55
【问题描述】:

在我的子主题中使用 wordpress 并编辑一些 PHP 代码,但在热门帖子的侧边栏小部件方面遇到了问题。

wordpress 中每篇受欢迎的博客文章都会准确显示在热门文章侧边栏中,单击故事标题将转到我博客上的单个故事页面

此小部件中链接到我自己在 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>';
    }

由于这是热门文章的侧边栏小部件,我希望侧边栏点击直接转到每篇文章的外部网站,而不是我的博客文章。

在每篇博客文章中都有一个直接外部链接,指向每篇文章的实际文章。 每个帖子都标记为以下代码 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 URL 的任何想法?谢谢。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    阅读http://codex.wordpress.org/Function_Reference/get_permalink

    在循环内是当前帖子。 在循环外,您需要一个帖子 ID。

    但这是您博客上的帖子。

    我认为解决方案比您想象的要容易。抓住网址,并将其粘贴在href中。你可能想修剪它。 (post meta 有点垃圾。)

    【讨论】:

    • 罗布,谢谢您的评论。问题是我不能在 href 中放置直接 URL,因为它是基于帖子的动态的。考虑到内部/外部循环,我认为我做得对,包括 $url = get_post_meta( $post->ID, 'article_out_url', true );在 PHP 文件中。有什么想法吗?
    【解决方案2】:

    get_permalink() 仅适用于 WordPress 帖子和页面,不适用于外部链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      相关资源
      最近更新 更多