0

Just another basic question of wordpress development. I have a custom post created using the functions.php on theme folder that I am currently developing. I simply want to display all post from it.

function people() {
    register_post_type( 'people',
        array(
            'labels' => array(
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array(
                'title',
                'editor',
                'thumbnail',
                'custom-fields'
            )
        ));
}
add_action( 'init', 'people' );

That is the function I have to put it on the wp-admin. and I tried to display it with this on content-people.php that is located in

theme/my-theme/content-templates/content-people.php

this is how I did it on my a section of my index.php:

<?php
if( have_posts( ) ): while( have_posts( ) ): the_post('people');

    get_template_part( 'content-templates/content', 'people');

endwhile;
endif;
?> 

and this is what I have on **content-people.php

<div class="col-lg-3 col-md-6 text-center">
    <div class="service-box">
        <i class="fa fa-4x fa-diamond text-primary sr-icons"></i>
        <h3><?php the_title(); ?></h3>

    </div>
</div>

But, it shows just the standard/default post from WordPress not the one that I have defined. I have tried to find it on WordPress codex guide, but still cannot find the exact solution for it.

3 Answers 3

0

Try below code

index.php

<?php 
$args = array(
       'post' => 'people',
       'post_status' => 'publish'
       )

 query_posts( $args ); ?>

<?php if ( have_posts() ) : ?>


 <!-- the loop -->
 <?php while ( have_posts() ) : the_post(); ?>

   <?php get_template_part( 'content-templates/content', 'people' ); ?>

 <?php endwhile; ?>

<?php else : ?>
 <?php get_template_part( 'content-templates/content', 'none' ); ?>
<?php endif; ?>

content-people.php

<div class="col-lg-3 col-md-6 text-center">
<div class="service-box">
   <i class="fa fa-4x fa-diamond text-primary sr-icons"></i>
       <h3><?php the_title(); ?></h3>

</div>

Hope this will helps you.

0

Thank you for the response. I think we have the same approach. here is what I have done and it works.

 <?php 
            $args =  array( 
                'post_type' => 'people',
                'orderby' => 'menu_order',
                'order' => 'ASC'
            );
             $custom_query = new WP_Query( $args );
                while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
                    <div class="col-lg-3 col-md-6 text-center">
                            <div class="service-box">
                                <i class="fa fa-4x fa-diamond text-primary sr-icons"></i>
                                    <h3><?php the_title(); ?></h3>
                                        <p class="text-muted">
                                           <?php the_excerpt(); ?>
                                        </p>
                            </div>
                    </div>

                <?php endwhile; ?>
0

You should go through the Template Hierarchy, it explains how WordPress determines which template file(s) to use on individual pages.

If you want to show the archive of all posts in your custom post type, you are going to need a new template file called archive-{post_type}.php.

In you scenario, you're going to create archive-people.php that would be located in theme/my-theme/archive-people.php

<?php
if( have_posts( ) ): while( have_posts( ) ): the_post('people');

    get_template_part( 'content-templates/content', 'people');

endwhile;
endif;
?> 

This way wordpress loads this Template file when you visit yoursite at example.com/people and you don't have to write a custom query to handle the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.