【问题标题】:render Visual Composer shortcodes onto page将 Visual Composer 短代码呈现到页面上
【发布时间】:2018-02-15 11:03:14
【问题描述】:

我正在尝试将echo 视觉作曲家shortcodes 放到一个页面上。

我尝试了以下两种方法,但都不起作用:

functions.php:

方法一

/*
 * add shortcode file
 */
function include_file($atts) {
    $a = shortcode_atts( array(
        'slug' => 'NULL',
    ), $atts );

    if($slug != 'NULL'){
        ob_start();
        get_template_part($a['slug']);
        return ob_get_clean();
    }
}
add_shortcode('include', 'include_file');

方法二

function someshortocode_callback( $atts = array(), $content = null ) {

    $output = "[vc_section full_width=\"stretch_row\" css=\".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}\"][vc_row 0=\"\"][vc_column offset=\"vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6\"][/vc_column][/vc_row][/vc_section]";
    return $output;
}
add_shortcode('someshortocode', 'someshortocode_callback');

file_rendering_vc_shortcodes.php:

方法一

<?php if ( is_plugin_active( 'js_composer/js_composer.php' ) ) {
    wc_print_notice('js_composer plugin ACTIVE', 'notice');
    echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>

结果

  • js_composer 插件激活
  • 简码在页面上,带括号,原样

方法二

<?php $post = get_post();

if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
// Visual composer works on current page/post
    wc_print_notice('VC ON', 'notice');
    echo add_shortcode('someshortocode', 'someshortocode_callback');
} else {
    wc_print_notice('VC OFF', 'notice');
    //echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>

结果

  • VC OFF(显然,因为短代码中的vc_row 不存在)
  • 短代码不在页面上

shop-page.php

<?php
/**
Template Name:  Shop Page in theme
Preview Image:  #
Descriptions:   #
 * [vc_row][vc_column][/vc_column][/vc_row]
 */
?>
[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"][/vc_column][/vc_row][/vc_section]

是否可以在页面上呈现vc shortcodes,如果可以,如何实现?

【问题讨论】:

    标签: php wordpress shortcode visual-composer


    【解决方案1】:

    使用:

    WPBMap::addAllMappedShortcodes();
    

    然后像往常一样do_shortcode($content);

    简而言之,页面构建器由于性能原因不会注册短代码,除非它不是必需的。

    如果你的元素是由vc_mapvc_lean_map注册的,那么不需要使用add_shortcode函数,你可以使用WPBMap::addAllMappedShortcodes();来做任何事情,它会在渲染过程中调用一个短代码类回调,然后简码模板。

    【讨论】:

    • 今晚你是我的英雄!非常感谢!
    【解决方案2】:

    关于方法二。

    你必须在你的简码函数中使用do_shortcode()

    function someshortocode_callback( $atts = array(), $content = null ) {
        $output = '[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"]column text[/vc_column][/vc_row][/vc_section]';
    
        return do_shortcode( $output );
    }
    
    add_shortcode( 'someshortocode', 'someshortocode_callback' );
    

    我的测试站点上的工作示例:http://test.kagg.eu/46083958-2/

    页面仅包含[someshortocode]。上面的代码添加到functions.php

    方法 2 的代码中还有另一个错误:行

    echo add_shortcode('someshortocode', 'someshortocode_callback');
    

    无法工作,因为add_shortcode() 什么也不返回。这段代码应该如下:

    <?php $post = get_post();
    
    if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
    // Visual composer works on current page/post
        wc_print_notice('VC ON', 'notice');
    } else {
        wc_print_notice('VC OFF', 'notice');
        add_shortcode('someshortocode', 'someshortocode_callback');
        echo do_shortcode('[someshortocode]');
    }; ?>
    

    【讨论】:

    • 不,页面包含通常的文本:[someshortcode]。请看图:take.ms/lLMdL
    • 我的答案中的代码包含 do_shortcode()。请注意第4行:return do_shortcode($output);
    • 我看到了这一切......只是我试图让它以编程方式工作......请仔细阅读我的问题。
    • 我仔细阅读了您的问题。在您的方法 2 中有 2 个错误:1)您没有在 someshortocode_callback 中使用 do_shortcode(),2)“echo add_shortcode('someshortocode', 'someshortocode_callback');”行无法工作,因为 add_shortcode 什么也不返回。
    • 我已经用你的方法 2 的代码更正了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2017-03-30
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多