Get the title after the first paragraph: XPath
P粉447002127
P粉447002127 2023-08-20 20:52:45
0
1
529

I want to add a FAQPage schema to my website.

In order to do this, I need to find every

or

tag with a question mark. That's the problem.

After that, I need the first

tag after the title as the answer.

The final result should look like this:

{ "@type": "Question", "name": "How long does it take to process a refund?", "acceptedAnswer": { "@type": "Answer", "text": "Content obtained from the first P tag", "url": "https://www.example.com/answer#anchor_link" } }
  • The "name" in question is the

    or

    tag.

  • The "url" of the answer is obtained from the

    or

    tag Permalinks and anchor links.

  • These two parameters have been resolved

Unfortunately, I can't find out how to get the first paragraph tag after the title tag.

I need to get the content of the first paragraph in the following line:

"text": "Content obtained from the first P tag",

This is my current code:

post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); libxml_use_internal_errors(true); $dom = new DOMDocument; $dom->loadHTML('' . $content); $xp = new DOMXPath($dom); $query = "//h2[contains(., '?')] | //h3[contains(., '?')]"; $nodes = $xp->query($query); $stack = []; if ($nodes) { $faq_count = count($nodes); $faq_i = 1; echo ' '; } ?>

As you can see, I use this line of code to find every

or with ? ;

Tag:

$query = "//h2[contains(., '?')] | //h3[contains(., '?')]"; 

I guess I need a second $query to find the paragraph after the title? But how to check the first tag after the title?

I tried this additional query:

$query2 = "//h2[contains(., '?')]/following-sibling::p[1] | //h3[contains (., '?')]/following-sibling::p[1]";

But neither following-sibling:: nor following:: works. It always displays the paragraph after the last heading.

Do I need to resolve the first query? to know what level am I at?

Here is an example of $content_post (it is always different):

Lorem ipsum dolor sit amet?

consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim

Veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel eum?

Iris dolor in hendrerit in vulputate velit Esse molestie consequential, vel illum 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.

Nam liber tempor cum soluta?

nobis eleifend option congue nihil

imperdiet doming id quod mazim placerat facer possim assum.

Et wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel?

Eum iriure dolor in hendrerit in vulputate velit essse molestie consequat, vel illum 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.

Nam liber tempor cum soluta nobis

eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.


P粉447002127
P粉447002127

reply all (1)
P粉957661544

Try changing yourforeachto the following and see if it works.

foreach($nodes as $node) { $ans = $xp->query("./following-sibling::p[1]",$node)[0]->nodeValue; echo "{ '@type': 'Question', 'name': '".$node->nodeValue."', 'acceptedAnswer': { '@type': 'Answer', 'text': {$ans} } }";
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!