add page items to index

时间:2020-06-16 作者:hadis

我想在wordpress主题中显示页面项目,代码如下:

<section id="two">
    <div class="inner">
        <?php
        $pagesargs = array(
            \'posts_per_page\'=> 2,
            \'offset\'=> 0,
            \'category\' => \'\',
            \'category_name\' => \'\',
            \'orderby\' => \'post_date\' ,
            \'order\' => \'DESC\',
            \'include\' => \'\',
            \'exclude\' => \'\',
            \'meta_key\' => \'\',
            \'meta_value\' => \'\',
            \'post_type\'    => \'page\',
            \'post_mime_type\' => \'\',
            \'post_parent\' => \'\',
            \'post_status\' => \'publish\',
            \'suppress_filters\' => true,
        );
        $my_pages = get_pages($pagesargs);
        foreach ($my_pages as $page){
        ?>
        <article>
            <div class="content">
                <header>
                    <h3><?php the_title() ?></h3>
                </header>
                <div class="image fit">
                    <img src="<?php bloginfo(\'template_url\')?>/style/images/pic01.jpg" alt="" />
                </div>
                <p><?php the_excerpt(); ?></p>
            </div>
        </article>
    <?php } ?>
    </div>
</section>
但在索引中只显示帖子!!如何更改并显示帖子?

2 个回复
SO网友:mozboz

如果您想要帖子,请尝试使用get_posts, https://developer.wordpress.org/reference/functions/get_posts/

SO网友:gael

您可以根据网站的;首页显示;正在读取设置show_on_frontpage_on_front.

在那里,您将能够在索引上显示帖子,并在其他位置显示页面:

<section id="two">
    <div class="inner">
    <?php
        if ( is_home() || is_front_page() ) {
   $post_type="post";
        }
        else {
   $post_type="page";
    }
        $pagesargs = array(
            \'posts_per_page\'=> 2,
            \'offset\'=> 0,
            \'category\' => \'\',
            \'category_name\' => \'\',
            \'orderby\' => \'post_date\' ,
            \'order\' => \'DESC\',
            \'include\' => \'\',
            \'exclude\' => \'\',
            \'meta_key\' => \'\',
            \'meta_value\' => \'\',
            \'post_type\'    => $post_type,
            \'post_mime_type\' => \'\',
            \'post_parent\' => \'\',
            \'post_status\' => \'publish\',
            \'suppress_filters\' => true,
        );
        
        if ( is_home() || is_front_page() ) {
        $my_pages = get_posts($pagesargs);
        }
        else {
        $my_pages = get_pages($pagesargs);
        }
        foreach ($my_pages as $page){
        ?>
        <article>
            <div class="content">
                <header>
                    <h3><?php the_title() ?></h3>
                </header>
                <div class="image fit">
                    <img src="<?php bloginfo(\'template_url\')?>/style/images/pic01.jpg" alt="" />
                </div>
                <p><?php the_excerpt(); ?></p>
            </div>
        </article>
    <?php } ?>
    </div>
</section>

相关推荐