【问题标题】:How to loop pages in wordpress?如何在wordpress中循环页面?
【发布时间】:2018-02-25 14:52:15
【问题描述】:

我正在尝试制作包含项目的作品集以及一个小博客。我想为我的项目使用“页面”,为我的博客文章使用普通帖子。我正在尝试循环页面,但我不断收到错误消息。

这是我的代码:

<?php
$args = array(
    'post_type' => 'page', // set the post type to page
);
wp_reset_query();
$pages = new WP_Query($args);
if ($pages->have_posts()) { ?>

    <?php

    while ($pages->have_posts()) {
        $count++;
        $pages->the_post(); ?>

        <div>
            <article class="card post">
                <figure class="figure"
                        style="background-image: url(<?php if (has_post_thumbnail()) {
                            the_post_thumbnail_url();
                        } else {
                            bloginfo('template_url'); ?>/resources/assets/images/no-img-post.png <?php } ?>)">
                    <figcaption class="post-img d-flex align-items-center justify-content-center">
                        <a href="<?php echo get_permalink() ?>" title="<?php get_the_title() ?>">
                            <i class="material-icons">link</i>
                        </a>
                    </figcaption>
                </figure>
                <div class="card-body">
                    <a href="<?php echo get_permalink() ?>" title="<?php get_the_title() ?>" class="card-title">
                        <?php echo wp_trim_words(get_the_title(), 60); ?>
                    </a>
                    <div class="card-text">
                        <?php the_content(false, true) ?>
                    </div>
                    <div class="card-bottom text-align-right">
                        <a href="<?php echo get_permalink() ?>" title="Read the full article" class="btn btn-primary">
                            Read More
                            <i class="fa fa-angle-right" aria-hidden="true"></i>
                        </a>
                    </div>
                </div>
            </article>
        </div>

    <?php } } ?>

我不断收到的错误是:

致命错误 :未捕获的错误:调用 C:\xampp\htdocs\ivannikolov\wp-content\themes\port18\index.php:31 中的数组上的成员函数 have_posts() 堆栈跟踪:#0 C:\xampp\htdocs\ivannikolov \wp-includes\template-loader.php(74): include() #1 C:\xampp\htdocs\ivannikolov\wp-blog-header.php(19): require_once('C:\xampp\htdocs.. .') #2 C:\xampp\htdocs\ivannikolov\index.php(17): require('C:\xampp\htdocs...') #3 {main} 抛出 C:\xampp\htdocs\ivannikolov\wp-content\themes\port18\index.php 在线的 31

我该如何解决这个问题?

【问题讨论】:

    标签: php wordpress content-management-system


    【解决方案1】:
    <?php
    $args = array(
        'post_type' => 'page', // set the post type to page
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        ?>
    
        <?php
        while ($query->have_posts()) {
            $query->the_post();
            $count++;
            ?>
    
            <div>
                <article class="card post">
                    <figure class="figure"
                            style="background-image: url(<?php
                            if (has_post_thumbnail()) {
                                the_post_thumbnail_url();
                            } else {
                                bloginfo('template_url');
                                ?>/resources/assets/images/no-img-post.png <?php } ?>)">
                        <figcaption class="post-img d-flex align-items-center justify-content-center">
                            <a href="<?php echo get_permalink() ?>" title="<?php get_the_title() ?>">
                                <i class="material-icons">link</i>
                            </a>
                        </figcaption>
                    </figure>
                    <div class="card-body">
                        <a href="<?php echo get_permalink() ?>" title="<?php get_the_title() ?>" class="card-title">
                            <?php echo wp_trim_words(get_the_title(), 60); ?>
                        </a>
                        <div class="card-text">
            <?php the_content(false, true) ?>
                        </div>
                        <div class="card-bottom text-align-right">
                            <a href="<?php echo get_permalink() ?>" title="Read the full article" class="btn btn-primary">
                                Read More
                                <i class="fa fa-angle-right" aria-hidden="true"></i>
                            </a>
                        </div>
                    </div>
                </article>
            </div>
    
        <?php
        }
    }
    wp_reset_postdata();
    ?>
    

    【讨论】:

      猜你喜欢
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      相关资源
      最近更新 更多