仅在主页上张贴WordPress贴纸



试图在我的客户家中获得英雄区显示了最近的粘性帖子,但我似乎一直遇到了障碍。错误就在代码的末尾,这让我看到了死亡的白色屏幕。这是我的代码:

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

在代码中,您忘记为第二个if statement -> if(have_posts())添加结束}。此外,我认为你不需要使用rsortarray_slice来获得第一个最新的粘性帖子。您可以尝试使用以下经过编辑的代码(未经过测试,但应该可以使用(:这将显示第一个最新的帖子。若并没有粘性帖子,那个么将显示第一个最新的非粘性帖子。

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

您可以尝试此代码

<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 1 );
if (is_numeric($sticky&#91;0&#93;)) {
/* 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();
}
?>

最新更新