如何获得分类法的最后 5 个帖子标题?

How to get last 5 posts title of taxonomy?

我有一个优惠券网站,我想在我的优惠券商店页面上添加架构。假设我有一家亚马逊商店,并在亚马逊上添加了 5 张优惠券 post。现在,我想将这 5 个 post 标题和 post 链接添加到架构中。你能告诉我该怎么做吗?

下面是我的代码:

<script type="application/ld+json">{
    "@context": "http://schema.org",
    "@graph": [
        {
           "@type": "WebPage",
            "url": "<?php echo esc_html($taglink);?>",
            "image": {
                "@type": "ImageObject",
                "url": "<?php echo esc_html($brandimage);?>",
                "height": 100,
                "width": 170
            },
            "publisher": {
                "@type": "Organization",
                "name": "Coupon.com",
                "logo": {
                    "@type": "ImageObject",
                    "url": "https://cdn.coupontac.com/ovowhakr/2020/10/Logo.png.webp",
                    "width": 500,
                    "height": 200
                }
            },
            "dateModified": "",
            "description":"<?php echo esc_html($schemdesc2);?>",
            "name": "",
            "headline":"<?php echo esc_html($heading);?>",
            "mainEntity": {
                "@context": "http://schema.org",
                "@type": "Store",
                "name": "<?php echo esc_html($tagname);?>",
                "image": "<?php echo esc_html($brandimage);?>",
                "sameAs": "<?php echo esc_html($weburl);?>",
                
                "aggregateRating": {
                        "@type": "AggregateRating",
                        "ratingValue": <?php echo esc_html($userrating);?>,
                        "ratingCount": <?php echo esc_html($ratingcount);?>,
                        "bestRating": 5,
                        "worstRating": 0
                },
                "description": "<?php echo esc_html($schemdesc);?>",
                "makesOffer": [
                {
                        "@type": "Offer",
                        "name": "",
                        "url": "",
                        "description": "",
                        "validFrom": "",
                        "validThrough": ""  
        },
                ]
            }
            
        }
        
    ]
}</script>

任何人都可以帮助我在下面的部分获得 5 post 个标题吗?

 "makesOffer": [
                {
                        "@type": "Offer",
                        "name": "",
                        "url": "",
                        "description": "",
                        "validFrom": "",
                        "validThrough": ""  
        },
                ] 

提前谢谢你。

您可以简单地从 <head> 中查询所需的 post,但您可以使用模式模板而不是 html 模板。

正如您在示例中已经展示的那样,PHP 可以进入 javascript,因为它是基于服务器的,它首先呈现。

您不能多次使用 "makesOffer" 或任何唯一键。您必须重复报价。

您可以根据 Google own data testing tool.

在线测试您的结构化数据

以下内容基于makesOffer schema.org page example.

<script type="application/ld+json">
[
<?php
$args = array(
'post_type' => 'post',
//...
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while( $query->have_posts() ) :
$query->the_post(); ?>
{
  "@context": "https://schema.org",
  "@type": "Person",
  "name" : "Brent",
  "makesOffer" : {
        "@type" :"Offer",
        "priceSpecification" : {
            "@type" : "UnitPriceSpecification",
            "priceCurrency" : "USD",
            "price" : "18000" },
        "itemOffered" : {
            "@type" : "Car",
            "name" : "<?= wp_strip_all_tags( get_the_title(), true ); ?>",
            "description" : "<?= wp_strip_all_tags( get_the_excerpt(), true ); ?>",
        "image" : "2009_Volkswagen_Golf_V_GTI_MY09.png",
        "color" : "Black",
        "numberOfForwardGears" : "6",
        "vehicleEngine" : {
            "@type": "EngineSpecification",
            "name" : "4 cylinder Petrol Turbo Intercooled 2.0 L (1984 cc)"
            }
        }
    }
},
<?php endwhile;
endif;
wp_reset_postdata(); ?>
]
</script>