1

So I started a question to figure out why my PHP code that is meant to grab the only MP3 URL that exists within each of my Post contents on my Wordpress installation for a custom use of the content here >> Why doesn't my properly defined variable evaluate for length correctly (and subsequently work in the rest of my code)?

I have made several edits and updates and now need to re-formulate both the question and the context. User mrtsherman points out that based on the order I have written code, that I redefine $doc but don't load that into $xpath, however when I try to adjust this my code throws the fatal error "Object of class DOMAttr could not be converted to string" on the line at the very end where I echo the variable $BEmp3s, the end result of all this. Does this mean it cannot convert the attributes to a string I think??

I know I am soo close to the solution here it's nearly killing me but I also think I have been looking at this wayy too much and for too long. Any insight is golden at this point! Here is my code that branches correctly throughout now:

<?php
    // Start MP3 URL
    $doc   = new DOMDocument();
    $doc->strictErrorChecking = FALSE;

    $xpath = new DOMXpath($doc);
    // End MP3 URL

    $a = 1;
    if (have_posts()) :
        while ( have_posts() ) : the_post();
?>
<?php
$BEpost_content = get_the_content();
if (strlen($BEpost_content) > 0) {
    echo "<div id='debug_content'>get_the_content has something</div>";
} else {
    echo "<div id='debug_content'>BEpost_content is empty</div>" ;
};
$success = $doc->loadHTML($BEpost_content);
$xpath = new DOMXpath($doc);
if ($success === FALSE) {
    echo "<div id='debug_loadcontent'>loadHTML failed to load post content</div>";
} else {
    $hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href");
    if ($hrefs->length > 0) {
        echo "<div id='debug_xpath'>xpath found something</div>";
    } else {
        echo "<div id='debug_xpath'>xpath found nothing</div>";
    };
    $BEmp3s = $hrefs->item(0);
};
?>
<script type="text/javascript">
    var myCP<?php echo $a; ?> = new CirclePlayer("#jquery_jplayer_<?php echo $a; ?>",
    {
        mp3: "<?php echo $BEmp3s; ?>"
    }, {
        cssSelectorAncestor: "#cp_container_<?php echo $a; ?>",
        volume: 0.5
    });
</script>
Community
  • 1
  • 1
lukas56z
  • 157
  • 12

1 Answers1

2
 mp3: "<?php echo $BEmp3s->value; ?>"

Try using value?

Jake N
  • 10,535
  • 11
  • 66
  • 112
  • When I do that and refresh the page it runs correctly error-free but the source reveals the value that should show up in mp3: (the mp3 URL) is empty, like this: mp3: "" – lukas56z Nov 21 '11 at 00:40
  • @virtuapete - Right. The thing to realize is that your XPath is selecting the *attribute*, not the attribute's *value*. An attribute will have a name, a value, and possibly some other properties. It's an abstraction. – Wayne Nov 21 '11 at 03:01