【问题标题】:How to include page title of wordpress post in the content possibly with a shortcode如何使用简码在内容中包含 wordpress 帖子的页面标题
【发布时间】:2013-04-11 08:43:27
【问题描述】:

在 wordpress 管理员中,我想在创建页面时执行以下操作:

页面标题:测试

页面内容:

Lorem ipsum dolor [page_title] 坐下来, consectetur adipiscing elit。 Nunc et lectus sat amet ante vulputate ultrices at sat amet [page_title] 侵权。在 semper 中的 Nam mattis commodo mi。 Suspendisse ut eros dolor。 Morbi at odio feugiat [page_title] nunc vestibulum venenatis 坐在 amet vitae neque。 Nam ullamcorper ante ac risus malesuada id iaculis nibh ultrices。


上面写着 [page_title] 我希望它打印页面标题(测试)

这需要通过管理系统来实现,而不是硬编码在模板中。

【问题讨论】:

标签: php html wordpress shortcode


【解决方案1】:

将此添加到您的主题中,或从中制作插件。

/* 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');

【讨论】:

  • 嘿,顺便说一句,你得到了我的 +1。这是在做我的回答不会做的事情吗?两者都只有在循环内的 the_content 期间调用时才有效,是吗?
  • 是的,两者都可以在循环中工作,我建议的那个也可以在循环之外工作。 get_the_title 只能在内部工作。
  • 好极了。谢谢,S. Visser!
【解决方案2】:

参考法典:Shortcode API

function myshortcode_title( ){
   return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );

将此添加到主题的 functions.php 文件中。

请注意,根据 S.Visser 和我在他的回答中的 cmets 交换 - 此解决方案仅适用于 The Loop 内部,而他的也适用于 The Loop 外部,因此他的答案更完整。

【讨论】:

  • 更简洁,使用更短。有用的功能非常感谢
  • @scott btw 更新了答案以删除 $atts 参数,因为您在此示例中没有使用任何短代码属性。
【解决方案3】:

在网上找到了这个解决方案,希望它能帮助其他面临同样问题的人。只需将以下代码添加到 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));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2020-04-15
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多