实际定义的未定义函数的问题

问题描述 投票:-1回答:1

我没有构建这个PHP文件,但我想弄清楚它为什么会给我错误。如果“块布局模式”ACF变量设置为“模态”,则块可以工作,但是给出了这个错误。 “第176行的未定义变量$ vids”。如果我选择“嵌入和缩略图”模式,则不显示任何块,我收到此错误。 “在第252行调用未定义的函数vc_embed_layout()”。我无法确定为什么会发生这种情况,我无法在我的php文件中找到任何未关闭的标签或任何内容。这是有问题的文件,我已经标记了触发错误的行。

<?php
global $theme_text_domain;

$block_class_identifier = 'blk__vidcol';

// default variables
$block_id = $top_class = $top_style = $inner_class = $inner_style = '';

// check for custom id
$block_id .= BLOCK::set_id( get_sub_field('content_block_id') );

// set block class, $i comes from FLEX class loop and represents the block order on a page
$top_class .= BLOCK::set_standard_classes( $i );
$top_class .= BLOCK::set_custom_classes( get_sub_field('content_block_classes') );

// check for top padding adjustment
$top_class .= BLOCK::set_padding_class( get_sub_field('block_padding_adjustment') );
$top_style .= BLOCK::set_padding_style( get_sub_field('block_padding_adjustment'), get_sub_field('custom_top_padding'), get_sub_field('custom_bottom_padding'));

// check for custom color theme
$top_style .= BLOCK::set_colors( get_sub_field('color_theme'), get_sub_field('text_color'), get_sub_field('custom_background_color') );

// check for width settings
$inner_class .= BLOCK::set_width_class( get_sub_field('content_width') );
$inner_style .= BLOCK::set_width_style( get_sub_field('content_width'), get_sub_field('custom_maximum_content_width') );

$block_assets = get_stylesheet_directory_uri() . '/inc/blocks/' . basename(__FILE__, '.php') . '/assets/';


/*
CUSTOM BLOCK SPECIFIC SETUP
 */

$layout = get_sub_field('block_layout_mode');
$top_class .= ' layout-' . $layout;


// title color
$h2_style = (get_sub_field('color_theme') == 'custom') ? ' color: ' . get_sub_field('custom_title_color') . ';' : '';





/**
 * outputs code for the modal version of the video collection block
 * @return html output
 */
if (!function_exists('vc_modal_layout')) {
function vc_modal_layout() {
    global $block_class_identifier;
    $cta_label = get_sub_field('cta_label');
    $counter = 0;


    $cta = array(
        'style' => get_sub_field('cta_style'),
        'button_color' => get_sub_field('button_color'),
        'button_text_color' => get_sub_field('button_color')
    );
    $cta_class = BLOCK::get_cta_style( $cta, '.' . $block_class_identifier . ' .button');
?>

    <?php // list section ?>
    <?php if( have_rows('videos') ): ?>
    <div class="vid-container">
        <?php while ( have_rows('videos') ) : the_row(); ?>

            <?php if ($counter == 0): ?>

            <div class="vid-featured">
                <div class="video-wrapper">
                    <iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
                    <a class="vid_overlay" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_0" href="javascript:;"></a>
                </div>

                <!-- modal -->
                <div id="<?php echo $block_class_identifier; ?>_vid_0" class="modal video-modal" style="display: none;">
                    <div class="modal-inner">
                        <h3><?php echo get_sub_field('video_title'); ?></h3>
                        <div class="video-wrapper">
                            <iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
                        </div>
                    </div>
                </div>
            </div>

            <div class="vid-list">

            <?php else: ?>

                    <div class="vid-item">
                        <div class="item-btn">
                            <a class="button <?php echo $cta_class; ?>" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" href="javascript:;"><?php echo $cta_label; ?></a>
                        </div>
                        <div class="item-title"><?php echo get_sub_field('video_title'); ?></div>
                    </div>

                    <!-- modal -->
                    <div id="<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" class="modal video-modal" style="display: none;">
                        <div class="modal-inner">
                            <h3><?php echo get_sub_field('video_title'); ?></h3>
                            <div class="video-wrapper">
                                <iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
                            </div>
                        </div>
                    </div>

            <?php endif; ?>

            <?php $counter++; ?>
        <?php endwhile; ?>

        </div>
    </div>
    <?php else: ?>
        No videos defined.
    <?php endif; ?>
<?
}
}





/**
 * outputs code for the embed version of the video collection block
 * @return html output
 */
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout($vids, $i) {
?>
    <?php if ( $vids ): ?> **this is line 176**

    <div class="vid-container2">

        <h2 class="vid-title"><?php echo $vids[0]['video_title']; ?></h2>

        <div class="vid-container2-inner">

            <div class="vid-embed">
                <div class="video-wrapper">
                    <iframe src="https://www.youtube.com/embed/<?php echo $vids[0]['youtube_embed_code']; ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen onload="this.style.visibility = 'visible';"></iframe>
                </div>
            </div>


            <div class="vid-thumbnails">
                <?php foreach ($vids as $key => $vid): ?>
                    <?php $wrap_class = ($key == 0) ? ' tn-hide' : ''; ?>

                    <div class="tn-wrapper <?php echo $wrap_class; ?>" data-embed="<?php echo $vid['youtube_embed_code']; ?>" data-title="<?php echo $vid['video_title']; ?>">
                        <img src="https://img.youtube.com/vi/<?php echo $vid['youtube_embed_code']; ?>/mqdefault.jpg" alt="">
                    </div>

                <?php endforeach; ?>
            </div>
        </div>

        <script>
            var vid_swap = function() {
                $('.block_<?php echo $i; ?> .vid-embed').css({opacity: 1});

                $('.block_<?php echo $i; ?> .tn-wrapper').on('click', function(e) {
                    var $this = $(this);
                    var embed_id = $this.attr('data-embed');
                    var title = $this.attr('data-title');
                    console.log('embed = ' + embed_id);

                    $('.block_<?php echo $i; ?> .vid-title').animate({opacity: 0}, 100);
                    $('.block_<?php echo $i; ?> .vid-embed').animate({opacity: 0}, 1000, function() {
                        $('.block_<?php echo $i; ?> .vid-embed, .block_<?php echo $i; ?> .vid-title').css('visiblity','hidden');
                        $('.block_<?php echo $i; ?> .vid-title').html( title ); 
                        $('.block_<?php echo $i; ?> .vid-embed iframe').attr('src', 'https://www.youtube.com/embed/' + embed_id);
                        $('.block_<?php echo $i; ?> .tn-wrapper.tn-hide').removeClass('tn-hide');
                        $this.addClass('tn-hide');
                        $('.block_<?php echo $i; ?> .vid-title').css('visiblity','visible').delay(500).animate({opacity: 1}, 500);
                        $('.block_<?php echo $i; ?> .vid-embed').delay(500).animate({opacity: 1}, 1000);
                    });
                });
            }

            jQuery(document).on('block_init', vid_swap);
        </script>
    </div>

    <?php else: ?>
        No videos defined.
    <?php endif; ?>

<?php
}
}

?>



<section id="<?php echo $block_id; ?>" class="<?php echo $block_class_identifier; ?> <?php echo $top_class; ?>" style="<?php echo $top_style; ?>">
    <div class="inner <?php echo $inner_class; ?>" style="<?php echo $inner_style; ?>">

        <?php // title section ?>
        <?php if (get_sub_field('title')): ?>
        <h2 style="<?php echo $h2_style; ?>"><?php echo get_sub_field('title'); ?></h2>
        <?php endif; ?>

        <?php $vids = get_sub_field('videos'); ?>
        <?php if ($layout == 'modal') vc_modal_layout($vids); ?>
        <?php if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**


    </div>
</section>
php wordpress advanced-custom-fields
1个回答
1
投票

你不接受vc_modal_layout()函数声明中的任何参数。

if (!function_exists('vc_embed_layout')) {
  function vc_embed_layout() {

需要有参数才能接受参数。

你正在寻找的是这样的东西:

if (!function_exists('vc_embed_layout')) {
  function vc_embed_layout($vids=[], $i=0) {

vc_modal_layout的想法相同,函数需要能够从外部接受变量。

当我们调用一个函数时,该函数需要能够接受传递给它的所有参数。我们使用的所有变量都需要定义,如果存在未定义或空数组的风险,我们可以使用if(!empty($var))if(isset($var))检查它们

将变量传递给函数的另一种方法是将其作为全局访问。但是将变量传递给函数通常会更好,因为当我们将变量传递给PHP中的函数时,它会使用函数内部变量值的副本。当我们做这样的事情时:

$var = 10;
function doIt(){
  global $var;
  $var++;
}
doit();
echo $var;

我们可以过度复杂化......

让我们仔细看看:

if ($layout == 'modal') vc_modal_layout($vids);

if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**


function vc_modal_layout(){ ... }
// should be:
function vc_modal_layout($vids){ ... }

function vc_embed_layout($vids, $i) 
// looks good.

除此之外,没有什么是非常不寻常的,我想看看实际网站进一步修补这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.