我安装了“Sticky Custom Post Types“插件,我将此代码添加到function.php
:
function wpb_cpt_sticky_at_top( $posts ) {
// apply it on the archives only
if ( is_main_query() && is_post_type_archive() ) {
global $wp_query;
$sticky_posts = get_option( \'sticky_posts\' );
$num_posts = count( $posts );
$sticky_offset = 0;
// Find the sticky posts
for ($i = 0; $i < $num_posts; $i++) {
// Put sticky posts at the top of the posts array
if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {
$sticky_post = $posts[$i];
// Remove sticky from current position
array_splice( $posts, $i, 1 );
// Move to front, after other stickies
array_splice( $posts, $sticky_offset, 0, array($sticky_post) );
$sticky_offset++;
// Remove post from sticky posts array
$offset = array_search($sticky_post->ID, $sticky_posts);
unset( $sticky_posts[$offset] );
}
}
// Look for more sticky posts if needed
if ( !empty( $sticky_posts) ) {
$stickies = get_posts( array(
\'post__in\' => $sticky_posts,
\'post_type\' => $wp_query->query_vars[\'post_type\'],
\'post_status\' => \'publish\',
\'nopaging\' => true
) );
foreach ( $stickies as $sticky_post ) {
array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
$sticky_offset++;
}
}
}
return $posts;
}
add_filter( \'the_posts\', \'wpb_cpt_sticky_at_top\' );
// Add sticky class in article title to style sticky posts differently
function cpt_sticky_class($classes) {
if ( is_sticky() ) :
$classes[] = \'sticky\';
return $classes;
endif;
return $classes;
}
add_filter(\'post_class\', \'cpt_sticky_class\');
在仪表板上一切正常,但在我的主页上什么也没发生。这是显示主页内容的代码:
<div id="content">
<?php include (TEMPLATEPATH . \'/lib/slider.php\'); ?>
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query(\'post_type=listings\'.\'&paged=\'.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">
<?php if( has_term( \'featured\', \'type\', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( \'sold\', \'type\', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( \'reduced\', \'type\', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>
<?php
if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>"><img class="propimg" src="<?php bloginfo(\'stylesheet_directory\'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=180&w=310&zc=1" alt=""/></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><img class="propimg" src="<?php bloginfo(\'template_directory\'); ?>/images/dummy.jpg" alt="" /></a>
<?php } ?>
</div>
<div class="cover">
<div class="title">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
</div>
<div class="propmeta">
<div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, \'wtf_price\', true); echo $price; ?></span></div>
<div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, \'location\', \'\', \' \', \'\' ); ?></span></div>
<div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, \'property\', \'\', \' \', \'\' ); ?></span></div>
<div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, \'area\', \'\', \' \', \'\' ); ?></span></div>
</div>
<div class="entry">
<?php wpe_excerpt(\'wpe_excerptlength_archive\', \'\'); ?>
<a class="morer" href="<?php the_permalink() ?>">Check this</a>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>