如何从插件文件中调用Comments_Template();?

时间:2013-12-04 作者:Desi

我正在打电话comments_template(); 从插件文件。具体来说player.php 插件中的文件All Video Gallery. 下面是文件本身。

<?php

require_once(\'ismobile.php\');

/******************************************************************
/* Build Player
******************************************************************/
class Player {

    var $width, $height;

    public function buildPlayer( $profileid = 1, $videoid = 1, $autodetect = 1 ) {
        global $wpdb;       

        $profile      = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."allvideogallery_profiles WHERE id=".$profileid);     
        $this->width  = $profile->width;
        $this->height = $profile->height;

        if( isset($_GET[\'slg\']) && $autodetect == 1 ) {
            $slug    = $_GET[\'slg\'];
            $video   = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."allvideogallery_videos WHERE slug=\'$slug\'");
            $videoid = $video->id;
        } else {
            $video   = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."allvideogallery_videos WHERE  id=".$videoid);
        }   

        if($video->type == \'thirdparty\') {
            $result    = \'<div style="width:\' . $this->width . \'px; height:\' . $this->height . \'px;">\';
            $result   .= $video->thirdparty;
            $result   .= \'</div>\';
        } else {
            $arr_params = array();  
            $custpage   = ( $profile->custom_page_url ) ? $profile->custom_page_url : $this->curPageURL();
            if( isset($_GET[\'catid\']) ) {
                $arr_params[\'catid\'] = $_GET[\'catid\'];
            }
            if( isset($_GET[\'sort\']) ) {
                $arr_params[\'sort\'] = $_GET[\'sort\'];
            }       
            $flashvars = \'base=\'.get_option(\'siteurl\').\'&wp=1&vid=\'.$videoid.\'&pid=\'.$profileid.\'&page=\'.urlencode( @add_query_arg($arr_params, $custpage) );
            $detect    = new IsMobile();            
            $result    = $detect->isMobile() ? $this->gethtmlplayer( $profile, $video ) : $this->getflashplayer( $profile, $flashvars );    
        }       

        $this->updatehits( $video->slug );

        $cust_class = ( $profile->custom_player_class ) ? $profile->custom_player_class : \'avs_player\';

        $output  = \'<style type="text/css">\' . "\\n";
        $output .= $profile->player_css . "\\n";
        $output .= \'</style>\' . "\\n";
        $output .= \'<div class="\'.$cust_class.\'" style="width:\'.$this->width.\'px;">\' . "\\n";
        if($profile->title) {
            $output .= \'<div class="title">\'.$video->title.\'</div>\' . "\\n";
        }
        if($profile->category) {
            $output .= \'<div class="category"><strong>Category : </strong>\'.$video->category.\'</div>\' . "\\n";
        }
        if($profile->hits) {
            $output .= \'<div class="hits"><strong>Hits : </strong>\'.$video->hits.\'</div>\' . "\\n";
        }
        $output .= \'<div style="clear:both;"></div>\' . "\\n";
        $output .= $result . "\\n";
        if($video->description) {
            $output .= \'<div class="description">\'.$video->description.\'</div>\' . "\\n";
        }
        $output .= \'</div>\' . "\\n";

        return $output;
    }

    public function getflashplayer( $profile, $flashvars ) {
        global $allvideogallery_plugin_dir;

        $siteurl = get_option(\'siteurl\');
        $src     = $siteurl . \'/wp-content/plugins/\' . $allvideogallery_plugin_dir . \'/player.swf?random=\' . rand();

        $result  = \'<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="\' . $this->width . \'" height="\' . $this->height . \'">\';
        $result .= \'<param name="movie" value="\' . $src . \'" />\';
        $result .= \'<param name="wmode" value="opaque" />\';
        $result .= \'<param name="allowfullscreen" value="true" />\';
        $result .= \'<param name="allowscriptaccess" value="always" />\';
        $result .= \'<param name="flashvars" value="\' . $flashvars . \'" />\';
        $result .= \'<object type="application/x-shockwave-flash" data="\' . $src . \'" width="\' . $this->width . \'" height="\' . $this->height . \'">\';
        $result .= \'<param name="movie" value="\' . $src . \'" />\';
        $result .= \'<param name="wmode" value="opaque" />\';
        $result .= \'<param name="allowfullscreen" value="true" />\';
        $result .= \'<param name="allowscriptaccess" value="always" />\';
        $result .= \'<param name="flashvars" value="\' . $flashvars . \'" />\';
        $result .= \'</object>\';
        $result .= \'</object>\';

        return $result;
    }

    public function gethtmlplayer( $profile, $video ) {
        if($video->type == \'youtube\') {
            $url_string = parse_url($video->video, PHP_URL_QUERY);
            parse_str($url_string, $args);
            $result  = \'<iframe title="YouTube Video Player" width="\'.$this->width.\'" height="\'.$this->height.\'" \';
            $result .= \'src="http://www.youtube.com/embed/\'.$args[\'v\'].\'" frameborder="0" allowfullscreen></iframe>\';
        } else {
            $preview = $video->preview ? \'poster="\' . $video->preview . \'"\' : \'\';
            $result  = \'<video onclick="this.play();" width="\'.$this->width.\'" height="\'.$this->height.\'" \'.$preview.\' controls>\';
            $result .= \'<source src="\'.$video->video.\'" />\';
            $result .= \'</video>\';
        }

        return $result;
    }

    function curPageURL() {
        $pageURL = \'http\'; 

        if (!empty($_SERVER[\'HTTPS\'])) {if($_SERVER[\'HTTPS\'] == \'on\'){$pageURL .= "s";}} 
        $pageURL .= "://"; 
        if ($_SERVER["SERVER_PORT"] != "80") { 
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
        } else { 
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 
        } 

        return $pageURL;
    }

    function updatehits( $slug ) {
        global $wpdb;

        $table_name = $wpdb->prefix."allvideogallery_videos";
        $video = $wpdb->get_row("SELECT * FROM $table_name WHERE slug=\'$slug\'");        
        $count = ($video) ? $video->hits + 1 : 1;
        $video->hits = $count;

        $wpdb->update($table_name, array(\'hits\' => $video->hits), array(\'id\' => $video->id));
    }

}

?>
我尝试添加comments_template(); 就在?> 但什么都没有出现。当它不是主题文件时,是否有其他方法调用该函数?

1 个回复
SO网友:Rubins

来自wordpress codex:

 comments_template();
 //Loads the comment template. For use in single Post and Page displays. 
 //Will not work outside of single displays unless $withcomments is set to "1".
我打赌你需要这个->$带评论的东西

<?php
global $withcomments;
$withcomments = true;
comments_template();
?>

结束

相关推荐

如何在子目录WordPress安装中包含外部PHP文件?

我最初在SO上发布了这篇文章,但被指示这样做。我在目录/博客中安装了Wordpress。我正在尝试使用相对路径(如“../../include/file.PHP”)包含位于根父目录中的多个PHP文件。由于Wordpress使用其安装目录作为基础,我试图通过包括<base href=\"http://mydomain.com\" /> 在头部内部,但没有效果。在我的Wordpress主题中,我可以适当地调用这些PHP文件吗?