I want to parse xml for getting element with same attribute and display it into my wordpress post using shortcode by php.
I create a xml file called emp_test.xml
<?xml version="1.0" encoding="utf-8"?>
<all_emp>
<emp_detail>
<emp emp_name="john"><img>john_1.jpg</img></emp>
<emp emp_name="john"><img>john_2.jpg</img></emp>
<emp emp_name="john"><img>john_3.jpg</img></emp>
<emp emp_name="marry"><img>marry_1.jpg</img></emp>
<emp emp_name="marry"><img>marry_2.jpg</img></emp>
<emp emp_name="david"><img>david_1.jpg</img></emp>
</emp_detail>
</all_emp>
I create shortcode in functions.php to get all img has attribute is john:
function create_shortcode_empimg() {
$url = 'https://.../emp_test.xml';
$xml = simplexml_load_file("$url") or die("Error: Cannot create object");
foreach ($xml->xpath("//*[@emp_name='john']/img") as $node)
{
$img = (string) $node;
return $img;
}
}
add_shortcode( 'empimg_shortcode', 'create_shortcode_empimg' );
I use this shortcode into my wordpress post.
But, the reult is comming like this:
john_1.jpg
How to get all img has attribute is john like?
john_1.jpg
john_2.jpg
john_3.jpg
Anyone's help is appreciated.