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; } if ( ! has_category( 'Kurzgeschichten', $post_id ) ) { return; } $html = self::build_markup_for_post( $post ); if ( $html === '' ) { return; } echo "\n" . $html . "\n"; } private static function make_description( string $text ): string { $target_length = 150; $parts = explode(' ', $text); $lengths = array_map('strlen', $parts); $total_length = 0; $i = 0; for ( ; $i < count($lengths); $i++ ) { if ( $total_length + $lengths[$i] > $target_length ) break; $total_length += $lengths[$i]; } $desc = implode(' ', array_slice($parts, 0, $i)); $ellipsis = substr($desc, -1, 1) == '.' ? '' : ' ...'; // full sentences with a '.' don't need ellipsis '...' return $desc . $ellipsis; } /** * Build the head markup for one post. */ 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 = get_the_excerpt( $post ); $site_title = get_bloginfo( 'name' ); $site_url = site_url(); $site_url_p = parse_url($site_url); $site_host = $site_url_p['host']; $description = self::make_description(wp_strip_all_tags( $excerpt )); // 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(); ?>