【问题标题】:Dynamically Insert page title into every page on Wordpress将页面标题动态插入到 Wordpress 上的每一页中
【发布时间】:2016-11-01 13:53:57
【问题描述】:

我在 Wordpress 上完成了我网站的所有页面。

例如。
题目一:如何在 Wordpress 上建站
标题 2:如何保护 WP 网站

现在我想将此行添加到底部的所有页面:
“如果您对 [页面标题] 有任何疑问,请在下方发表评论!”

我期望的结果应该是:
第 1 页内容:
第 1 步
第 2 步
如果您对如何在 Wordpress 上建立网站有任何疑问,请在下方发表评论!

第 2 页内容
第 1 步
第 2 步
如果您对如何保护 WP 网站有任何疑问,请在下方发表评论!

我怎样才能做到这一点?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以通过the_content 过滤器更改页面内容 看看下面的代码

    add_filter( 'the_content', 'add_bottom_content' );
    function add_bottom_content( $content ) {
        $content .=  "If you have any question on ".get_the_title().", please give a comment below!";
        return $content;
    }
    

    【讨论】:

    • 效果很好。有什么方法可以让它在管理面板中与 Visual Composer 一起工作?
    • 可以,请查看content_edit_pre过滤器codex.wordpress.org/Plugin_API/Filter_Reference/…
    • 你能解释一下吗?我想使用 Visual Composer 创建以上内容(“如果您对 [页面标题] 有任何问题,请在下面发表评论!”)然后将其插入到每个页面。谢谢
    【解决方案2】:

    你可以使用 wp 函数the_title()

    the_title(string $before = '', string $after = '', bool $echo = true)

    显示或检索带有可选内容的当前帖子标题。

    参数#Parameters

    $之前

    (string) (Optional) Content to prepend to the title.
    
    Default value: '' 
    

    $之后

    (string) (Optional) Content to append to the title.
    
    Default value: '' 
    

    $回声

    (bool) (Optional) default to true.Whether to display or return.
    
    Default value: true
    

    即:

    the_title("If you have any question on ", ", please give a comment below!")
    

    【讨论】:

      【解决方案3】:

      您可以在页面模板中添加一些 php 代码。

      我以二十六主题为例。在 wp-content/themes/twentysixteen/template-parts/content-page.php

      在 entry-content div 中添加 If you have any question on <?php the_title(); ?>, please give a comment below!

      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <header class="entry-header">
              <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
          </header><!-- .entry-header -->
      
          <?php twentysixteen_post_thumbnail(); ?>
      
          <div class="entry-content">
              <?php
              the_content();
      
              wp_link_pages( array(
                  'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentysixteen' ) . '</span>',
                  'after'       => '</div>',
                  'link_before' => '<span>',
                  'link_after'  => '</span>',
                  'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>%',
                  'separator'   => '<span class="screen-reader-text">, </span>',
              ) );
              ?>
              If you have any question on <?php the_title(); ?>, please give a comment below!
      
          </div><!-- .entry-content -->
      
          <?php
              edit_post_link(
                  sprintf(
                      /* translators: %s: Name of current post */
                      __( 'Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen' ),
                      get_the_title()
                  ),
                  '<footer class="entry-footer"><span class="edit-link">',
                  '</span></footer><!-- .entry-footer -->'
              );
          ?>
      
      </article><!-- #post-## -->
      

      【讨论】:

        猜你喜欢
        • 2016-08-21
        • 1970-01-01
        • 2022-10-06
        • 1970-01-01
        • 1970-01-01
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多