2

シングル投稿モードのときに最新の2つの投稿を返したいのですが、現在の投稿を除外します。そして、私はそれをしました。問題は、それが機能しなくなったことです。コードは変更されず、サーバーとローカルホストで停止しました。

コードは次のとおりです。

<section id='recent_posts'>

            <header class='recent_header'>
                Recent posts
            </header>

            <?php 
                $id = $post->ID;
                $recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");

                foreach( $recent_posts as $recent ) { ?>                        

                    <article class='single_recent'>
                        <header>
                            <a href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo $recent["post_title"]; ?></a>
                        </header>
                        <p>
                            <?php echo get_excerpt_by_id($recent["ID"]); ?>
                        </p>
                    </article>

            <?php } ?>

        </section>

誰か説明がありますか?

私は引数を削除しようとしましたが、それでも何もありません。空の配列を返します。

同じ効果を達成するために他のどの機能を使用すべきか提案はありますか?

編集:

    <?php 

get_header();
get_sidebar();

?>

        <?php the_post() ?>

        <article class='post-single'>

                <header class='post_header'>

                    <h1><?php the_title(); ?></h1>

                    <div class='post_header_bottom'>
                        <strong class='post_category'><?php echo get_the_category_list(', '); ?></strong>
                        <strong class='post_author'><span class='symbol'>U</span> by <?php the_author(); ?></strong>
                    </div>

                </header>

                <?php if (has_post_thumbnail()) : ?>
                <figure class='post_single_image'>
                    <?php the_post_thumbnail(); ?>
                    <figcaption>No Will No Skill</figcaption>
                </figure>
                <?php endif; ?>

                <div class='post_perex'>
                    <?php the_content(); ?>
                </div>

                <footer class='post_footer'>

                    <div class='post_footer_top'>

                        <div class='post_tags'>
                            <?php the_tags('', '', ''); ?>
                        </div>

                        <div class='post_time'>
                            <time datetime='<?php the_time('Y-m-d'); ?>' pubdate>
                                <span class='symbol'>P </span>
                                <?php relative_post_the_date(); ?>
                            </time>
                        </div>

                    </div>

                    <div class='post_share'>

                            <div class='share_show'>
                                <span class='symbol'>f</span> Like
                                 | 
                                <span class='symbol'>g</span> +1
                                 | 
                                <span class='symbol'>t</span> Tweet

                                <?php
                                    if(function_exists('display_social4i'))
                                        echo display_social4i("large","align-left");
                                ?>

                            </div>

                        </div>

                </footer>

            </article>

            <?php comments_template(); ?>   

            <section id='recent_posts'>

                <header class='recent_header'>
                    Recent posts
                </header>

                <?php 
                    global $post;
                    $id = $post->ID;
                    $qargs = array(
                        'post__not_in'=> array($id),
                        'posts_per_page' => 2
                    );
                    $recent_posts = new WP_Query($qargs);

                    if ($recent_posts->have_posts()) echo 'yes'; else echo 'nope';

                    if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>                        

                        <article class='single_recent'>
                            <header>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </header>
                            <p>
                                <?php the_excerpt(); ?>
                            </p>
                        </article>
                <?php endwhile;endif; ?>
            </section>

            <div class='space'></div>
        </div>


<?php
get_footer();
?>
4

1 に答える 1

0

ループ、または少なくとも現在の投稿オブジェクトのグローバル インスタンスがありません。私はループを自分で使用することを好みますが、後者を使用しても問題ありません。

変化する:

$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");

に:

global $post;
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");

アップデート:

ループを単一のビューで使用して、デフォルトの query_posts オブジェクトから情報を取得できます。たった 1 つの投稿ですが、ループは the_content やその他の情報を入力するために最もよく使用されます。

wp_get_recent_posts() は引数なしでいくつかの結果を返す必要があるため (もちろん、カスタム投稿タイプを扱っている場合を除きます)、このインスタンスでは ID は無関係であることは間違いありません。

別の解決策は、WP_Query を使用して最近の投稿を取得することです。

<section id='recent_posts'>

        <header class='recent_header'>
            Recent posts
        </header>

        <?php 
            global $post;
            $id = $post->ID;
            $qargs = array(
                'post__not_in'=> array($id),
                'posts_per_page' => 2
            );
            $recent_posts = new WP_Query($qargs);

            if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>                        

                <article class='single_recent'>
                    <header>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </header>
                    <p>
                        <?php the_excerpt(); ?>
                    </p>
                </article>
        <?php endwhile;endif; ?>
</section>

WP_Query は標準の 'orderby'=>'date' および 'order'=>'DESC' パラメーターにデフォルト設定されるため、明示的に指定する必要はありません (ただし、必要に応じて追加できます)。

上記と私のコメントで述べたように、最新の投稿だけが必要なのか、それとも最近のカスタム投稿タイプをクエリしようとしているのかわからない. 最近の投稿のタイプが「post」であり、それが必要な場合は、それが WP_Query の「post_type」パラメーターと wp_get_recent_posts のデフォルトです。

それ以外の場合は、'post_type' パラメータを明示的に指定する必要があるため、$qargs は次のようになります。

$qargs = array(
    'post__not_in'=> array($id),
    'posts_per_page' => 2,
    'post_type' => array('post', 'custom_post_type_slug', ...)
);

確認のために、SOMETHING が確実に返されるようにする場合は、'post_type' を 'all' に設定することもできます。「post_type」を「all」に設定して WP_Query または wp_get_recent_posts から何も返されない場合、問題はさらに大きく、single.php テンプレートのすべてのコードを確認する必要があります。

お役に立てれば。

新しいアップデート:

コードのブロック全体をコメントアウトして、次のものに置き換えてみてください。

wp_get_archives(array('type'=>'postbypost'));

それがうまくいかない場合、私はアイデアがありません。ファイルホスト側で何かが起こった可能性があり、何もないところから何かが起こっていることを説明できる可能性があります。この種の活動に関する発表があるかどうかを確認してください。多くのファイルホストが PHP4 と古いバージョンの MySQL を置き換えていることを知っています。これにより、予期しない問題が発生する可能性があります。

WordPress のクリーンな個別インストールとテーマのコピーを使用して、新しいサブドメインを作成してみます。設定したら、新しい投稿を作成して、同じ問題が発生するかどうかを確認してください。

その場合は、single.php ファイル内のコードのチャンクをコメント アウトし (get_sidebar から最初の<article>...</article>ブロックまでコメント アウトすることから始めることができます)、ポップアップする可能性のあるエラーを解決します。私たちが取り組んでいたコード ブロックが突然生成され始めた場合は、それを妨げていたコード ブロックを投稿してください。さらに協力します (つまり、まだ問題がある場合)。

それ以外の場合、クリーン インストールで問題が解決する場合は、おそらく wp_options テーブルになんらかの不一致がある可能性があります。問題が再び発生するまで、(最初に wp_posts、次に wp_postmeta、次に wp_terms....) テーブルのマージを開始します。

残念ながら、この異常を正すために徹底的なテストが行​​われる可能性があると思います。申し訳ありませんが、純粋なコード ソリューションはありません。これはあなたが経験しているかなり奇妙な問題です。お知らせください。できる限りのことをします。

于 2012-07-29T16:27:23.603 に答える