php 如何使用简码在内容中包含wordpress帖子的页面标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16079490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include page title of wordpress post in the content possibly with a shortcode
提问by scott
in the wordpress admin, i would like to do the following when creating a page:
在 wordpress 管理员中,我想在创建页面时执行以下操作:
Page Title: Test
页面标题:测试
Page content:
页面内容:
Lorem ipsum dolor [page_title]sit amet, consectetur adipiscing elit. Nunc et lectus sit amet ante vulputate ultrices at sit amet [page_title]tortor. Nam mattis commodo mi in semper. Suspendisse ut eros dolor. Morbi at odio feugiat [page_title]nunc vestibulum venenatis sit amet vitae neque. Nam ullamcorper ante ac risus malesuada id iaculis nibh ultrices.
Lorem ipsum dolor [page_title]坐在 amet,consectetur adipiscing 精英。Nunc et lectus 坐在 amet ante vulputate ultrices at sat amet [page_title]Tortor。Nam mattis commodo mi in semper。Suspendisse ut eros dolor。Morbi at odio feugiat [page_title]nunc 前庭 venenatis 坐在 amet vitae neque。Nam ullamcorper ante ac risus malesuada id iaculis nibh ultrices。
Where it says [page_title] I would like it to print the page title (Test)
它说 [page_title] 我希望它打印页面标题(测试)
This needs to be achieved through the admin system, not hard-coded in the template.
这需要通过管理系统来实现,而不是在模板中进行硬编码。
回答by Sepster
Refer to the codex: Shortcode API
参考代码:Shortcode API
function myshortcode_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );
Add this to your theme's functions.php file.
将此添加到主题的functions.php 文件中。
Note that per the comments exchange between S.Visser and I in his answer - this solution will only work inside The Loop, while his will also work outside The Loop and so his is the more complete answer.
请注意,根据 S.Visser 和我在他的回答中的评论交流 - 此解决方案仅适用于 The Loop,而他的也适用于 The Loop 之外,因此他的答案更完整。
回答by S.Visser
Add this to your theme, or make an plugin from it.
将此添加到您的主题中,或从中制作插件。
/* 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');
回答by Ashwin
Found this solution on web hope it will help others who face the same problem as mine. Just add the below code to functions.php file or to the page_title plugin .php file.
在网上找到了这个解决方案,希望它能帮助和我遇到同样问题的其他人。只需将以下代码添加到 functions.php 文件或 page_title 插件 .php 文件中。
add_filter('get_the_excerpt', 'show_shortcode_in_excerpt');
add_filter('the_excerpt', 'show_shortcode_in_excerpt');
function show_shortcode_in_excerpt($excerpt) {
return do_shortcode(wp_trim_words(get_the_content(), 55));
}