Hot answers tagged

75

Just setting the argument 'publicly_queryable' => false when you call register_post_type()


57

Visit the permalinks page (which will flush it) and check again. WordPress probably just needs to be nudged to recognize your addition to the hierarchy.


44

You can use template_redirect hook to redirect for a custom post type, you can use any other URL you want to in place of home_url() and the error code in other argument. <?php add_action( 'template_redirect', 'wpse_128636_redirect_post' ); function wpse_128636_redirect_post() { $queried_post_type = get_query_var('post_type'); if ( is_single() &&...


41

You should set your publicly_queryable argument to true when registering your custom post type. TAKE NOTE: Add flush_rewrite_rules(), refresh the page once or twice and REMOVE IT IMMEDIATELY. You SHOULD NOT keep flush_rewrite_rules() unless under the provisions as in the codex. this is an expensive operation so it should only be used when absolutely ...


37

has_term should work in this case: if ( has_term( 'audio', 'product_cat' ) ) { echo 'In audio'; woocommerce_get_template_part( 'content', 'single-product' ); } elseif ( has_term( 'elektro', 'product_cat' ) ) { echo 'In elektro'; woocommerce_get_template_part( 'content', 'single-product' ); } else { echo 'some blabla'; }...


28

I don't think get_categories() is the best option for you in this case because it returns a string with all the categories listed as anchor tags, fine for displaying, but not great for figuring out in code what the categories are. Ok, so the first thing you need to do is grab the product/post object for the current page if you don't already have it: global ...


20

You can use the following instead, if (is_singular('post')) { //your code here... } Where by is_singular is the WordPress API conditional function for testing for the existence of a post type. You can also pass an array of post types if you wish. http://codex.wordpress.org/Function_Reference/is_singular


19

According to the WordPress conditional docs it should be: if ( is_singular( 'video' ) ) { // do conditional stuff } else { //do other stuff } Also if you want a blank page or no content to show when the CPT is not video you don't really need the else so you could omit it. You could ALSO do something like: if ( ! is_singular( 'video' ) ) { /...


14

Flush the rewrite rule from dashboard -> Settings->Permalink page. Click on save button and then check your slide details page. It will work now. Otherwise you can write this code flush_rewrite_rules() in your create_slide_post_type() function. See the Codex


14

It's the nature of the WordPress template hierarchy - point being, if you don't have a page.php, it will use singular.php, same if you don't have single.php, it will fallback to a template lower in the hierarchy. Ideal for themes that have the same layout for posts/pages, instead of having duplicate code in each respective template.


13

singular.php is basically an extra fallback for all post types, regardless if built-in or custom. It comes in straight after single.php and page.php, so you can omit the latter two templates and just have a singular.php template which will be used by all post types in single view. How useful it will be and the necessity of it will, just as with any other ...


12

Update 1: Thanks to @birgire for suggesting a better way: wp_list_categories( [ 'include' => wp_list_pluck( get_the_category(), 'term_id' ) ] ); Try this in your single.php template: $current_cats = get_the_category(); $current_cats_ids = []; foreach ($current_cats as $cat) { $current_cats_ids[] = $cat->term_id; } wp_list_categories([ '...


11

Newly registered CPT shows 404 because, the register_post_type() doesn't flush the rewrite rules. So it's up to you, whether you want to do it manually or automatically. Manually: Get to /wp-admin/, then Settings » Permalinks, and then just hit the Save Changes button to flush the rewrite rules. Automatically: You can flush the rewrite rules using ...


10

You don't want a post to be the front page, you want a custom post type entry to be the front page. Now that we have the terminology right, yes it's possible. A client once asked me to do the exact same thing. They had a custom post type they needed displayed on the front page. Doing so was as simple as adding a filter to allow them to select a "stack" (...


9

Make your single.php the following: <?php $post = $wp_query->post; if ( in_category( 'work' ) ) { include( TEMPLATEPATH.'/single-work-cat.php' ); } else { include( TEMPLATEPATH.'/single-generic.php' ); } ?> and make single-work-cat.php the template you wish to show for single work category posts, and single-generic.php the one you wish ...


9

For the templates WordPress uses, please always refer to Template hierarchy scheme in the Codex. As you can see there, single-{$posttype}-{$slug}.php does not exist, there is only single-{$posttype}.php. To do what you want, have a look at the filter 'single_template': add_filter( 'single_template', function( $template ) { global $post; if ( $post-...


8

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments): add_action( 'pre_get_posts', 'wpse44983_single_post_404' ); function wpse44983_single_post_404( $query ) { if ( $query->is_main_query() ...


8

There's nothing stopping you using AJAX for a singlepage website that loads in other pages dynamically rather than going to a whole new page. I would advise you build the site without the AJAX and then add AJAX on top so that everything degrades gracefully should an error occur or if javascript is turned off. So assuming that you've now got a site that ...


8

It's worth noting that you can go through a list of options by calling an array rather than having to clutter up your code with lots of elseif checks, assuming you want to do the same thing with each category that is. if( has_term( array( 'laptop', 'fridge', 'hats', 'magic wand' ), 'product_cat' ) ) : // Do stuff here else : // Do some other stuff endif;...


8

I found a solution here: http://gabrieleromanato.name/wordpress-fix-the-404-error-on-custom-post-types/ Here's what you need to do to get it to work (quoted from site above): Go to Settings → Permalinks and change your current structure to: /%category%/%postname% Save changes. Restore your original permalink settings. Save changes.


8

A simpler way to do that can be passing the following args when registering the Custom Post Type register_post_type('sample_post_type',array( 'labels' => array( 'name' => _x('Sample Posts', 'post type general name'), 'singular_name' => _x('Sample Post', 'post type singular name') ), 'public' => true, 'exclude_from_search' => true, '...


7

You could try something like (http://codex.wordpress.org/Function_Reference/get_post_type) : to check if it's not a specific post-type : if ( is_single() && 'portfolio' != get_post_type() ) { // DO STUFF } or to check if it's a post and not a post-type : if ( is_single() && 'post' == get_post_type() ) { // DO STUFF }


7

One. From your functions file. add_action( 'template_redirect', 'redirect_cpt_singular_posts' ); function redirect_cpt_singular_posts() { if ( is_singular('your-cpt-slug') ) { wp_redirect( home_url(), 302 ); exit; } } Two. From your single.cpt.php file: <?php wp_redirect( home_url() ); exit; ?>


6

Refresh your permalinks. Go to Admin->Settings->Permalinks and hit Save. It refreshes your permalinks and should help, especially if you are changing rewrite ( 'rewrite' => array( 'slug' => 'slide' ),)


6

I think a hook into template_include like described here could be a proper way to do this. Code could be like this: add_filter('template_include', 'my_plugin_templates'); function my_plugin_templates( $template ) { $post_types = array('post'); if (is_singular($post_types)) { $template = 'path/to/singular/template/in/plugin/folder.php'; ...


6

In your dashboard go to settings/permalinks. Hit save. You should be able to see your cpts now.


6

You should correct the file names on the theme folder matching with the slugs Like: single-services_cpt.php Hope you got the error :)


5

There doesn't appear to be one, but the following should work: //Get post type $post_type_obj = get_post_type_object( get_post_type() ); //Get post type's label $title = apply_filters('post_type_archive_title', $post_type_obj->labels->name ); $archive_title = apply_filters('post_type_archive_title', $post_type_obj->labels-...


5

Well, for anyone else looking to solve a similar problem, I found this post, and it led me in the right direction: http://wordpress.org/support/topic/pagination-on-singlephp-causing-301-redirect?replies=9. I added this snippet of code to my functions.php, and I can paginate in custom queries without issue on single.php now. I don't know enough about it to ...


5

Change the code From : 'has_archive' => true, To : 'has_archive' => false, And then go to permalink page, change to default and go back to your "pretty permalink" %postname%/ Now it should work. The reason why its not going to the single-{custom_post_type}.php page is because of the has_archive. When has_archive is set on ...


Only top voted, non community-wiki answers of a minimum length are eligible