无法在放置在functions.php中的自定义函数中正确写入自定义字段数据

时间:2018-09-25 09:27:05

标签: php html wordpress

我需要在functions.php中放置的自定义函数中写入自定义帖子类型的过滤器数据:

大多数人都已得到纠正,但我无法处理以下几行:

<div class="cpt-studies-block-image picture">
  <?php $thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien'); ?>
  <img class="img" src="<?php print($thumb_img[0]); ?>">
</div>

和:

 <a href="<?php the_permalink()?>" class="cpt-studies-block-link link-read-more"> <?php
  include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
 ?>
 <?php if (get_field('button_text')):
 the_field('button_text');
 else : echo __('More', 'dw');?>
 <?php endif;?>
 </a>

文件夹中用于写入自定义帖子类型数据的代码:

if($query->have_posts()):
        while($query->have_posts()) : $query->the_post();?>

        <div class="col-md-6">
            <div class="cpt-studies-block">
                <div class="row">
                    <div class="col-md-6">
                        <a class="zoom-picture-hover" href="<?php the_permalink()?>">
                            <div class="cpt-studies-block-image picture">
                                <?php $thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien'); ?>
                                <img class="img" src="<?php print($thumb_img[0]); ?>">
                            </div>
                        </a>
                    </div>
                    <div class="col-md-6">
                            <span><?php the_field('date')?></span>
                            <h3>
                                <a href="<?php the_permalink()?>">
                                    <?php the_title()?>
                                </a>
                            </h3>
                            <p class="cpt-studies-block-text"><?php the_field('text')?></p>
                            <a href="<?php the_permalink()?>" class="cpt-studies-block-link link-read-more"> <?php
                              include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
                                ?>
                                <?php if (get_field('button_text')):
                                    the_field('button_text');
                                else : echo __('More', 'dw');?>
                                <?php endif;?>
                            </a>
                    </div>
                </div>
            </div>
        </div>

        <?php endwhile;
        endif;
        ?>

functions.php中“自定义”功能中的代码:

if(have_posts($wp_query))
   {
       while(have_posts($wp_query))
       {
           the_post();

           echo '<div class="col-md-6">'
            .'<div class="cpt-studies-block">'.
                '<div class="row">'.
                    '<div class="col-md-6">'
                        .'<a class="zoom-picture-hover" href="'.get_permalink().'">'
                            .'<div class="cpt-studies-block-image picture">'
            .$thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien').
          '<img class="img" src=".print($thumb_img[0]).">'
                            .'</div>'
                        .'</a>'
                    .'</div>'
                    .'<div class="col-md-6">'
                            .'<span>'.get_field('date').'</span>'
                            .'<h3>'
                                .'<a href="'.get_permalink().'">'
                                    .get_the_title().
                                '</a>'
                            .'</h3>'
                            .'<p class="cpt-studies-block-text">'.get_field('text').'</p>'.
                              '<a href="'.get_permalink().'" class="cpt-studies-block-link link-read-more">'
                                   include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
                            '</a>'.

                    '</div>'.
                '</div>'.
            '</div>'.
        '</div>';
       }
   }

请有人给我建议,还是我需要更改编写这些行的方法?

1 个答案:

答案 0 :(得分:1)

您遇到语法错误:

.'<div class="cpt-studies-block-image picture">'  // Add semicolon.
.$thumb_img = wp_get_attachment_image_src(
    get_field('image'),'custom-crop-studien').  // Assignement can't be concatenated
    // and missing parenthesis
'<img class="img" src=".print($thumb_img[0]).">'  // Resume echo.

所以:

// Ommited
.'<div class="cpt-studies-block-image picture">';
$thumb_img = wp_get_attachment_image_src(
   get_field('image'), 'custom-crop-studien'));  
echo '<img class="img" src=".print($thumb_img[0]).">'

或者更好的是,使用模板(如我在this other question of yours中所述)