12/10/2017 - 1:41 AM

Custom post type and Taxonomy



1) Create Custom post type: slug: recipesindex,  Name: [ Recipes ]
   --- Hierarchical: TRUE

2) create taxonomy  : slug: recipes,  Name: [ Recipes Taxonomy ]
   -- Hierarchical : TRUE

3) In taxonomy Attach to Post type, select: [Recipes]

4) In Custom Post type Built-in Taxonomies, select:  [Recipes Taxonomies]

5) Create a new Page template for Recipes Index Page: page-recipes.php and enter:
NOTE: we have a custom field that belongs to: All Recipes > Recipes Taxonomy 
that will add an image field when you go to the Recipes Categories that belong
to this Recipes Taxonomy only and you will have an image to display for the main categories
in the index Recipes Page. 

field Name: category_image

example URL:

<div class="category_line">
<h1><?php the_title() ;  ?></h1>

$taxonomy = 'recipes';

$terms = get_terms($taxonomy);

if ( $terms && !is_wp_error( $terms ) ) :  ?>
        <?php foreach ( $terms as $term ) {    ?>
            <div class="col-md-6 col-sm-4 col-xm-12 category_cont">
            	<?php $image = get_field('category_image', $term);  ?>
            	<div class="category_image">
	            	<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><img src="<?php echo $image; ?>"></a>
            	<div class="category_link text-center">
	            	<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><strong><?php echo $term->name; ?></strong></a>
        <?php } ?>
<?php endif;?>

6) open archive.php and enter:
This will display the Recipes of the selected Taxonomy, for example:

// get current category taxonomy name and slug from the URL after you click one
// of the main Recipes in the Recipe Index Page
$taxonomy_name = get_queried_object()->name;
$url_taxonomy = get_queried_object()->slug;
echo  '<!-- ARCHIVE DEFAULT.PHP  -->' ;

// echo 'CATEGORY TAXONOMY NAME: ' . get_queried_object()->name . '<br>' ;
// echo 'CATEGORY TAXONOMY SLUG: ' . get_queried_object()->slug. '<br>' ; 

$args =  array(
        'posts_per_page' => -1,
        'post_type' => 'recipesindex',        
        'tax_query' => array(                   
                'taxonomy' =>  'recipes',
                'field' => 'slug',    
                'terms' => $url_taxonomy,
$loop = new WP_Query($args);     

<div class="element-top-50"></div>

<div class="container"> 
	<div class="category_line">
		<h1><?php echo get_queried_object()->name ;  ?></h1>
     if($loop->have_posts()) : 
        while($loop->have_posts()) : $loop->the_post(); ?>
		<div class="col-md-4 col-sm-6 col-xs-12">
			<div class="img__wrap">
			  <img class="img__img" src="<?php echo get_field('recipe_image') ?>">
			  <div class="img__description_layer">
			    <p class="img__description"><a href="<?php the_permalink() ; ?>"><?php the_title() ; ?></a></p>

7) create: single-recipesindex.php 
and retrieve the post data, custom fields can
also be used, this will open the selected recipe from the archive above that contains
one or more recipes that belong that Taxonomy, for example: