WordPress Sticky Post 仅在主页上
WordPress Sticky Post on Homepage only
试图在我的客户主页上显示一个英雄区域显示最新的粘性 post,但我似乎总是遇到障碍。代码末尾的错误使我在死亡时出现白屏。这是我的代码:
<?php if (is_home()) {
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
rsort( $sticky ); // Sort the stickies, latest first
$sticky = array_slice( $sticky, 0, 1 ); // Number of stickies to show
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
<div class="trend-post">
<div class="thumb"><?php the_post_thumbnail(); ?></div>
<div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div></div>
<?php endwhile;?>
<?php wp_reset_query()?>
}
?>
你可以试试这个代码
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 1 );
if (is_numeric($sticky[0])) {
/* Query sticky posts */
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
while ( have_posts() ) : the_post();
the_title('<h3>', '</h3>');
if($post->post_excerpt) :
the_excerpt();
else:
the_content();
endif;
endwhile; // End the loop. Whew.
wp_reset_query();
}
?>
在您的代码中,您忘记为第二个 if statement -> if(have_posts())
添加结尾 }
。此外,我认为您不需要使用 rsort
和 array_slice
来获取第一个最新的粘滞 post。您可以尝试使用下面的编辑代码(未经测试但应该可以工作):
这将显示第一个最新的置顶贴 post。如果没有粘性 post 然后将显示第一个最新的非粘性 post.
<?php if (is_home()) {
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
?>
<div class="trend-post">
<div class="thumb"><?php the_post_thumbnail(); ?></div>
<div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div></div>
<?php
}
wp_reset_query();
}
}
?>
试图在我的客户主页上显示一个英雄区域显示最新的粘性 post,但我似乎总是遇到障碍。代码末尾的错误使我在死亡时出现白屏。这是我的代码:
<?php if (is_home()) {
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
rsort( $sticky ); // Sort the stickies, latest first
$sticky = array_slice( $sticky, 0, 1 ); // Number of stickies to show
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
<div class="trend-post">
<div class="thumb"><?php the_post_thumbnail(); ?></div>
<div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div></div>
<?php endwhile;?>
<?php wp_reset_query()?>
}
?>
你可以试试这个代码
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 1 );
if (is_numeric($sticky[0])) {
/* Query sticky posts */
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
while ( have_posts() ) : the_post();
the_title('<h3>', '</h3>');
if($post->post_excerpt) :
the_excerpt();
else:
the_content();
endif;
endwhile; // End the loop. Whew.
wp_reset_query();
}
?>
在您的代码中,您忘记为第二个 if statement -> if(have_posts())
添加结尾 }
。此外,我认为您不需要使用 rsort
和 array_slice
来获取第一个最新的粘滞 post。您可以尝试使用下面的编辑代码(未经测试但应该可以工作):
这将显示第一个最新的置顶贴 post。如果没有粘性 post 然后将显示第一个最新的非粘性 post.
<?php if (is_home()) {
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
?>
<div class="trend-post">
<div class="thumb"><?php the_post_thumbnail(); ?></div>
<div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div></div>
<?php
}
wp_reset_query();
}
}
?>