简体   繁体   English

Wordpress get_permalink()PHP链接不适用于帖子中的外部链接

[英]Wordpress get_permalink() PHP Link not working for external links in posts

Using wordpress and editing some PHP code in my child theme and have run into an issue with a sidebar widget for popular posts. 在我的孩子主题中使用wordpress并编辑一些PHP代码,并且遇到了热门帖子的侧边栏小部件的问题。

Each blog post in wordpress that is popular accurately shows up in the popular posts sidebar, and clicking the story title it will go to the single story page on my blog . wordpress中的每个博客文章都会在流行的帖子边栏中准确显示,点击故事标题,它将转到我博客上的单个故事页面。

The correct code in this widget to link to my own blog posts in widgets.php is: 此小部件中的正确代码链接到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>';
    }

Since this is a sidebar widget of popular articles, I would like the sidebar clicks to go directly to the external site for each article, and not my blog posts. 由于这是热门文章的侧边栏小部件,我希望侧边栏点击直接转到每篇文章的外部网站 ,而不是我的博客文章。

Within each blog post is a direct external link to the actual article for each. 在每篇博文中都有一个直接的外部链接,指向每个博文的实际文章。 This is tagged for each post as the following code article_out_url 这是为每个帖子标记为以下代码article_out_url

I tried the following code edit for get_permalink() to make it get_permalink($url) to go to each external such such as: 我为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>';

Such as the following: 如下:

    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>';
    }

However, this didn't work. 但是,这不起作用。 Instead of going to the external link, clicking on the link simply went to my root domain only. 点击链接只是转到我的根域,而不是转到外部链接。 Not even the individual post on the topic. 甚至没有关于该主题的个人帖子。

I'm stumped. 我很难过。 Any ideas with how I can get each article link to go to the the article_out_url URL for each post? 有关如何将每篇文章链接转到每篇文章的article_out_url网址的任何想法? Thanks. 谢谢。

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

inside loop it's the current post. 内部循环它是当前的帖子。 outside loop you need a post id. 外部循环你需要一个帖子ID。

but it's a post on your blog. 但这是你博客上的一篇文章。

i think the solution is easier than you imagine. 我认为解决方案比你想象的要容易。 grab the url, and stick it in the href. 抓住网址,并将其粘贴在href中。 you may want to trim it. 你可能想修剪它。 (post meta is a bit junk in junk out.) (post meta在垃圾邮件中有点垃圾。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM