我有一个 a.php 文件,我在其中定义了<a href="abc.com/page_id=11?key=123">link</a>
我创建了一个自定义模板 custom.php ,并在wordpress后端创建了一个页面 page_id = 11
所以,从 a.php 我在 page_id的帮助下将用户发送到 custom.php ,然后获取密钥= 123
这很好..
唯一的问题是我不确定 page_id = 11 。
是否可以使用短代码获得相同的内容,以避免在模板文件中使用 page_id 。
答案 0 :(得分:1)
创建shortcode
以获取page id
function id_shortcode( $atts ) {
// extract the variables, if there is no ID attr, the default is 1
extract( shortcode_atts( array(
'id' => 1
), $atts ) );
// set page id to either the id attr which defaults to one.
$page_id = $id;
$page_data = get_page( $page_id );
return
}
// shortcode registration
add_shortcode( 'getid', 'id_shortcode' );
然后,您只需在页面上使用 [getid]
shortcode
即可获得page id
我希望我理解并解决了你的问题...
您也可以在模板文件中调用此短代码
<?php echo do_shortcode("[getid]"); ?>