无需在WordPress管理员中添加页面即可获取动态URL

时间:2018-12-12 15:29:23

标签: php wordpress .htaccess templates url-redirection

我有这个网址:

example.com/parent /

我已经在WordPress中创建了此页面,它使用了模板page-parent.php

有没有一种方法可以在使用此模板的情况下动态创建页面,而无需向WordPress添加页面,我知道我可以使用查询变量

例如example.com/parent/?q=17

但是我想知道是否可以拥有

example.com/parent/child1 /

example.com/parent/child2 /

无需将它们添加为'Pages'并使用父模板。

3 个答案:

答案 0 :(得分:0)

  1. 在主题文件夹中创建一个名为“ page-templates”的子文件夹
  2. 将“ parent.php”代码移至该文件夹中
  3. 将此行添加到parent.php:<?php /* Template Name: Example Template */ ?>的顶部,并使用您要使用的任何名称。
  4. 在Wordpress中,您应该能够在页面编辑器中选择该模板。

答案 1 :(得分:0)

感谢@cabrerahector-我一直在寻找add_rewrite_rule(),我只需要添加这些功能并刷新我的永久链接。

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

function custom_rewrite_rule() {
    add_rewrite_rule('^parent/([^/]*)/?','index.php?page_id=207&child=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

答案 2 :(得分:0)

当前,您的page-parent.php是特定于该页面的模板(由于页面文件与文件名匹配)。但是,您可以将现有的page-parent.php文件转换为可用于任意数量页面的模板。

  1. 将page-parent.php文件重命名为template-parent.php
  2. 将此行添加到template-parent.php的顶部:(将“父页面”替换为您要使用的模板名称)。
  3. 在WordPress管理员中,为“父”页面和您希望用于其的所有其他子页面选择此新模板。