1

I have created a custom post type named property, and a taxonomy named city is attached to this post type. There are several cities (terms) like toronto, taiwan,dhaka.

I want to create a template which can show all the post under a city. I have created file taxonomy-city.php, but it is not working .

Here is my code for taxonomy-city.php file .

<?php
/*
 * Template name: City
*/
get_header();

?>
<div id="page_content">
        <div class="page-wrapper">

            <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
            <div id="page_content_wrapper">

                <div id="careers_table">
                    <p class="content_text">asdsd</p>

                </div>

                    <?php endwhile; ?>
                    <?php else : ?>
                            <div class="post">
                                <h3><?php _e('404 Error&#58; Not Found', 'cmeasytheme'); ?></h3>
                            </div>
                    <?php endif; ?> 

            </div>
            <div style="clear:both;"></div>
        </div>
        </div>
<?php get_footer(); ?>

It shows everything, but doesn't print out anything inside the loop.

I have visited these link too , but none of them came to any help

2
  • Is it possible your taxonomy is actually called cities, therefore the file should be taxonomy-cities.php? Also, did you register the taxonomy with public=true? Oct 9, 2014 at 6:52
  • No , my taxonomy name is "city" and public = true Oct 9, 2014 at 6:56

1 Answer 1

3

First of all, a taxonomy-{taxonomy}.php file is an Archive Template and not a good name for a custom template. So if you want to proceed with a custom template, try naming the file with something like template-cities.php and call the file to a Page.

Otherwise, make a simple taxonomy-{taxonomy}.php archive template, and in your case taxonomy-city.php with code like:

<?php get_header(); ?>
<div id="page_content">
    <div class="page-wrapper">
        <div id="page_content_wrapper">
            <?php if (have_posts()) : ?>
                <div id="careers_table">
                    <?php while (have_posts()) : the_post(); ?>
                        <p class="content_text"><?php the_title(); ?></p>
                    <?php endwhile; ?>
                    <?php // pagination code here ?>
                </div>
            <?php else : ?>
                <div class="post">
                    <h3><?php _e('No City Found', 'cmeasytheme'); ?></h3>
                </div>
            <?php endif; ?>
        </div> <!-- .page_content_wrapper -->
        <div style="clear:both;"></div>
    </div> <!-- .page-wrapper -->
</div> <!-- .page_content -->
<?php get_footer(); ?>

And in this way, NO NEED to make any page and call a page template.

Suggestions

Though the suggestions are not related to WordPress, but related to Good practice:

  • Try naming you classes hyphen-separated (-), NOT underscore-separated (_). So class like page_content_wrapper would be page-content-wrapper.
  • Avoid inline styles, and make a class instead. So the clearing div <div style="clear:both;"></div> can be something like <div class="clearfix"></div> and you can place .clearfix{clear:both} in your external CSS file.
  • Use important commenting so that you can have trace of the closing divs etc.
3
  • Thanks for your answer . It helps me a lot. But the problem was something different , I made a mistake when I created the "property" post type . I made 'exclude_from_search' => true, , after changing it to false , now template is showing data Oct 9, 2014 at 9:03
  • i have found tutorial related custom taxonomies which base on hierarchical and Non-hierarchical. Hope it will help you to solve your problem wpblog.com/create-custom-taxonomies-in-wordpress
    – Owais Alam
    Aug 10, 2017 at 7:21
  • will I able to relation with the product ? Dec 1, 2019 at 6:52

Your Answer

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

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