Wordpress-根据URL中的变量显示变量内容 [英] Wordpress - Display variable content based on variable in URL

查看:57
本文介绍了Wordpress-根据URL中的变量显示变量内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个wordpress平台,我正在考虑基于url中的变量在页面上显示变量内容.

I have a wordpress platform and I am looking at displaying variable content on a page based on a variable in the url.

我有一个1页的网站(www.vidlab.io),如果URL为vidlab.io/?type=GoPro,或者我希望婚礼的信息,我希望"GoPro视频编辑"部分显示有关GoPro视频编辑的信息编辑网址是否为vidlab.io/?type=wedding,依此类推.我读过您可以使用get调用,但不确定如何在没有得到404的情况下在URL中获取该信息.

I have a 1 page website (www.vidlab.io) and i would like the section "GoPro Video Editing" to display information about GoPro video editing if the url is vidlab.io/?type=GoPro or information about wedding editing if the Url is vidlab.io/?type=wedding, and so on. I have read that you can use a get call, but not really sure how to have that information in the URL without getting a 404.

对要使用的插件有什么想法吗?还是我可以插入页面中以获得结果的PHP?

Any ideas on plugins to use? Or PHP that I can insert into the page to get this result?

推荐答案

那很容易.

您必须使用两个Wordpress功能.

You have to use two Wordpress functions.

add_rewrite_rule()

add_rewrite_rule()

add_rewrite_tag()

add_rewrite_tag()

首先,将 add_rewrite_tag()函数包含在主题的 functions.php 中.

First you include the add_rewrite_tag() function into your functions.php of your theme.

例如:

function custom_rewrite_tag() {
  add_rewrite_tag('%food%', '([^&]+)');
  add_rewrite_tag('%variety%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

此后,您必须选择一页包含您的内容的wordpress.您必须使用此页面的页面ID.

After that you have to choice one page of wordpress which contains your content. You have to use the page ID of this page.

在您的 functions.php 中包含 add_rewrite_rule().

例如:

 function custom_rewrite_rule() {
    add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');
  }
  add_action('init', 'custom_rewrite_rule', 10, 0);

通知!:

此后,您必须转到您的设置->永久链接,只需按保存按钮即可清理永久链接缓存.

After this you have to go to your settings -> permalinks and only push the save button to clean your permalink cache.

重要提示:不要忘记刷新并重新生成重写规则修改规则后的数据库.在WordPress管理屏幕中,选择设置"->永久链接",然后单击保存更改而不添加任何内容"变化.

IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

我的示例来自wordpress的法典页面:

My example is from the codex page of wordpress:

https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

https://codex.wordpress.org/Rewrite_API/add_rewrite_tag

这篇关于Wordpress-根据URL中的变量显示变量内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆