Set a conditional argument for multiple Custom Post Types in WordPress

You can set a conditional argument for multiple custom post types in WordPress using in_array

The format is like so…

if( in_array( get_post_type(), array( 'treatment', 'package' )) ) {//add in cpts here
// do stuff
 }
 else { //what all the other post types get
// do other  stuff
}
 }

It searches the array of cpts to see if they belong to the value, which is the function get_post_type() – which they do, and returns true.

So altering the read more excerpt would be like…

<?php
// Change Read More Text For Excerpts - change based on CPT
add_filter( 'excerpt_more', 'prefix_read_more_link' );
function prefix_read_more_link( $more ) {
if( in_array( get_post_type(), array('treatment', 'package')) ) { //add in CPTs
return '<a href="' . get_permalink() . '" class="more-link" title="See More">See More</a>'; //change read more text
}
else { //what all the other post types get
return '<a href="' . get_permalink() . '" class="more-link" title="Read More">Read More</a>';
}
}
view raw cpt.php hosted with ❤ by GitHub

To target the archive pages for multiple custom post types you can use is_post_type_archive

if ( is_post_type_archive( array( 'portfolio', 'testimonial' )) {

// do stuff

}

 

Leave a Comment





%d bloggers like this: