最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - add page items to index

programmeradmin1浏览0评论

I want to show page items in my wordpress theme and this is the code:

<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>

but in index just show posts!!how can I change it and show posts?

I want to show page items in my wordpress theme and this is the code:

<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>

but in index just show posts!!how can I change it and show posts?

Share Improve this question asked Jun 16, 2020 at 7:48 hadishadis 212 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If you want posts, try using get_posts, https://developer.wordpress/reference/functions/get_posts/

You can check if the page is the Front or Home page Depending on the site’s "Front page displays" Reading Settings show_on_front and page_on_front.

There, you will be able to display Posts on your index and Pages on other places:

<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>
发布评论

评论列表(0)

  1. 暂无评论