WordPress粘贴帖不显示
问题描述:
在我的Index.php模板文件中,我有下面的代码,它应该显示一个特色帖子,如果它是粘性的,然后循环通过下面的其余帖子。然而在第二个循环显示任何东西和粘贴帖子不会出现...WordPress粘贴帖不显示
该帖子已明确粘滞,第二个循环已成功隐藏它,但第一个循环只是不承认它:/它没有设置私有并被标记为已发布。
任何人都可以看到代码的任何问题?由于
<?php get_header(); ?>
<div class="clearfix">
<?php if(is_home()) { //if home then show the featured post and others ?>
<div class="main-column">
<?php
$sticky = get_option('sticky_posts');
$sticky_query = new WP_Query('p=' . $sticky[0]);
// The Loop
while ($sticky_query->have_posts()) : $sticky_query->the_post(); ?>
<div class="featured">
<article class="clearfix">
<h2><a href="<?php echo get_permalink() ?>"><?php echo $post->post_title ?></a></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<a href="'.get_permalink().'"><img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" /></a>';
}?>
<p><?php the_excerpt_max_charlength(300) ?></p>
</article>
</div>
<?php endwhile; ?>
<div class="articles">
<?php $query = new WP_Query(array('post__not_in' => get_option('sticky_posts'), 'posts_per_page' => 10));
$count = 0;
while ($query->have_posts()) : $query->the_post();
$count++; ?>
<div <?php if($count % 2 == 0) echo 'class="no-margin"' ?>>
<article>
<h2><a href="<?php the_permalink(); ?>"><?php echo $post->post_title ?></a></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<a href="'.get_permalink().'"><img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" /></a>';
}?>
<p><?php the_excerpt_max_charlength(150) ?></p>
</article>
</div>
<?php if($count % 2 == 0) echo '<hr/>' ?>
<?php endwhile; ?>
</div>
<?php }else if(is_search() || is_category || is_tag()){ //if other than home (search, tag, category) then show normal list (no featured style) ?>
<div class="main-column search">
<?php
$count = 0;
if(is_search()){
echo '<h1>Search Results</h1>';
} elseif(is_author()){ ?>
<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
<h1><?php echo $curauth->user_firstname; ?> <?php echo $curauth->user_lastname; ?></h1>
<div class="archive-meta">
<p><?php echo $curauth->user_description; ?></p>
</div>
<?php } else if(is_category()){ ?>
<h1><?php single_cat_title('', true); ?></h1>
<?php
$category_description = category_description();
if (! empty($category_description))
echo '<div class="archive-meta">' . $category_description . '</div>';
?>
<?php }else if (is_tag()){
echo '<h1>'.the_tag().'</h1>';
}
if (have_posts()) : while (have_posts()) : the_post();
$count++; ?>
<div>
<article>
<h2><a href="<?php the_permalink(); ?>"><?php echo $post->post_title ?></a></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<a href="'.get_permalink().'"><img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" /></a>';
}?>
<p><?php the_excerpt_max_charlength(300) ?></p>
</article>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php } ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
答
的代码更改为:
$sticky_query = new WP_Query('p=' . $sticky[1]);
修复这个问题,但我不知道为什么???
你是如何将帖子标记为粘滞的? – 2011-12-19 14:03:22
我在这里没有看到任何错误。要开始调试,请尝试回显''sticky''变量以查看您获得的内容。 – ronakg 2011-12-19 14:32:12
@RonakG我得到一个错误'可捕获的致命错误:类WP_Query的对象无法转换为字符串',如果我试着回显, – Cameron 2011-12-19 17:36:13