for the currently displayed singular object.
*/
public static function render_head_markup(): void {
// Adjust this if you only want standard posts:
// if ( ! is_singular( 'post' ) ) { return; }
if ( ! is_singular() ) {
return;
}
$post_id = get_queried_object_id();
if ( ! $post_id ) {
return;
}
$post = get_post( $post_id );
if ( ! $post instanceof WP_Post ) {
return;
}
$html = self::build_markup_for_post( $post );
if ( $html === '' ) {
return;
}
echo "\n" . $html . "\n";
}
/**
* Build the head markup for one post.
*
* This is the function you will customize.
*/
private static function build_markup_for_post( WP_Post $post ): string {
// Example 1: conditionally emit markup only for a specific post type.
if ( $post->post_type !== 'post' ) {
return '';
}
$post_url = get_permalink( $post );
$post_title = get_the_title( $post );
$excerpt = has_excerpt( $post ) ? get_the_excerpt( $post ) : '';
// Example business logic:
// - add a meta tag for every post
// - add JSON-LD only if the post is in category "news"
$is_news = has_category( 'news', $post );
ob_start();
?>