CPT
How to Show Total Number of Posts, Pages, or Custom Post Types
Display title of current Custom Post Type’s taxonomy
$terms = get_the_terms( $post->ID, 'YOUR_TAXONOMY' ); foreach($terms as $term) { echo $term->name; echo $term->slug; }
Echo something conditionally on the single page of specific Custom Post Type Taxonomy
// ================================================================== // SINGLE PAGE OF SPECIFIC CPT TAXONOMY // ================================================================== function single_page_of_specific_cpt_taxonomy(){ global $post; if(is_singular('YOUR_CPT')){ foreach (get_the_terms(get_the_ID(), 'YOUR_TAXONOMY') as $cat) { //echo $cat->slug; if($cat->slug === 'YOUR_TERM'){ echo 'YEAHHH!!!!'; } } } } add_action('genesis_entry_content', 'single_page_of_specific_cpt_taxonomy');
How to Make Custom Post Type Child of Page
'rewrite' => array( 'slug' => get_post_field( 'post_name', get_option('cpt_parent_page') ) . '/PARENT/YOUR_POST_TYPE', 'with_front' => false )
Custom Post Type and Taxonomy
Create file named post-types.php in the mu-plugins (must use) folder:
wp-content/mu-plugins/post-types.php
If You’re using variables, only it’s name need to be changed
You can use more parameters
Remove slug from custom post type post URLs
If You don’t want to use the Custom Post Type’s slug, just paste this snippet in your functions.php and rewrite the post type:
How to fix Yoast breadcrumb in Custom Post Types
Create a file called breadcrumb-fix.php
<div class="breadcrumb-wrapper"> <?php if ( function_exists('yoast_breadcrumb') ){ if(get_post_type() == 'szolgaltatas'){ include('breadcrumb-services.php'); } elseif(get_post_type() == 'post'){ include('breadcrumb-post.php'); } else { yoast_breadcrumb( '<p id="breadcrumbs">','</p>' ); } } ?> </div>
Create another file
<p id="breadcrumbs"> <span xmlns:v="http://rdf.data-vocabulary.org/#"> <span typeof="v:Breadcrumb"> <a href="<?php echo home_url(); ?>/" rel="v:url" property="v:title"> wtsklient.hu </a> </span> <i class="fas fa-chevron-right"></i> <span typeof="v:Breadcrumb"> <?php lang_post_breadcrumbs(); ?> </span> <i class="fas fa-chevron-right"></i> <span typeof="v:Breadcrumb"> <span class="breadcrumb_last" property="v:title"> <?php the_title(); ?> </span> </span> </span> </p>
Create a function for 3 languages
/**************************************************** //BREADCRUMBS POSTS ****************************************************/ function lang_post_breadcrumbs(){ if(ICL_LANGUAGE_CODE=='hu'){ echo '<a href="#">Hírek</a>'; } elseif(ICL_LANGUAGE_CODE=='en'){ echo '<a href="#">News</a>'; } elseif(ICL_LANGUAGE_CODE=='de'){ echo '<a href="#">Nachrichten</a>'; } }
Search only in specific Custom Post Types
//Search only in specific Custom Post Types function searchfilter($query) { if ($query->is_search && !is_admin() ) { $query->set('post_type',array('post','page')); } return $query; } add_filter('pre_get_posts','searchfilter');
Add Images to CPT Taxonomy
I use for add images to categories this plugin: Categories Images
<div class="grid row kategoria"> <?php foreach (get_terms('kategoria') as $cat) : ?> <div class="col-xs-12 col-sm-6 col-md-3"> <a class="cat-link" href="<?php echo get_term_link($cat->slug, 'kategoria'); ?>"> <figure class="effect-ming"> <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /> <figcaption> <h2 class="kategoria"><?php echo $cat->name; ?></h2> <p class="hover hide"><?php echo $cat->name; ?></p> </figcaption> </figure> </a> </div> <?php endforeach; ?> </div>
Custom Post Type Taxonomy Archive Page in Genesis
Create a file with name taxonomy-yourtaxonomy.php
<?php //Include taxonomy template file add_action('genesis_entry_header', 'add_custom_category'); function add_custom_category(){ include('loop/taxonomy-archive.php'); } //Add wrapper open add_action('genesis_before_while', 'add_grid_wrapper_open'); function add_grid_wrapper_open(){ echo '<div class="grid row"> '; } //Add wrapper close add_action('genesis_after_endwhile', 'add_grid_wrapper_close'); function add_grid_wrapper_close(){ echo ' </div>'; } genesis();
Create another file in loop folder with name taxonomy-archive.php
<div class="card melo"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail( 'full', array('class' => 'card-img-top') ); ?> </a> <div class="card-body"> <h4 class="card-title"><?php the_title(); ?></h4> <div class="card-text"> </div> </div> </div>
How to echo CPT taxonomy name and slug
Echo taxonomy slug
<?php foreach (get_the_terms(get_the_ID(), 'your_taxonomy') as $cat) { echo $cat->slug; } ?>
Echo taxonomy name
<?php foreach (get_the_terms(get_the_ID(), 'your_taxonomy') as $cat_name) { echo $cat_name->name; } ?>
Loop Custom Post Type Sub Category
<?php wp_reset_postdata(); $args = array( 'post_type' => 'posttype', 'posts_per_page' => 20, 'tax_query' => array( array( 'taxonomy' => 'taxonomy', 'field' => 'slug', 'terms' => 'subcategory', ), ), ); $query = new WP_Query( $args ); ?> <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>