如何添加<a href=""> to link other post in Wordpress

时间:2015-10-09 09:49:12

标签: php wordpress wordpress-plugin wordpress-theming

I am new to wordpress, I have searched this thing on net but unable to find exact solution.

I have created a post that contains the Actor's Profile and the list of Movies Actor has worked in. Each Movie has also a different Wordpress Post that contains Movie details.

Now i want to create a link on each Movie, by which user can view the details of that particular movie.

This following link gives the result

<a href="http://localhost/wordpress/2015/10/09/movie-main-page">

but if i add this in each post, then it would be very difficult for me in future, when i will be uploading the site on web server, to change each link individually on every post.

I believe that there must be some way out there better than what i doing here, but somehow i am unable to find that trick.

Kindly guide me.

Thanks

3 个答案:

答案 0 :(得分:1)

您可以使用get_site_url()模板标记返回网站网址并连接到字符串中。我还没有测试过这段代码,所以可能需要一些修改,但它应该让你开始:

    <?php echo '<a href="' . get_site_url() . '/2015/10/09/movie-main-page">' ?>

答案 1 :(得分:1)

如果您的电影页面网址相同,则可以使用

echo get_permalink('11');

此处11是页面/帖子的ID。

或者如果你的电影有不同的帖子可以有不同的网址。

您可以为每个帖子使用wp_query循环 [https://codex.wordpress.org/Class_Reference/WP_Query][1]

并使用 echo get_permalink();

答案 2 :(得分:0)

有两种方法可以实现您的目标。

  1. 第一种也是最简单的方法是在数据库上运行搜索和替换,将帖子中的任何网址更改为新网址。你应该看看https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

  2. 您还可以使用高级自定义字段来实现此目的。您需要使用post对象字段链接到wordpress中的另一个页面。使用帖子对象字段,您可以为链接到另一个帖子的每个帖子创建一个字段,然后您可以在模板中显示该字段。看看https://www.advancedcustomfields.com/resources/post-object/。使用此方法,您可以传输所有内容,相关链接仍然有效。