【问题标题】:How to include the post title in the post content in wordpress?如何在wordpress的帖子内容中包含帖子标题?
【发布时间】:2013-12-04 19:38:41
【问题描述】:

如果帖子标题是“概念” 我希望帖子内容是这样的 “在“职位标题”中,我们通过遵循“职位标题”高质量政策来关心我们的客户满意度” 所以我希望帖子内容包含“概念”并将其替换为帖子标题 我认为 wordpress 短代码 api 可以做类似的事情。但我不知道怎么做?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我不完全理解你的说法。但我认为 wordpress 钩子可以帮助你做到这一点 你可以试试这样的:

    function change_post_title ( $post_title ) {
       if ( $post_title ==  'conecpts'  ) {
         $title = 'The new title'; 
         return $title; 
     }
     return $post_title;
    }
    
    add_filter('the_title', 'change_post_title');
    

    如果标题是“概念”,这会将帖子标题更改为“新帖子标题”

    请明确您的问题是什么以及您想要完成什么。

    【讨论】:

      【解决方案2】:

      在您的帖子内容中,您需要在要打印标题的位置添加一个简码,下面的简码示例将使用[post_title]

      function post_title_shortcode( $atts ){
           extract( shortcode_atts( array(
              'post_id' => get_the_ID()
              ), $atts ) );
      
           return get_the_title( $post_id );
      }
      add_shortcode( 'post_title', 'post_title_shortcode' );
      

      使用该简码,您可以传递帖子 ID,这样如果您想获取另一篇帖子的标题,您可以使用简码 [bartag post_id="123"],其中 123 是您要显示的标题的帖子 ID。

      【讨论】:

      • 如果我想在帖子内容中传递当前帖子标题怎么办(我的意思是不使用 atts)。因此短代码变得更加通用,可以在任何帖子中使用
      猜你喜欢
      • 2020-04-15
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多