3

I am trying to make a page of restaurant by major cities.

My first page show cities, upon clicking any city I'll show all the restaurants and cliking single restaurant i'll show its details. what is the structure of my pages.

home (home page)
--restaurant (layout/template-restaurant.php)
----Eastern Cape (taxonomy-province.php)
------KFC (single-restaurant.php)

Am I doing it right?

i am using template-sanha-restau.php as page template. taxonomy-province.php show provinces which got restaurants. now i am confuse what hierarchy should i follow to show all restaurants of single province?

add_action( 'init', 'restau' );
function restau(){
  register_post_type( 'sanha-restau',
    array(
      'labels' => array(
        'name' => __( 'Restaurants' ),
        'singular_name' => __( 'Restaurant' )
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-media-text',
      'hierarchical'      => true,
      'show_ui'           => true,
      'show_admin_column' => true,
      'query_var'         => true,
      'rewrite' => array('slug' => 'restaurants',),
      'supports'=> array( 'title', 'editor', 'excerpt', 'author', 'thumbnail',),
    )
  );
}

add_action( 'init', 'create_restau_taxonomy' );

function create_restau_taxonomy() {
    register_taxonomy(
        'province',
        'sanha-restau',
        array(
            'label' => 'Province',
            'hierarchical' => true,
        )
    );
}

kindly advice

code for template-restaurant.php

/*
Template Name: Restaurants Template
The template for displaying show image in header.
*/
get_header();
$custom_terms = get_terms('province');
foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array(
        'post_type' => 'sanha-restau',
        'orderby' => 'title menu_order',
        'tax_query' => array(
            array(
                'taxonomy' => 'province',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );
     $loop = new WP_Query($args);
     if($loop->have_posts()) {
         echo '<div">';
            echo '<a href="' . esc_url( get_term_link( $custom_term, $custom_term->taxonomy ) ) . '">'. $custom_term->name . '</a><br>';
         echo '</div>';
     }
}
get_footer();
3

I think you are in the right direction. You can display all the provinces with get_terms() and display all the restraunts in that perticular province using loop-restraunt.php template.

One more suggestion, you might also want to add "custom-fields" in the "supports" array of register_post_type. This will enable custom fields and will be helpful in many ways.

  • But its not showing expected result – John Paul Jun 20 '17 at 13:31
  • i have change the permalink to 'Plain' and it start working. my whole website is based on 'Post name' – John Paul Jun 20 '17 at 13:44
  • 1
    cool. Glad i could help! – Sid Jun 21 '17 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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