我怎样才能使一个定制的邮寄类型变得粘性?

时间:2013-09-08 作者:Bogdan Bătrânu

我安装了“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(); ?>&amp;h=180&amp;w=310&amp;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>

1 个回复
SO网友:s_ha_dum

您的代码仅在post类型存档页上运行。除非您的主页是一个帖子类型的存档(可疑),否则此代码不会运行。

更改此项:

if ( is_main_query() && is_post_type_archive() ) {
对此:

if ( is_main_query() ) {
看看会发生什么。代码将在每个主查询(每个页面)上执行,包括您的主页。你应该在那个页面上看到你想要的结果,但你不能就这样离开它。您必须将该函数更改为仅在主页上运行。类似于:

if ( is_main_query() && is_front_page() ) {
或:

if ( is_main_query() && is_home() ) {
鉴于问题提供的有限背景,我不确定您需要哪一个。

结束