0

我采用了 Marc B 建议的答案,但当我在 while 循环之后回显它时,变量中仍然没有任何结果,所以我添加了一些检查来显示通过代码处理的事物的状态。当 if/else 语句接下来运行时,它显示变量有长度的结果。下一个 if/else 语句分支到 else 语句,然后在下一个 if/else 中采用 else 语句,表示 xpath 什么也没找到。所以很明显,当我使用变量 $BEmp3s 时,它里面什么都没有。

这对我来说没有多大意义,因为一开始,$BEpost_content 的回声显示了完整的正确内容,但对其长度的评估显示什么都没有/NULL?请帮忙!

<?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);
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);
};
?>

这是函数 get_the_content() 根据我的知识返回一个字符串:

function get_the_content($more_link_text = null, $stripteaser = 0) {
global $post, $more, $page, $pages, $multipage, $preview;

if ( null === $more_link_text )
    $more_link_text = __( '(more...)' );

$output = '';
$hasTeaser = false;

// If post password required and it doesn't match the cookie.
if ( post_password_required($post) ) {
    $output = get_the_password_form();
    return $output;
}

if ( $page > count($pages) ) // if the requested page doesn't exist
    $page = count($pages); // give them the highest numbered page that DOES exist

$content = $pages[$page-1];
if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
    $content = explode($matches[0], $content, 2);
    if ( !empty($matches[1]) && !empty($more_link_text) )
        $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));

    $hasTeaser = true;
} else {
    $content = array($content);
}
if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
    $stripteaser = 1;
$teaser = $content[0];
if ( ($more) && ($stripteaser) && ($hasTeaser) )
    $teaser = '';
$output .= $teaser;
if ( count($content) > 1 ) {
    if ( $more ) {
        $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
    } else {
        if ( ! empty($more_link_text) )
            $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
        $output = force_balance_tags($output);
    }

}
if ( $preview ) // preview fix for javascript bug with foreign languages
    $output =   preg_replace_callback('/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output);

return $output;

}

4

1 回答 1

1

您之前的问题告诉您检查hrefs 的长度以查看它是否有内容。这是正确的,因为 hrefs 是一个数组。它有一个长度并支持长度属性。get_the_content()返回一个字符串(参见文档)。

要检查字符串长度,请使用strlen

要检查是否为空,请使用is_null

检查是否设置使用isset

isset 和 is_null 的区别

更新

您在以下行询问了为什么您的代码分支不正确:

$hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href");

您还说$xpath在代码中进一步定义。但是,您重新定义$doc了为什么$xpath会有正确的值呢?

$success = $doc->loadHTML($BEpost_content);  //you change $doc here!
$xpath = new DOMXpath($doc);  //so perhaps you should load it into xpath here?
$hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href"); //don't know what this query does. maybe it is broken.
于 2011-11-19T02:08:06.390 回答