【问题标题】:Issues with WP Shortcode output wrapping around contentWP Shortcode 输出环绕内容的问题
【发布时间】:2018-05-07 17:15:17
【问题描述】:

我想在 WordPress 中围绕 get_template_part('path/file') 包装一个打开和关闭简码函数。它不是在 HTML 输出中环绕代码,而是显示在内容下方。

下面的例子:

PHP 输出:

<?php 
function get_products($atts = [], $output = null) { 

    PHP outout:
    $output= get_template_part('partials/modules/content', 'fields');
    $output .= '[shortcode]'. $output .'[/shortcode]';
    $output .= ob_get_clean();
    return $output;
    }

add_shortcode('resources', 'get_products');
?>

<?php echo do_shortcode('[resources]'); ?>

HTML 输出:

<div class="content">
</div>

<div class="shortcode">
</div>

HTML 所需的输出:

<div class="shortcode">
<div class="content">
</div>
</div>

【问题讨论】:

标签: php wordpress templates shortcode


【解决方案1】:

试试看。

function my_template( $attr ) {
    ob_start();
    get_template_part( 'partials/modules/content' );
    return ob_get_clean();
}

add_shortcode( 'template', 'my_template' );

function get_products($atts = [], $output = null) { 

    return "
        <div class='shortcode'>
            ".do_shortcode('[template]')."
        </div>
    ";
}

add_shortcode('resources', 'get_products');

【讨论】:

  • 看起来它仍然没有环绕内容。还有其他建议吗?
  • @PaulSolomon 试试这个新的例如
猜你喜欢
  • 1970-01-01
  • 2017-04-07
  • 1970-01-01
  • 2022-11-17
  • 2019-05-30
  • 1970-01-01
  • 2011-08-04
  • 2018-01-11
  • 1970-01-01
相关资源
最近更新 更多