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

问题描述

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

下面是我的代码

<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","url": "","description": "","validFrom": "","validThrough": ""  
        },]
            }
            
        }
        
    ]
}</script>

谁能帮我在下面的部分获得 5 个帖子标题

 "makesOffer": [
                {
                        "@type": "Offer",] 

提前致谢。

解决方法

您只需从 <head> 中查询所需的帖子,但不使用 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(),"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>