php 自定义帖子类型和分类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 自定义帖子类型和分类相关的知识,希望对你有一定的参考价值。

https://wordpress.stackexchange.com/questions/66219/list-all-posts-in-custom-post-type-by-taxonomy

https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/



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: http://165.227.91.177/index.php/recipes/

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


<?php
$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>
            	<div class="category_link text-center">
	            	<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><strong><?php echo $term->name; ?></strong></a>
            	</div>
            </div>
        <?php } ?>
    
<?php endif;?>



6) open archive.php and enter:
This will display the Recipes of the selected Taxonomy, for example:
http://165.227.91.177/index.php/recipes/appetizers/

<?php
// 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(                   
            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>
	</div>	
	  
	<?php  
     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>
			  </div>
			</div>		
		</div>
    <?php
        endwhile;
     endif;
     ?>
</div>


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:
http://165.227.91.177/index.php/recipesindex/test-12/






以上是关于php 自定义帖子类型和分类的主要内容,如果未能解决你的问题,请参考以下文章

PHP 自定义帖子类型和分类

PHP 带有自定义帖子类型和自定义分类的WordPress图库页面

php 在自定义帖子类型中显示所有自定义分类中的所有帖子。

php 具有自定义分类类别的自定义帖子类型

php 创建具有自定义分类的自定义帖子类型

php 在自定义帖子类型存档和一些自定义分类存档上强制命令到`menu_order`