admin管理员组

文章数量:1279042

I am trying to wrap my head around custom post types. What I am trying to achieve is a simple branch listing. There is a dealers menu that has drop downs with regions. When you click on a region I just want it to list branches within the region. Nothing fancy, no maps, just plain text with branch name and contact details + address. Something like this:

Branch Name
Contact Person
Address
Contact number


Branch Name 2
Contact Person 2
Address 2
Contact number 2

I have used custom post UI plugin and another one that lets me add this data in the backend. I can't for the life of me figure out how to display it in the frontend.

I don't want it to act like a post where it lists everything and you click on it for more detail. It should just display as above when you click on a region from the dropdown. Is there a plugin that would do this or do you have to hard code it?

I am trying to wrap my head around custom post types. What I am trying to achieve is a simple branch listing. There is a dealers menu that has drop downs with regions. When you click on a region I just want it to list branches within the region. Nothing fancy, no maps, just plain text with branch name and contact details + address. Something like this:

Branch Name
Contact Person
Address
Contact number


Branch Name 2
Contact Person 2
Address 2
Contact number 2

I have used custom post UI plugin and another one that lets me add this data in the backend. I can't for the life of me figure out how to display it in the frontend.

I don't want it to act like a post where it lists everything and you click on it for more detail. It should just display as above when you click on a region from the dropdown. Is there a plugin that would do this or do you have to hard code it?

Share Improve this question asked Dec 1, 2014 at 5:36 Iggy's PopIggy's Pop 1151 gold badge2 silver badges8 bronze badges 3
  • You have to make a custom post type archive in your theme like: archive-{$post-type}.php. That's all. It's the core thing. :) – Mayeenul Islam Commented Dec 1, 2014 at 5:55
  • Did you create the CPT and custom fields using a plugin? – Brad Dalton Commented Dec 1, 2014 at 8:44
  • Hi. Yes, I used a plugin. Custom Post UI I think it was – Iggy's Pop Commented Dec 1, 2014 at 20:08
Add a comment  | 

3 Answers 3

Reset to default 2

You can do that easy on this way, just duplicate single.php and rename to single-custom-post-type-name.php like single-cars.php, same thing with archive or taxonomy, taxonomy-taxonomy-name.php or archive-taxonomy-name.php

Or you can make your query for random page, home or blog:

<?php
// The Query
$query = new WP_Query(array('post_type' => 'your-custom-post'));
query_posts( $query );

// The Loop
while ( $query->have_posts() ) : $query->the_post();  
  // your post content ( title, excerpt, thumb....)
endwhile;

// Reset Query
wp_reset_query();
?>

Good luck! ;)

?php while (have_posts()) : the_post(); ?>
<?php
    $args = array('post_type' => 'employee', 'posts_per_page' => 10);
    $the_query = new WP_Query($args);
    ?>
    <?php if ($the_query->have_posts()) : ?>
      <?php while ($the_query->have_posts()) : $the_query->the_post();?>

        <?php endwhile;
        wp_reset_postdata(); ?>
    <?php else :  ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
<?php
endwhile;
?>

Try It and Good Luck


$args = array(
    'post_type' => 'your custom post name',
    'post_status' => 'publish',
    'posts_per_page' => 10,
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
    echo the_title(); 
endwhile;endif;
wp_reset_postdata();

本文标签: Displaying custom post types in front end