我可以在模板中使用WP块吗?

时间:2022-01-21 作者:paulwk

我不确定如何解释这一点,但我有一个使用ACF构建的滑块,我把它放在了主页上。在这段代码之后,我想使用我构建的WP块,然后我想放置另一个滑块。所以看起来是这样的:

        <div class="col-lg-9 col-sm-12">
        
            <div class="row slide-area">    

              <!-- // * Slider starts -->
              <section class="slider_container_top_main">

              <!-- all slides container starts -->
              <ul class="slider_container_top">

                <?php

                      // get slider data
                      if (have_rows(\'slider\')) :

                          while (have_rows(\'slider\')) : the_row();

                              $slider_image = get_sub_field(\'slider_image\');
                              $slider_headline = get_sub_field(\'slider_headline\');
                              
                      ?>

                <!-- slide starts -->
                <li>
                  <div class="slider_container_top_img_container">            

                    <img src="<?php echo $slider_image[\'url\'] ?>" alt="slider image" class="slider_container_top_img"
                      loading="lazy">          

                    <div class="row align-items-center justify-content-center top-navigation-div">
                      
                      <div class="col-lg-9 offset-lg-3 col-sm-12 col-12 black-s top-navigation-controls"><?php echo $slider_headline; ?></div>
                    </div>
                  </div>
                </li>
                <!-- slide ends -->

                <?php

                  endwhile;
                  endif;
                      
                ?>

              </ul>
              <!-- all slides container ends -->

              </section>
              <!-- // * Slider ends -->
              
            </div>
            
          
        </div>
      </div>

** WP block would go here **

// And here\'s another slider

        <div class="col-lg-9 col-sm-12">
        
            <div class="row slide-area">    

              <!-- // * Slider starts -->
              <section class="slider_container_top_main">

              <!-- all slides container starts -->
              <ul class="slider_container_top">

                <?php

                      // get slider data
                      if (have_rows(\'slider\')) :

                          while (have_rows(\'slider\')) : the_row();

                              $slider_image = get_sub_field(\'slider_image\');
                              $slider_headline = get_sub_field(\'slider_headline\');
                              
                      ?>

                <!-- slide starts -->
                <li>
                  <div class="slider_container_top_img_container">            

                    <img src="<?php echo $slider_image[\'url\'] ?>" alt="slider image" class="slider_container_top_img"
                      loading="lazy">          

                    <div class="row align-items-center justify-content-center top-navigation-div">
                      
                      <div class="col-lg-9 offset-lg-3 col-sm-12 col-12 black-s top-navigation-controls"><?php echo $slider_headline; ?></div>
                    </div>
                  </div>
                </li>
                <!-- slide ends -->

                <?php

                  endwhile;
                  endif;
                      
                ?>

              </ul>
              <!-- all slides container ends -->

              </section>
              <!-- // * Slider ends -->
              
            </div>
            
          
        </div>
      </div>
有没有办法做到这一点?在模板中的代码之间放置WP块?

2 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

2种方法

  1. do_blocks, 将块内容作为字符串,对其进行解析,渲染每个块,然后返回最终渲染的内容。这与中的内容相同post_content, 又名HTML注释和其中的HTML标记,例如类似于以下内容:echo do_blocks( \'<!-- wp:paragraph --><p>Hello world.</p><!-- /wp:paragraph -->\');
SO网友:tiago calado

您可以通过另一种方式来实现,对页面使用wp块,并将代码作为短代码插入。

function my_shortcode() {
    ob_start(); <?php
    <h3> this is my shortcode </h3>
    ?>
    return ob_get_clean();
}
add_shortcode( \'myShortCodeName\', \'my_shortcode\');
现在在wp块上,可以使用shortcode块并调用[myShortCodeName]

相关推荐