【问题标题】:Removing the last loop comma as needed for schema script根据需要删除架构脚本的最后一个循环逗号
【发布时间】:2020-03-28 21:49:58
【问题描述】:

我只想删除最后一个循环中的逗号,因为如果最后一部分仍有逗号,脚本将不起作用。

这是我正在使用的代码:

<?php
$faqs = get_field('questions_&_answers');
if( $faqs ) :
?>
<script type="application/ld+json">
{
    "@context":"https://schema.org",
    "@type":"FAQPage",
    "mainEntity":[
<?php
    if( have_rows('questions_&_answers') ):
        $comma = ',';
        $i = 1;
        while ( have_rows('questions_&_answers') ) : the_row();
?>  
    {"@type":"Question","name":"<?=get_sub_field('question', false, false)?>","acceptedAnswer":{"@type":"Answer","text":"<?=get_sub_field('answer', false, false)?>"}}<?=($i < $faqs) ? $comma : '';?>
<?php
        $i++;
        endwhile;
    endif;
?>  
    ]
}
<?php
endif;
?>
</script>

这就是结果

{
    "@context":"https://schema.org",
    "@type":"FAQPage",
    "mainEntity":[

    {"@type":"Question","name":"My Question 1","acceptedAnswer":{"@type":"Answer","text":"My Answer 1"}},   
    {"@type":"Question","name":"My Question 2","acceptedAnswer":{"@type":"Answer","text":"My Answer 2"}},   
    ]
}

看到循环的最后一部分“我的问题2”末尾还有逗号,需要去掉

【问题讨论】:

    标签: php wordpress while-loop schema advanced-custom-fields


    【解决方案1】:
     function str_last_replace($search, $replace, $subject) {
            $pos = strrpos($subject, $search);
    
            if($pos !== false)
            {
                $subject = substr_replace($subject, $replace, $pos, strlen($search));
            }
    
            return $subject;
         }
    

    【讨论】:

      【解决方案2】:

      首先你需要计算转发器中的项目数,你可以像下面这样:

      <?php
      $faqs = get_field('questions_&_answers');
      if( $faqs ) :
          $rowCount = count( $faqs ); //Store the number of items in variable.
      

      那么你在显示逗号的地方,你需要检查上面的变量。

      {"@type":"Question","name":"<?=get_sub_field('question', false, false)?>","acceptedAnswer":{"@type":"Answer","text":"<?=get_sub_field('answer', false, false)?>"}}
      <?=($i < $faqs) ? $comma : '';?>
      

      这应该如下所示。看看$faqs 是如何被$rowCount 替换的。

      {"@type":"Question","name":"<?=get_sub_field('question', false, false)?>","acceptedAnswer":{"@type":"Answer","text":"<?=get_sub_field('answer', false, false)?>"}}
      <?=($i < $rowCount) ? $comma : '';?> <!-- See the variable has been changed -->
      

      【讨论】:

        【解决方案3】:

        使用现有代码解决此问题的最简单方法可能是在每个输出字符串的开头输出$comma,但只需将$comma 变量设置为,你之后已输出第一个字符串:

        <?php
            if( have_rows('questions_&_answers') ):
                $i = 1;
                $comma = '';
                while ( have_rows('questions_&_answers') ) : 
                    the_row();
                    echo $comma;
        ?>  
            {"@type":"Question","name":"<?=get_sub_field('question', false, false)?>","acceptedAnswer":{"@type":"Answer","text":"<?=get_sub_field('answer', false, false)?>"}}
        <?php
                $comma = ',';
                $i++;
                endwhile;
            endif;
        ?>  
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-24
          • 1970-01-01
          • 2019-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-14
          相关资源
          最近更新 更多