wp_get_recent_posts stopped working

2019-07-29 09:12发布

I want to return the 2 newest posts when in single post mode, but exclude the current post. And I did it. The problem is, it just stopped working. It didn't change the code and it stopped on the server as well as on my localhost.

Here's the code:

<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>

Does anyone have an explanation ?

I tried removing the argument, still nothing. It returns an empty array.

Any suggestions which other function should I use to achieve the same effect ?

EDIT:

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

标签: wordpress
1条回答
手持菜刀,她持情操
2楼-- · 2019-07-29 09:46

You're missing your loop, or at least an global instance of the current post object. While I prefer to use the loop myself, you can get away with using the latter.

Change:

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

To:

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

UPDATE:

The loop can be used in a single view to grab information from the default query_posts object. Even though it's just one post, the loop is most often used to populate the_content and other information.

You're correct that the ID should be irrelevant in this instance, since wp_get_recent_posts() should still return some results without any arguments (unless, of course, you're dealing with custom post types).

An alternate solution would be to try getting your recent posts using 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 will default to the standard 'orderby'=>'date' and 'order'=>'DESC' parameters, so those don't need to be explicitly stated (though, you can add them if you like).

As mentioned above and in my comment, I'm unsure if you want just the most recent posts, or if you're trying to query recent custom post types. If the recent posts are of type 'post' and that's what you want, then that is the default for WP_Query's 'post_type' parameter as well as wp_get_recent_posts.

Otherwise, you will need to explicitly state the 'post_type' parameter, so that $qargs looks like:

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

Just to check, you can also set 'post_type' to 'all' if you want to ensure SOMETHING is returned. If nothing is returned by either WP_Query or wp_get_recent_posts with 'post_type' set to 'all', then the issue is much larger, and I'll need to see all of the code in your single.php template.

Hope this helps.

NEW UPDATE:

Try commenting out the block of code altogether and substitute for:

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

If that doesn't work, then I'm fresh out of ideas. Something may have happened on your filehost's end that could possibly explain something happening out of nothing. Check and see if they have any announcements about this kind of activity. I know that many filehosts are in the process of replacing PHP4 and older versions of MySQL, which can probably cause some unforseen issues.

I would try creating a new subdomain with a clean, separate installation of Wordpress and a copy of your theme. Once that's set, just create a new post and see if the same problem occurs.

If so, then comment out chunks of code (possibly start with commenting out from get_sidebar through your first <article>...</article> block) in your single.php file and work out any errors that might pop up. If the block of code we worked on suddenly begins to populate, then post the block of code that was preventing it from happening and I'll try to work with you further (that is, if you're still having trouble).

Otherwise, if the clean installation fixes your issues, then it's probably some kind of discrepancy in your wp_options table. I would start merging tables over (first wp_posts, then wp_postmeta, then wp_terms....) until the problem pops up again.

Unfortunately, I think exhaustive testing might be in order to get this anomaly straightened out. Sorry I don't have a pure code solution. This is a pretty strange issue you're going through. Keep me posted, I'll do what I can to help.

查看更多
登录 后发表回答