/* ====================================================== МЕТАБОКС "ОТВЕТ ЭКСПЕРТА" + ДАТА ====================================================== */ add_action('add_meta_boxes', function () { add_meta_box( 'accepted_answer_box', 'Ответ эксперта', 'render_accepted_answer_box', 'question', 'normal', 'high' ); }); function render_accepted_answer_box($post) { $text = get_post_meta($post->ID, 'accepted_answer_text', true); $date = get_post_meta($post->ID, 'accepted_answer_date', true); echo '

Дата ответа:

'; echo ''; echo '

Текст ответа:

'; echo ''; } add_action('save_post_question', function ($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (!current_user_can('edit_post', $post_id)) return; if (isset($_POST['accepted_answer_text'])) { update_post_meta($post_id, 'accepted_answer_text', $_POST['accepted_answer_text']); } if (isset($_POST['accepted_answer_date']) && !empty($_POST['accepted_answer_date'])) { update_post_meta($post_id, 'accepted_answer_date', $_POST['accepted_answer_date']); } }); /* ====================================================== QAPage SCHEMA ====================================================== */ function add_qapage_schema_to_questions() { if (!is_singular('question')) return; global $post; $lawyers_page_ids = [24, 988]; $question_title = get_the_title($post->ID); $question_text = wp_strip_all_tags(apply_filters('the_content', $post->post_content)); $question_url = get_permalink($post->ID); $question_date_created = get_the_time('c', $post->ID); $question_author_id = $post->post_author; $question_author_name = get_the_author_meta('display_name', $question_author_id) ?: 'Гость'; $final_question_author_url = in_array($question_author_id, $lawyers_page_ids) ? 'https://zakon.kg/lawyers/' : 'https://zakon.kg/account/?user=' . $question_author_id; /* === РЕДАКЦИОННЫЙ ОТВЕТ === */ $editor_answer = get_post_meta($post->ID, 'accepted_answer_text', true); $editor_date = get_post_meta($post->ID, 'accepted_answer_date', true); $accepted_answer = null; if (!empty($editor_answer)) { $editor_date_iso = $editor_date ? date('c', strtotime($editor_date)) : $question_date_created; $accepted_answer = [ "@type" => "Answer", "text" => wp_strip_all_tags($editor_answer), "dateCreated" => $editor_date_iso, "author" => [ "@type" => "Organization", "name" => "Zakon.kg", "url" => "https://zakon.kg/lawyers/" ], "url" => $question_url . "#accepted-answer" ]; } /* === КОММЕНТАРИИ === */ $comments = get_comments([ 'post_id' => $post->ID, 'status' => 'approve', 'orderby' => 'comment_date_gmt', 'order' => 'ASC' ]); $suggested_answers = []; foreach ($comments as $comment) { $comment_author_id = $comment->user_id; $comment_author_name = $comment->comment_author ?: 'Гость'; $comment_permalink = get_comment_link($comment->comment_ID); $formatted_comment_date = get_comment_date('c', $comment->comment_ID); $final_comment_author_url = in_array($comment_author_id, $lawyers_page_ids) ? 'https://zakon.kg/lawyers/' : (!empty(get_the_author_meta('user_url', $comment_author_id)) ? get_the_author_meta('user_url', $comment_author_id) : 'https://zakon.kg/account/?user=' . $comment_author_id); $answer_data = [ "@type" => "Answer", "itemid" => $comment_permalink, "text" => wp_strip_all_tags($comment->comment_content), "dateCreated" => $formatted_comment_date, "author" => [ "@type" => "Person", "name" => $comment_author_name, "url" => $final_comment_author_url ], "url" => $comment_permalink ]; $suggested_answers[] = $answer_data; } /* === SCHEMA === */ $schema_markup = [ "@context" => "https://schema.org", "@type" => "QAPage", "mainEntity" => [ "@type" => "Question", "name" => $question_title, "text" => $question_text, "dateCreated" => $question_date_created, "url" => $question_url, "author" => [ "@type" => "Person", "name" => $question_author_name, "url" => $final_question_author_url ] ] ]; if ($accepted_answer) { $schema_markup['mainEntity']['acceptedAnswer'] = $accepted_answer; } if (!empty($suggested_answers)) { $schema_markup['mainEntity']['suggestedAnswer'] = $suggested_answers; } echo ''; } add_action('wp_head', 'add_qapage_schema_to_questions'); /* === Отключаем Yoast Schema на вопросах === */ add_filter('wpseo_json_ld_output', function($data){ if (is_singular('question')) return false; return $data; }, 10);