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