-
-
Save billerickson/ead97f48e5d1cb5611fa5a206ecc3b34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Schema Markup for Display Posts Shortcode | |
* @author Bill Erickson | |
* @see https://www.billerickson.net/code/schema-markup-display-posts-shortcode | |
* | |
* @param string $open_markup | |
* @param array $atts, shortcode attributes | |
* @param object $loop | |
* @return string | |
*/ | |
function ea_dps_schema( $open_markup, $atts, $loop ) { | |
if( !empty( $atts['disable_schema'] ) && true === filter_var( $atts['disable_schema'], FILTER_VALIDATE_BOOLEAN ) ) | |
return $open_markup; | |
$json = ea_json_summary_markup( $loop ); | |
return $json . $open_markup; | |
} | |
add_filter( 'display_posts_shortcode_wrapper_open', 'ea_dps_schema', 40, 3 ); | |
/** | |
* JSON Summary Markup | |
* @see https://developers.google.com/search/docs/guides/mark-up-listings#summary-page--multiple-full-details-pages | |
* | |
*/ | |
function ea_json_summary_markup( $loop = false ) { | |
global $wp_query; | |
$loop = empty( $loop ) ? $wp_query : $loop; | |
if( ! $loop->have_posts() ) | |
return; | |
$output = array(); | |
foreach( $loop->posts as $i => $post ) { | |
$position = $i + 1; | |
$output [] = '{ | |
"@type":"ListItem", | |
"position":' . $position . ', | |
"url":"' . get_permalink( $post->ID ) . '" | |
}'; | |
} | |
return '<script type="application/ld+json"> | |
{ | |
"@context":"http://schema.org", | |
"@type":"ItemList", | |
"itemListElement":[' . join( ',', $output ) . '] | |
} | |
</script>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment