我想在单一发布模式下返回2个最新帖子,但排除当前帖子。我做到了。问题是,它刚刚停止工作。它没有改变代码,它停止在服务器以及我的本地主机上。wp_get_recent_posts停止工作
下面的代码:
<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();
?>
为什么我会在单个帖子视图中使用循环? 顺便提一下,帖子ID不是问题。它得到的ID很好。问题是该函数返回零个帖子(即使没有参数) – 2012-07-30 10:13:19
你是正确的,该函数应该仍然返回一个结果数组,无论ID如何。我很抱歉。出于好奇,我们是在处理标准职位?或者你是否试图查询自定义帖子?因为如果您想要查询最近的自定义帖子或页面,则需要设置'post_type'参数。 – maiorano84 2012-07-30 16:58:51
是的,我想返回标准帖子。我试过你的代码,它仍然没有返回。我将尝试编辑帖子并将其中的整个single.php代码粘贴。 – 2012-07-30 23:22:12