1

別のWordpress DBからの最新の投稿をfooter.phpファイルに入れようとしていますが、明らかに概念を理解していません. 私はWPと「ループ」が初めてなので、助けていただければ幸いです!

<!-- .entry-content -->
<?php
$originaldb = new wpdb('db_name', 'db_password', 'db_user', 'localhost'); //This has been replaced obviously.
$newestPost = $originaldb->query("SELECT * FROM wp_posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 0,1;");
$newestPost = mysql_fetch_array($newestPost);
        if ($newestPost) {
        foreach ($newestPost as $newPost) {
        ?>
            <header class="entry-header">
                <div class="entry-meta">
                    <?php echo '<span class="entry-day">'.get_the_date('j').'</span><br><span class="entry-month">'.get_the_date('M').'</span>'; ?>
                </div>
                <div class="title-box">
                    <h2 class="blog-title"><a href="<?php //the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <?php echo '<a href="'.get_author_posts_url( get_the_author_meta( 'ID' ) ).'">'.get_the_author().'</a>'; ?>
                </div>
                <div class="clear"></div>
            </header>
            <div class="entry-content">
                <?php the_excerpt(); ?>
            </div>
        <?php } //end foreach ?>
    <?php } //endif ?>
4

2 に答える 2

1

あなたが何をしようとしているのか理解できれば...まず、結果行を1つだけ使用する予定なので、foreachループは必要ありません。次に、$newestPost の値に連想配列として直接アクセスしてみませんか? 「the_title()」を置き換えるために「$newestPost['theTitle']」を追加した場所を参照してください。著者とコンテンツについても同様に行いました。

if ($newestPost)  {
       ?>
        <header class="entry-header">
            <div class="entry-meta">
                <?php echo '<span class="entry-day">'.get_the_date('j').'</span><br><span class="entry-month">'.get_the_date('M').'</span>'; ?>
            </div>
            <div class="title-box">
                <h2 class="blog-title"><a href="<?php //the_permalink(); ?>"><?php $newestPost['theTitle']; ?></a></h2>
                <?php echo '<a href="'.get_author_posts_url( get_the_author_meta( 'ID' ) ).'">'.$newestPost['theAuthor']).'</a>'; ?>
            </div>
            <div class="clear"></div>
        </header>
        <div class="entry-content">
            <?php echo $newestPost['theContent']; ?>
        </div>

    <?php
    } //endif ?>

「theTitle」などを db スキーマにあるものに置き換える必要があります。私はWPにあまり興味がないので、何か重要なものを見落としているかもしれませんが、これは普遍的に当てはまるようです.

編集: mysql_fetch_array の使用は推奨されなくなったようです。

于 2012-09-26T19:24:59.027 に答える