sco*_*ott 6 html php wordpress shortcode
在wordpress管理员中,我想在创建页面时执行以下操作:
页面标题:测试
页面内容:
Lorem ipsum dolor [page_title]坐下来,奉献精神.Nunc et lectus坐在amet [page_title] tortor上的ante ante vulputate ultrices.semper的Nam mattis commodo mi.Suspendisse ut eros dolor.Mordi at odio feugiat [page_title] nunc vestibulum venenatis sit amet vitae neque.Nam ullamcorper ante ac risus malesuada id iaculis nibh ultrices.
在哪里说[page_title]我希望它打印页面标题(测试)
这需要通过管理系统来实现,而不是在模板中硬编码.
Sep*_*ter 19
请参阅codex:Shortcode API
function myshortcode_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );
Run Code Online (Sandbox Code Playgroud)
将其添加到主题的functions.php文件中.
请注意,根据S.Visser和我在他的回答中的评论交换 - 这个解决方案只能在The Loop中工作,而他的也可以在The Loop之外工作,所以他的答案更完整.
将此添加到您的主题中,或从中制作插件。
/* title to get the post title */
function getPageTitle() {
global $wp_query;
return get_post_title($wp_query->post->ID);
}
/* Add shortcode */
add_shortcode('page_title', 'getPageTitle');
Run Code Online (Sandbox Code Playgroud)