如何为Wordpress构建这个嵌套的php循环?

时间:2016-09-11 09:45:23

标签: php wordpress loops nested-loops

我正在尝试为自定义Wordpress主题构建category.php页面。理想情况下,页面会找到所有类别的帖子,然后抓住该类别的粘贴帖子,如果至少有两个,它会在顶部显示它们(如果下面重复这些相同的帖子就没问题。)

这是我到目前为止构建的页面。它可以无误地收集所有结果,但试图找到最近的两个胶粘物的嵌套循环失败。我收到错误:

“警告:尝试在第22行的C:\ dev \ xampp \ htdocs \ wordpress \ wp-content \ themes \ ampersand \ category.php中分配非对象的属性”

如何修复此循环? (我已经包含了一个github要点,以便更容易阅读,category page for Wordpress theme in development

        <?php // Category Page Content Start ?>
        <div id="content">

            <main id="main" class="category-page" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Periodical">

                <h1><?php single_cat_title(); ?></h1>

                <?php // Find all posts for the queried category ?>
                <?php $catID = get_queried_object_id();
                $args = array( 'numberposts' => -1, 'posts_per_page' => 20,
                    'cat' => $catID, 'post-type' => 'post' );
                $query = new WP_query( $args ); $count = 0;
                if ($query -> have_posts()) : ?>

                    <section class="featured">

                        <?php // Find the most recent sticky posts for this category; if at least 2, post them seperately ?>
                        <?php $sticky = get_option( 'sticky_posts' );
                        $findsticky = array( 'post__in' => $sticky, 'numberposts' => 2 );
                        $stickyposts = get_posts( $findsticky ); if ($stickyposts -> count = 2) :
                        foreach ($stickyposts as $post) : $post -> the_post(); ?>

                            <article class="featured-article" itemtype="http://schema.org/Article">
                                <meta itemprop="wordCount" content="<?php echo word_count(); ?>">

                                <figure class="thumbnail">
                                    <div class="image-wrapper">
                                        <a href="<?php the_permalink() ?>">
                                            <img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
                                        </a>
                                    </div>
                                </figure>

                                <section>                                   
                                    <h2 class="headline">
                                        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="name"><?php the_title(); ?></a>
                                    </h2>
                                    <span class="byline">
                                        <?php $check = get_field( 'contributor' );
                                        if ($check):?>
                                            <span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
                                                <a href=""><?php the_field( 'contributor' ); ?></a>
                                            </span>
                                        <?php else: ?>
                                            <span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
                                        <?php endif; ?>                                     
                                    </span>
                                    <?php  $check = get_field( 'description' );
                                    if ($check): ?>
                                        <span class="description"><?php the_field( 'description' ); ?></span>
                                    <?php else: ?>
                                        <span class="excerpt"><?php the_excerpt(); ?></span>
                                    <?php endif; ?>
                                </section>

                            </article>

                        <?php endforeach; endif; ?>

                    </section>

                    <section class="all-results">

                        <?php while ($query -> have_posts()) : $query -> the_post(); $count++; ?>

                            <article class="<?php echo 'article-'.$count; ?>" itemtype="http://schema.org/Article">
                                <meta itemprop="wordCount" content="<?php echo word_count(); ?>">

                                <figure class="thumbnail">
                                    <div class="image-wrapper">
                                        <a href="<?php the_permalink() ?>">
                                            <img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
                                        </a>
                                    </div>
                                </figure>

                                <section>                                   
                                    <h2 class="headline">
                                        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="name"><?php the_title(); ?></a>
                                    </h2>
                                    <span class="byline">
                                        <?php $check = get_field( 'contributor' );
                                        if ($check):?>
                                            <span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
                                                <a href=""><?php the_field( 'contributor' ); ?></a>
                                            </span>
                                        <?php else: ?>
                                            <span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
                                        <?php endif; ?>                                     
                                    </span>
                                    <?php  $check = get_field( 'description' );
                                    if ($check): ?>
                                        <span class="description"><?php the_field( 'description' ); ?></span>
                                    <?php else: ?>
                                        <span class="excerpt"><?php the_excerpt(); ?></span>
                                    <?php endif; ?>
                                </section>

                            </article>

                        <?php endwhile; ?>

                    </section>

                    <div class="pagination-bottom">
                        <?php if( get_next_posts_link() ) :
                        previous_posts_link( 'Older Posts' );
                        endif; ?>

                        <?php if( get_next_posts_link() ) :
                        next_posts_link( 'Newer Posts', 0 );
                        endif; ?>
                    </div>

                <?php else : ?>

                    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

                <?php endif; wp_reset_postdata(); ?>

            </main>
        </div>

0 个答案:

没有答案