wp_get_recent_posts 停止工作 [英] wp_get_recent_posts stopped working

查看:24
本文介绍了wp_get_recent_posts 停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个帖子模式下返回 2 个最新帖子,但排除当前帖子.我做到了.问题是,它刚刚停止工作.它没有更改代码,并且在服务器和我的本地主机上都停止了.

代码如下:

<header class='recent_header'>最近的帖子</标题><?php$id = $post->ID;$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");foreach( $recent_posts as $recent ) { ?><文章类='single_recent'><标题><a href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo $recent["post_title"];?></a></标题><p><?php echo get_excerpt_by_id($recent["ID"]);?></p></文章><?php } ?></节>

有没有人解释一下?

我尝试删除参数,仍然没有.它返回一个空数组.

有什么建议我应该使用哪些其他功能来达到相同的效果?

 <?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>通过 <?php the_author();?></strong>

</标题><?php if (has_post_thumbnail()) : ?><figure class='post_single_image'><?php the_post_thumbnail();?><figcaption>No Will No Skill</figcaption></图><?php endif;?><div class='post_perex'><?php the_content();?>

<footer class='post_footer'><div class='post_footer_top'>

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

</页脚></文章><?php comments_template();?>

<header class='recent_header'>最近的帖子</标题><?php全球 $post;$id = $post->ID;$qargs = 数组('post__not_in'=>数组($ id),'posts_per_page' =>2);$recent_posts = new WP_Query($qargs);if ($recent_posts->have_posts()) echo 'yes';否则回声'不';if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post();?><文章类='single_recent'><标题><a href="<?php the_permalink(); ?>"><?php the_title();?></a></标题><p><?php the_excerpt();?></p></文章><?php endwhile;endif;?></节><div class='space'></div>

<?phpget_footer();?>

解决方案

您缺少循环,或者至少缺少当前帖子对象的全局实例.虽然我更喜欢自己使用循环,但您可以避免使用后者.

变化:

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

致:

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

更新:

可以在单个视图中使用循环从默认的 query_posts 对象中获取信息.尽管它只是一个帖子,但循环最常用于填充 the_content 和其他信息.

在这种情况下,ID 应该无关紧要是正确的,因为 wp_get_recent_posts() 仍应返回一些没有任何参数的结果(当然,除非您正在处理自定义帖子类型).

另一种解决方案是尝试使用 WP_Query 获取您最近的帖子:

<header class='recent_header'>最近的帖子</标题><?php全球 $post;$id = $post->ID;$qargs = 数组('post__not_in'=>数组($ 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();?><文章类='single_recent'><标题><a href="<?php the_permalink(); ?>"><?php the_title();?></a></标题><p><?php the_excerpt();?></p></文章><?php endwhile;endif;?></节>

WP_Query 将默认为标准的 'orderby'=>'date' 和 'order'=>'DESC' 参数,因此不需要明确说明这些参数(不过,您可以根据需要添加它们).

如上所述,在我的评论中,我不确定您是否只想要最近的帖子,或者您是否正在尝试查询最近的自定义帖子类型.如果最近的帖子属于post"类型并且这就是您想要的,那么这是 WP_Query 的post_type"参数以及 wp_get_recent_posts 的默认值.

否则,您将需要明确声明post_type"参数,以便 $qargs 看起来像:

$qargs = 数组('post__not_in'=>数组($ id),'posts_per_page' =>2、'post_type' =>数组('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....),直到问题再次出现.

不幸的是,我认为详尽的测试可能是为了解决这个异常问题.抱歉,我没有纯代码解决方案.这是你正在经历的一个非常奇怪的问题.随时关注我,我会尽我所能提供帮助.

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

解决方案

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.

这篇关于wp_get_recent_posts 停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆