wordpress目前在使用中,短码占据着很重要的位置。无论你是网站主题的设计还是在日常应用中,短码无处不在。如VC,Elementor Pro等可视化编辑器,其实他们都是通过短码来实现的。短码的基础应用网上有很多。我们就不多介绍了。我们通过本文来说明在短码中的一些高级应用。希望对您的开发过程有所帮助。

快速了解WordPress短码

如果您了解WordPress短代码,您就知道它们是将一些编程添加到文章正文中的简单方法。只需将[our_example_shortcode]之类的内容插入到文章的主体中,我们就可以用函数的某些输出替换它。它是如此美丽和有用。替换[our_example_shortcode]的代码可以简单如下:

The WordPress Toolbox
Unlimited Downloads: 500,000+ WordPress Themes, Plugins, Templates & Design Assets

WordPress 短码高级应用之PHP Output Buffering多种用法

DOWNLOAD NOW

add_shortcode(
    'our_example_shortcode',
    'iesay_example_shortcode_function'
);
function iesay_example_shortcode_function() {
    return 'I show up where you called the shortcode!';
}

通常情况下,只需使用短代码的名称和函数的名称调用add_shortcode(这有点像一个特殊情况add_filter或者add_action)。

如果我们要在短码中输出文章内容或是html长代码。我们用传统的echo,就无能为力了。这个时候我们就需要用PHP Output Buffering来引用各种内容了。

我们将通过下面的例子来详细说明。

目标:在短代码中输出复杂模板

我们在此演示中的目标是创建一个可以输出相对复杂的HTML / PHP模板的短代码。此模板位于其自己的文件中,post-quickview.php位于我们主题的子文件夹中custom-templates:如下面的例子,我们通过短码来输出文章标题和摘要等。需要输出的代码为:

// Environment: This is the post-quickview.php file inside the custom-templates folder.
<div class="post-quickview">
  <h1 class="quickview-title"><?php the_title(); ?></h1>
  <div class="quickview-featured-image"><?php the_post_thumbnail( 'large' ); ?></div>
  <?php the_excerpt(); ?>
</div>

要通过短码来输出上的内容。用echo基本无法实现。例如我们需要通过短码[quickview_post post_ID='12345']来输出,我们就需要以下的代码:

add_shortcode( 'quickview_post', 'quickview_post' );
function quickview_post( $atts ) {
  // If we didn't choose a post ID, return
  if( ! $atts || ! $atts['post_id'] ) {
    return 'No post ID specified.';
  }

  // Fetch current post object and set the global $post to point to it
  $this_post = get_post( $atts['post_id'] );
  global $post;
  $post = $this_post;
  setup_postdata( $post );

  // Start output buffering so get_template_part doesn't output to the page
  ob_start();
    // Get template file output
    get_template_part( 'custom-templates/post-quickview' );
  // Save output and stop output buffering
  $output = ob_get_clean();

  // Return $post to its original state
  wp_reset_postdata();

  // Return buffered output to be output in shortcode location
  return $output;
}

同理,你有可以通过这个例子来扩展到所有wordpress hook中。如下面的例子。就是通过PHP Output Buffering来为文章末尾添加内容。代码如下:

// PHP output buffering for the WordPress the_content filter hook
add_filter( 'the_content', 'iesay_add_single_post_footer' );
function iesay_add_single_post_footer( $content ) {
  // Only on single Posts
  if( ! is_singular( 'post' ) ) {
    return $content;
  }

  // Start output buffering so we can "write" to the page
  ob_start(); ?>

  <hr />
  <div class="single-footer">
    <h3>Footer section</h2>
    <p>I can do all kinds of HTML here</p>
    <table>
      <tr>
        <td>How about a table?</td>
        <td>That seems</td>
        <td>Easy enough</td>
      </tr>
    </table>
  </div>

  <?php

  // Get our output string and save to a variable
  $footer = ob_get_clean();

  // Return the string appended to the content
  return $content . $footer;
}

明白了吗?当然这个PHP Output Buffering用在各种函数中,更多的可以自己去体会吗。

发表回复