将Visual Composer短代码呈现到页面上。

14

我正在尝试将Visual Composer的shortcode通过echo输出到页面上。

我已经尝试了下面两种方法,但它们都不起作用:

functions.php:

方法1

/*
 * 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:

方法1

<?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插件已启用
  • 页面上带有括号的短代码原样使用

方法2

<?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已关闭(因为短代码中的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,如果可以的话,应该如何操作?

2个回答

2

使用:

WPBMap::addAllMappedShortcodes();

最初的回答是:通常情况下,需要使用do_shortcode($content);来执行短代码。简而言之,页面构建器出于性能考虑,除非必要,否则不会注册短代码。如果您的元素已由vc_mapvc_lean_map注册,则无需使用add_shortcode函数,您只需使用WPBMap::addAllMappedShortcodes();即可完成所有操作,它将在呈现过程中调用短代码类回调,然后调用短代码模板。

你今晚是我的英雄!非常感谢你! - plong0

0
关于方法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中。

在您的第二种方法的代码中还有另一个错误:行

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]。请查看图片:http://take.ms/lLMdL - KAGG Design
我的答案中包含 do_shortcode()。请注意第四行:返回 do_shortcode($output)。 - KAGG Design
我全都看见了……只是我正在努力使它在程序上运行……请仔细阅读我的问题。 - Jadeye
我仔细阅读了你的问题。在你的第二种方法中有两个错误:1)在someshortocode_callback中你没有使用do_shortcode(),2)行“echo add_shortcode('someshortocode','someshortocode_callback');”无法工作,因为add_shortcode不返回任何内容。 - KAGG Design
我已经使用您的代码扩展了答案,修正了第二种方法。 - KAGG Design
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接