自定义帖子循环的页面模板未显示正确的信息

时间:2014-04-04 20:50:10

标签: foreach custom-post-type custom-taxonomy

我在过去的5个小时里一直在研究这个问题。我尝试使用多个循环来显示特定分类中的自定义帖子,并在页面模板中运行以下代码,但标题,摘录和固定链接指向页面本身,而不是单个自定义帖子在我的循环中。

令人困惑的是:

  1. 当我在我的函数中使用print_r($posts)来查看是否 它显示正确的帖子来自get_posts($args) 正确的信息,即帖子和内容的标题等。
  2. 正在显示正确的帖子。
  3. 如果没有帖子,if(have_posts($posts))不会显示任何内容。
  4. 好像页面模板的ID覆盖了我的自定义循环 - 任何人都可以在我完全循环之前看到问题是什么(没有双关语意)?

    由于

    大须

    /* ****************************************************************** */
        /* !BOX LAYOUT */
    /* ****************************************************************** */ 
    
    // Function to create boxes
    function osu_fbox($day, $cpt, $tax, $term) {
    
        // Filter posts
        $args   =   array(
                        'post_type'         => $cpt,
                        'posts_per_page'    => -1,
                        'tax_query'         => array(
                            array(
                                'taxonomy'  => $tax,
                                'field'     => 'slug',
                                'terms'     => $term
                            )
                        )
                    );
        $posts  = get_posts( $args );
    
        print_r($posts);
    
        // Display content for this box
        echo '<p class="fheading">' . $day .'</p>';
        if (have_posts($posts)) :
            foreach ( $posts as $post ) : setup_postdata( $post ); ?>
    
            <div id="lk-<?php echo sanitize_title($day); ?>-post-<?php the_ID(); ?>">
                <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'literarykitchen' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark" class="readmore"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
                <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'literarykitchen' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark" class="readmore">Read more...</a>
            </div>
            <?php endforeach; ?>
        <?php else: ?>
            <p>No festival items currently scheduled</p>
        <?php endif; wp_reset_query(); // Reset query so we can go again
    }
    
    
    // ------------------------------------------------------------------
    //  // !BOXES
    // ------------------------------------------------------------------
    
    // CPT and taxonomy
    $cpt            = 'lkfestival';
    $tax            = 'lkdays';
    
    // Start if password protected
    if ( post_password_required() ) : ?>
        <p>We are currently working on the festivals area.</p>
        <p>Please check back soon</p>
        <?php echo get_the_password_form(); ?>
    
    <?php else: ?>
    
        <div id="weekdays" class="fcol">
            <?php echo osu_fbox( 'Monday', $cpt, $tax, 'monday' ); ?>
            <?php echo osu_fbox( 'Tuesday', $cpt, $tax, 'tuesday' ); ?>
            <?php echo osu_fbox( 'Wednesday', $cpt, $tax, 'wednesday' ); ?>
            <?php echo osu_fbox( 'Thursday', $cpt, $tax, 'thursday' ); ?>
            <?php echo osu_fbox( 'Friday', $cpt, $tax, 'friday' ); ?>
        </div> <!-- End #weekdays -->
    
        <div id="saturday" class="fcol">
            <?php echo osu_fbox('Saturday', $cpt, $tax, 'saturday' ); ?>        
        </div> <!-- End #saturday -->
    
        <div id="sunday" class="fcol">
            <?php echo osu_fbox('Sunday', $cpt, $tax, 'sunday' ); ?>
        </div> <!-- End #sunday -->
    
    <?php endif; /* End if post password protected */ ?>
    

2 个答案:

答案 0 :(得分:1)

在调用自定义帖子功能(wp_reset_query()the_title(),...)之前,您应该将the_content()放在某处。

来自Wordpress codex:

  

wp_reset_query()将$ wp_query和全局发布数据恢复到原始主查询。如果必须使用该函数,则应在query_posts()之后调用此函数。如下面的示例所示,强烈建议在查询之前使用pre_get_posts过滤器来更改查询参数。

http://codex.wordpress.org/Function_Reference/wp_reset_query

答案 1 :(得分:0)

好的,我知道如何让它工作,所以我会在这里发布答案。但是,我我应该能够在the_title();循环中使用普通的WP模板标签,例如the_excerpt();get_posts()?我是以正确的方式做到这一点还是可以改进这个答案?

由于

大须

/* ****************************************************************** */
    /* !BOX LAYOUT */
/* ****************************************************************** */ 

// Function to create boxes
function osu_fbox($day, $cpt, $tax, $term) {

    // Filter posts
    $args   =   array(
                    'post_type'         => $cpt,
                    'posts_per_page'    => -1,
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => $tax,
                            'field'     => 'slug',
                            'terms'     => $term
                        )
                    )
                );
    $posts  = get_posts( $args );

    // print_r($posts);
    // echo 'COUNT: ' . count($posts);

    // Display content for this box
    echo '<p class="fheading">' . $day .'</p>';
    if (count($posts) > 0) :
        foreach ( $posts as $post ) : setup_postdata( $post );
        $id         = $post->ID;
        $title      = $post->post_title;
        $excerpt    = $post->post_excerpt;
        $pl         = get_permalink( $post->ID );
        ?>

        <div id="lk-<?php echo sanitize_title($day); ?>-post-<?php echo $id; ?>">
            <h2><a href="<?php echo $pl; ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'literarykitchen' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark" class="readmore"><?php echo $title; ?></a></h2>
            <?php echo $excerpt; ?>
            <a href="<?php echo $pl; ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'literarykitchen' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark" class="readmore">Read more...</a>
        </div>
        <?php endforeach; ?>
    <?php else: ?>
        <p>No festival items currently scheduled</p>
    <?php endif; wp_reset_query(); // Reset query so we can go again
}


// ------------------------------------------------------------------
//  // !BOXES
// ------------------------------------------------------------------

// CPT and taxonomy
$cpt            = 'lkfestival';
$tax            = 'lkdays';

// Start if password protected
if ( post_password_required() ) : ?>

    <p>We are currently working on the festivals area.</p>
    <p>Please check back soon</p>
    <?php echo get_the_password_form(); ?>

<?php else: ?>

    <div id="weekdays" class="fcol">
        <?php echo osu_fbox( 'Monday', $cpt, $tax, 'monday' ); ?>
        <?php echo osu_fbox( 'Tuesday', $cpt, $tax, 'tuesday' ); ?>
        <?php echo osu_fbox( 'Wednesday', $cpt, $tax, 'wednesday' ); ?>
        <?php echo osu_fbox( 'Thursday', $cpt, $tax, 'thursday' ); ?>
        <?php echo osu_fbox( 'Friday', $cpt, $tax, 'friday' ); ?>
    </div> <!-- End #weekdays -->

    <div id="saturday" class="fcol">
        <?php echo osu_fbox('Saturday', $cpt, $tax, 'saturday' ); ?>        
    </div> <!-- End #saturday -->

    <div id="sunday" class="fcol">
        <?php echo osu_fbox('Sunday', $cpt, $tax, 'sunday' ); ?>
    </div> <!-- End #sunday -->

<?php endif; /* End if post password protected */ ?>