来自 PHP 循环的架构标记

Schema markup from a PHP loop

我正在尝试从 WordPress PHP 循环自动生成架构标记。我正在尝试从我的“hovedret”WordPress post 类别中的五个最新 post 创建我的标记。

我正在尝试生成的架构标记:

<script type="application/ld+json">
    {
      "@context":"https://schema.org",
      "@type":"ItemList",
      "itemListElement":[
        {
          "@type":"ListItem",
          "position":1,
          "url":"http://example.com/peanut-butter-cookies.html"
        },
        {
          "@type":"ListItem",
          "position":2,
          "url":"http://example.com/triple-chocolate-chunk.html"
        },
        {
          "@type":"ListItem",
          "position":3,
          "url":"http://example.com/snickerdoodles.html"
        }
      ]
    }
    </script>

我的代码应该遍历“hovedret”类别中的最新五个 post,这样我就有位置 1、2、3、4 和 5。我按顺序使用 get_the_permalink填写 URL.

我正在使用 wp_head 挂钩,以便 return 我的代码进入我的网站。出于某种原因,代码破坏了我的网站。我不确定我是否以正确的方式构建了我的 while 循环,或者我的代码有其他问题?我试过 echo 而不是 return $string;

关于我遗漏了什么的任何线索??

//SCHEMA MARKUP
function tt_hook_schema_new()
{
  if ( is_category() ) 
  {
        $post_per_page = 5;
            $the_query = new WP_Query( array( 'category_name' => 'hovedret', 'posts_per_page' => $post_per_page ) ); 
            
      if ( $the_query->have_posts() ) 
      {
                    $string .= '<script type="application/ld+json">
                                        {
                                    "@context":"https://schema.org",
                                    "@type":"ItemList",
                      "itemListElement":[';
          $counter = 0;
          
          while ( $the_query->have_posts() )
          {
              $counter = $counter + 1;
              $string .= '{';
              $string .= '"@type":"ListItem",';
              $string .= '"position":' . $counter;
              $string .= '"url":"' . get_the_permalink() . '"';   
              $string .= '}';
              
              if($counter != $post_per_page)
              {
                $string .= ',';
              }
          }
          $string .= ']
                                }
                      </script>';
                      
        return $string;

/* Restore original Post Data */
wp_reset_postdata();
            }

  }
}
add_action('wp_head', 'tt_hook_schema_new');

存在一处语法错误:

$string .= '<script type="application/ld+json">应该变成
$string = '<script type="application/ld+json">
(删除点,因为之前未定义 $string)。

然后你 运行 陷入无限循环,因为你永远不会得到下一个 post。应该是:

while ( $the_query->have_posts() ) {
    $the_query->the_post();
    // ...
}

你还需要 echo 内容。

编辑:我修复了更多错误,这里是更正后的版本(查看更改的评论):

function tt_hook_schema_new() {
    if ( is_category() ) {
        $post_per_page = 5;
        $the_query     = new WP_Query(
            array(
                'category_name'  => 'hovedret',
                'posts_per_page' => $post_per_page,
            )
        );

        if ( $the_query->have_posts() ) {
            // Here was a `.` too much.
            $string  = '<script type="application/ld+json">
                        {
                            "@context":"https://schema.org",
                            "@type":"ItemList",
                            "itemListElement":[';
            $counter = 0;

            while ( $the_query->have_posts() ) {
                // This line was missing.
                $the_query->the_post();

                $counter++;
                $string .= '{';
                $string .= '"@type":"ListItem",';
                // There was a missing comma.
                $string .= '"position":' . $counter . ',';
                $string .= '"url":"' . get_the_permalink() . '"';
                $string .= '}';

                // Having less then `$post_per_page` would still have the last comma.
                if ( $counter < $the_query->found_posts ) {
                    $string .= ',';
                }
            }
            $string .= ']
                    }
                    </script>';

            // Echoing stuff without escaping can be insecure!
            echo $string;
        }
        // You should reset postdata outside the `if` statement.
        /* Restore original Post Data */
        wp_reset_postdata();
    }
}
add_action( 'wp_head', 'tt_hook_schema_new' );