lele_ballack Posted July 2, 2020 Share Posted July 2, 2020 Hi everyone, I'm trying to create an article index using all the heading titles of an article, but haven't succeeded so far. I tried with: foreach ($page->text->h2 as $heading) But I get this: "Notice: Trying to get property 'h2' of non-object" What's the best way to get all the h2 tags inside the textarea? I'm using CKEditor to write the articles. Thank you! Link to comment Share on other sites More sharing options...
kongondo Posted July 2, 2020 Share Posted July 2, 2020 (edited) 1 hour ago, lele_ballack said: foreach ($page->text->h2 as $heading) Your text is a string, not an object That's what the error is about. 1 hour ago, lele_ballack said: What's the best way to get all the h2 tags inside the textarea? You can use PHP's DOMDocument Class and the method getElementsByTagName(). Here's a tutorial (with a h2 example :-)): https://codingreflections.com/php-parse-html/ I am not sure about performance issues with the class if it has to parse lots of text/HTML. Edited July 2, 2020 by kongondo 2 Link to comment Share on other sites More sharing options...
lele_ballack Posted July 2, 2020 Author Share Posted July 2, 2020 Thank you so much @kongondo! The tutorial link was pretty clear and this solution works like a charm! Link to comment Share on other sites More sharing options...
kongondo Posted July 3, 2020 Share Posted July 3, 2020 16 hours ago, lele_ballack said: this solution works like a charm! Excellent! Glad it worked. By the way, if your articles don't change often, you could cache the results of DOMDocument operations so that subsequent article index are fetched from the cache instead. Whenever an article changed, you would call DOMDocument again and then cache the new output. Have a look at WireCache here if this might be of interest: https://processwire.com/api/ref/wire-cache/ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now