첫 번째 단락 다음에 제목을 가져옵니다: XPath
P粉447002127
P粉447002127 2023-08-20 20:52:45
0
1
570
<p>내 웹사이트에 FAQPage 스키마를 추가하고 싶습니다. </p> <p>이 작업을 수행하려면 물음표가 있는 모든 <code><h2></code> 또는 <code><h3></code> 태그를 찾아야 합니다. 그것이 문제이다. </p> <p>이후에는 제목 뒤의 첫 번째 <code></code> 태그가 답변으로 필요합니다. </p> <p>최종 결과는 다음과 같습니다.</p> <pre class="brush:php;toolbar:false;">{ "@type": "질문", "name": "환불을 처리하는 데 얼마나 걸리나요?", "acceptedAnswer": { "@type": "답변", "text": "첫 번째 P 태그에서 얻은 콘텐츠", "url": "https://www.example.com/answer#anchor_link" } }</pre>
  • 문제의 "name"

    또는

    태그입니다. </li>
  • 답변의 "url"

    또는

    에서 얻습니다. ; 영구 링크 및 앵커 링크에 태그를 지정합니다. </li> <li><strong>이 두 매개변수는 해결되었습니다</strong></li> </ul> <p>안타깝게도 제목 태그 다음의 첫 번째 단락 태그를 가져오는 방법을 알 수 없습니다.</p> <p>다음 줄의 첫 번째 단락 내용을 가져와야 합니다. </p> <pre class="brush:php;toolbar:false;">"text": "첫 번째 P 태그에서 얻은 콘텐츠",</pre> <p>현재 코드는 다음과 같습니다.</p> <pre class="brush:php;toolbar:false;"><?php $content_postid = get_the_ID(); $content_post = get_post($content_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); libxml_use_internal_errors(true); $dom = 새 DOMDocument; $dom->loadHTML('<?xml 인코딩="utf-8" ?>' . $content); $xp = 새로운 DOMXPath($dom); $query = "//h2[contains(., '?')] | //h3[contains(., '?')]"; $nodes = $xp->query($query); $스택 = []; if ($노드) { $faq_count = 개수($노드); $faq_i = 1; 에코 ' <스크립트 유형="application/ld+json"> { "@context": "https://schema.org", "@type": "FAQ페이지", "메인엔티티": ['; foreach($nodes를 $node로) { 에코 '{ "@type": "질문", "name": "'.$node->nodeValue.'", "acceptedAnswer": { "@type": "답변", "text": "첫 번째 P 태그에서 얻은 콘텐츠", "url": "'.get_permalink().'#'.$node->getAttribute('id').'" } }'; if ($faq_i != $faq_count) : echo ',' endif; } echo ']}</script>'; } ?></pre> <p>보시다시피 저는 이 코드 줄을 사용하여 <code></code> <h3></code>태그:</p> <pre class="brush:php;toolbar:false;">$query = "//h2[contains(., '?')] | //h3[contains(., '?')]"; </pre> <p>제목 뒤의 단락을 찾으려면 두 번째 <code>$query</code>가 필요한 것 같아요. 그런데 제목 뒤의 첫 번째 태그를 어떻게 확인하나요?</p> <p>다음 추가 쿼리를 시도했습니다. </p> <pre class="brush:php;toolbar:false;">$query2 = "//h2[contains(., '?')]/following-sibling::p[1] | //h3[contains (., '?')]/following-sibling::p[1]";</pre> <p>그러나 <code>following-sibling::</code>나 <code>following::</code>도 작동하지 않습니다. 항상 마지막 제목 뒤에 단락을 표시합니다. </p> <p>첫 번째 쿼리를 해결해야 합니까? 내가 어느 수준인지 알아? </p> <p>다음은 <code>$content_post</code>의 예입니다(항상 다름). </p> <pre class="brush:php;toolbar:false;"><h2>Lorem ipsum dolor sit amet?</h2> consectetuer adipiscing elit, sed diam nonummy nibh euismodtincidunt ut laoreet dolore magna aliquam erat volutpat. <p>Veniam, quis nostrud exerciation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p> <h3>Duis autem veleum?</h3> <p>Iris dolor in vulputate velit Esse Molestie consequent, velillum dolore eu feugiat nulla facilisis at vero et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p> <h2>Nam liber tempor cum soluta?</h2> <h3>nobis eleifend option congue nihil</h3> <p>imperdiet doming id quod mazim placeratfacer possim assum. <p>Et wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p> <h3>Duis autem vel?</h3> <p>Eum iriure dolor in hendrerit in vulputate velit esse Molestie consequat, velillum dolore eu feugiat nulla facilisis at vero et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p> <h4>Nam liber tempor cum soluta nobis</h4> <p>eleifend option congue nihil imperdiet doming id quod mazim placeratfacer possim assum.</p></pre> <p><br /></p>

P粉447002127
P粉447002127

모든 응답(1)
P粉957661544

foreach를 다음과 같이 변경하고 작동하는지 확인해 보세요.

으아악
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!