WordPress和高级自定义字段问题

时间:2018-07-28 16:15:01

标签: php wordpress advanced-custom-fields

我使用AFC设置了Wordpress,并且创建了两个转发器字段,这些字段已包含在页面中。在标题中,我有一个repeater字段来显示旋转木马滑块。然后在标题下方,我将页面内容包含在基本div中。我已将masthead.php添加到get_header()下方的front-page.php中。

由于某种原因,我仅在加载页面时才获得标题。标头标签下面的所有内容都会消失。但是,当我注释掉include函数以将front-page.php上的masthead.php插入时,我的主要内容显示出来,但是当然没有标题。似乎存在某种冲突。

我检查了我的php_error.log,它给了我:

  

PHP解析错误:语法错误,出现意外的“ <”   [folder_structure] /masthead.php,第64行

我已经检查并再次检查了我的AFC字段,以确保它不是拼写错误,或者我没有遍历正确的内容。但这对我来说很好。

下面的MASTHEAD.PHP

 <header>
        <?php 
        $defaults = array (
            'theme_location' => 'main-navigation',
            'menu_class' => 'no-bullet',
            'container' => false
        ); ?>

            <nav class="main-header-nav">
                <img src="<?php echo get_template_directory_uri();?>/icons/Hilton-logo.svg" alt="Hilton Logo" class="header-logo">
                <?php  wp_nav_menu($defaults); ?>
            </nav>



    <?php if( have_rows('header_carousel') ): ?>



        <?php while( have_rows('header_carousel') ): the_row(); 

            // vars
            $mastHeadImages = get_sub_field('mast_head_images');


         ?>        

            <h3 class="header-text tagline"></h3>
            <h1 class="header-text"></h1>




    <?php 

    if( $mastHeadImages ): ?>
        <div class="cycle-slideshow carousel-container"  data-cycle-fx="scrollHorz" data-cycle-timeout="0" data-cycle-prev="#prev" data-cycle-next="#next" data-cycle-caption=".carousel-counter" data-cycle-caption-template="<span class='bigger'>{{slideNum}}/ </span> <span class='smaller'>{{slideCount}}</span>">
           <div class="cycle-caption"></div>
            <?php $i = 0; ?>
            <?php foreach( $mastHeadImages as $image ): $imageURL = $image['url']?>
         <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" class="carousel-container__img carousel-img-<?php echo $i; ?>" />


            <?php $i++; endforeach; ?>
            <div class="masthead_overlay"></div>
        </div>
        <div class="center carousel-control-container">
        <a href="#" id="prev">
            <span class="icon-arrow-left"></span>
        </a> 
        <a href="#" id="next">
            <span class="icon-arrow-right"></span>
        </a>
        <div class="carousel-counter">

        </div>
    </div>


    <?php endif; endwhile; endif; ?>  

    </header>

下面的FRONT-PAGE.PHP

 
 <?php include('masthead.php'); ?>

 <div id="main" role="main">


   <?php if( have_rows('flexible_content') ): ?>



    <?php while( have_rows('flexible_content') ): the_row(); 

        // vars
        $mainContentImage = get_sub_field('main_content_image');
        $asideContentImage = get_sub_field('aside_content_image');
        $mainContentText = get_sub_field('main_content_text');
        $asideContentText = get_sub_field('aside_content_text');
        $CTALink = get_sub_field('cta_link');
        $CTAText = get_sub_field('cta_text');



     ?>

    <main id="main-content-container">

<!-- main content start-->

        <div class="main-content" style="background-image: url(<?php echo $mainContentImage['url']; ?>)">


            <div class="main-content-text">
                <h3>
                    <?php echo $mainContentText; ?>
                </h3>
                <p>
                    Sub Text
                </p>
                <?php if($CTALink) :?>
                <a href="<?php echo $CTALink; ?>" class="main-cta-btn">
                    <?php echo $CTAText; ?>
                </a>
                <?php endif;?>
            </div>

        </div>



<!-- main content end -->

<!--Aside content start-->

      <div class="aside-content" style="background-image: url(<?php echo $asideContentImage['url']; ?>)">

        <h2 style="color: #fff;"><?php echo $asideContentText; ?></h2> 

      </div>

<!--Aside content end-->


     </main>

    <?php endwhile; ?>

<?php endif; ?>

</div>



<?php get_footer();?>

如果我没有提供足够的信息,请告诉我。这只是一个本地项目。

1 个答案:

答案 0 :(得分:0)

因此,下面的代码本质上仅是对您的代码的轻微修改。我注意到您正在将get_sub_field('mast_head_images');的值传递给foreach,如果您想识别键然后使用图像对象的值,或者如果您使用Gallery作为上述重复的字段类型,则可以字段(我怀疑是这种情况)。

在这种情况下,此代码应该有效。

<header>
    <?php 
    $defaults = array (
        'theme_location' => 'main-navigation',
        'menu_class' => 'no-bullet',
        'container' => false
    ); ?>
    <nav class="main-header-nav">
        <img src="<?php echo get_template_directory_uri();?>/icons/Hilton-logo.svg" alt="Hilton Logo" class="header-logo">
        <?php  wp_nav_menu($defaults); ?>
    </nav>
    <?php if( have_rows('header_carousel') ): ?>
        <?php $i = 0; ?>
        <?php while( have_rows('header_carousel') ): the_row(); 
            // vars
            $mastHeadImages = get_sub_field('mast_head_images');
            ?>        
            <h3 class="header-text tagline"></h3>
            <h1 class="header-text"></h1>
            <div class="cycle-slideshow carousel-container"  data-cycle-fx="scrollHorz" data-cycle-timeout="0" data-cycle-prev="#prev" data-cycle-next="#next" data-cycle-caption=".carousel-counter" data-cycle-caption-template="<span class='bigger'>/ </span> <span class='smaller'></span>">
                <div class="cycle-caption"></div>
                <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" class="carousel-container__img carousel-img-<?php echo $i; ?>" />
                <?php $i++;?>
                <div class="masthead_overlay"></div>
            </div>
            <div class="center carousel-control-container">
                <a href="#" id="prev">
                    <span class="icon-arrow-left"></span>
                </a> 
                <a href="#" id="next">
                    <span class="icon-arrow-right"></span>
                </a>
                <div class="carousel-counter">
                </div>
            </div>
        <?php endwhile; 
    endif; ?>  
</header>

但是,如果您使用Gallery作为字段类型,那么我的假设是错误的,并且您的代码没有我可以看到的错误。

相关问题