短代码只能在index.php上工作

时间:2015-07-20 07:29:36

标签: php wordpress wordpress-plugin

我已经下载了一些插件来在我的网站内呈现youtube搜索,短代码在页面/帖子中工作得很完美,但是当我在<?php echo do_shortcode('[soundcloud_wpress]');?>上使用index.php时,它没有#&# 39; t显示任何东西。

的index.php:

<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * e.g., it puts together the home page when no home.php file exists.
 *
 * Learn more: {@link https://codex.wordpress.org/Template_Hierarchy}
 *
 * @package WordPress
 * @subpackage Twenty_Fifteen
 * @since Twenty Fifteen 1.0
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

          <div class="row searchdiv-wrapper">

                <div class="col-sm-6 youtube-search">

                 <?php echo do_shortcode('[youtube_wpress]');?>
                </div>
                <div class="col-sm-6 soundcloud-search">

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

                </div> 

          </div>
        </main><!-- .site-main -->
    </div><!-- .content-area -->

<?php get_footer(); ?>

该插件添加短代码:

$GLOBALS['ygp_youtube_wpress'] = get_option('ygp_youtube_wpress');
$GLOBALS['ygp_youtube_wpress']['plugin_code'] = 'youtube_wpress';
$GLOBALS['ygp_youtube_wpress']['item_name'] = 'youtube_wpress';

//error_reporting(E_WARNING);

require_once dirname( __FILE__ ).'/include/vbox/include/webzone.php';
require_once dirname( __FILE__ ).'/activation.php';

$a1=new Youtube_wpress_activation();
if($a1->verify_activation()) {
    require_once dirname( __FILE__ ).'/youtube_widget.php';
}

if(is_admin()) {
    require_once dirname( __FILE__ ).'/admin/options.php';
}

class Youtube_wpress {

    function Youtube_wpress() {

        if(is_admin()) {
            register_activation_hook(__FILE__, array(__CLASS__, 'on_plugin_activation'));
            //Settings link
            add_filter( 'plugin_action_links', array(__CLASS__, 'plugin_action_links'), 10, 2);
        }

        //Shortcodes
        add_shortcode('youtube_wpress', array(__CLASS__, 'youtube_wpress_shortcode'));
    }

    function add_scripts_wp_footer() {

    }

我只添加了插件类中的第一个顶级代码。

任何想法为什么echo shortcode只能在index.php内工作?

感谢和抱歉初学者问题。

1 个答案:

答案 0 :(得分:0)

以下是有关如何使用do_shortcode()功能的一些说明:

<?php
add_filter( 'the_content', 'do_shortcode', 11 ); // From shortcodes.php

// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );

// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );


// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );


// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );


// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var; 
?>

WordPress提供的内置嵌入短代码有一个例外。要将此短代码与do_shortcode()一起使用,您可以执行以下操作:

<?php   
$embedurl = 'http://yourembeddableurl.com';
if ( ! empty( $embedurl ) ) {
    $var = apply_filters( 'the_content', "" );
    echo $var;
}
?>