For parsing the strings inside my Wordpress posts to HTML. I use Simple DOM Parser. With the below code I receive Fatal error: Call to a member function find() on a non-object
. Could somebody be of helping hand?
Thanks in advance
<?php
require ('simple_html_dom.php');
/*
Template Name: Practice
*/
$query = new WP_Query( 'category_name=arthouse' );
if ($query->have_posts()) {
while ( $query->have_posts() ) {
$query->the_post();
$page = the_content().' ';
$html = str_get_html($page). " ";
foreach($html->find('div[class=post]') as $element) {
echo $element->src . '<br>';
}
}
}else{
echo 'Sorry, no posts matched your criteria.';
}
?>
First of all, I didn't understand, why you are using DOM parser
here and if you are willing to get all the links from inside the div[class=post]
using simple dom parser
then you should write something like this
foreach($html->find('div.post a') as $a){ // or div[class=post] a
echo $a->href.'<br />'; // for link, i.e. http://somedomain.com/somepage.php
// or
echo $a; // to print out the link as it's
}
This may not right for your layout of html but it may give you an idea, also check this answer. If you try this code (fetch all the post title/link from my site), you may get an idea (scrapping my site is not allowed)
$html = file_get_html('http://heera.it');
foreach($html->find('section.post-body h3 a') as $a){
echo $a.'<br />';
}
This (section.post-body h3 a
) to get all the links from inside h3
tags which is inside section
tag with class post-body
. Again, make sure, why you are using a Dom parser
? I think, you don't need to use a parser, maybe you are doing it wrong, I think so.