A special single page templates for posts under a category and all its child category

You can do that with single_template filter. First you will need to check if a post belongs to a top level category. So here is the function. // custom single template for specific category function wpse_custom_category_single_template( $single_template ) { global $post; // get all categories of current post $categories = get_the_category( $post->ID ); $top_categories = … Read more

Single custom post type page redirecting to 404 page

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 … Read more

Related Posts by Multiple Tags?

I had the same idea and wrote a small little plugin to help me do this. function get_pew_related_data($args, $post_id, $related_id) { global $post, $wpdb; $post_id = intval( $post_id ); if( !$post_id && $post->ID ) { $post_id = $post->ID; } if( !$post_id ) { return false; } $defaults = array( ‘taxonomy’ => ‘topics’, ‘post_type’ => array(‘post’), … Read more

Custom permalink structure only for default posts

The solution is to reregister the default post type just after WordPress, and to add a rewrite slug. Also, the _builtin param needs to be set to false. add_action( ‘init’, ‘my_new_default_post_type’, 1 ); function my_new_default_post_type() { register_post_type( ‘post’, array( ‘labels’ => array( ‘name_admin_bar’ => _x( ‘Post’, ‘add new on admin bar’ ), ), ‘public’ => … Read more

How do I get the category URL from get_the_category?

Use: get_category_link( $category_id ); See: https://codex.wordpress.org/Function_Reference/get_category_link In your specific case: <?php global $post; $categories = get_the_category(); foreach ($categories as $category) : $exclude = get_the_ID(); $posts = get_posts(‘posts_per_page=4&category=’. $category->term_id); foreach($posts as $post) : if( $exclude != get_the_ID() ) { ?> <a href=”https://wordpress.stackexchange.com/questions/219954/<?php the_permalink(); ?>” title=”<?php the_title(); ?>” class=”post c-1″> Link to actual post</a> <?php } endforeach; … Read more

Template for specific post of custom post type

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->post_type === ‘event’ ) { … Read more

Using single.php from plugin folder instead of default template folder

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”; } return $template; }

Custom Single Post By Category

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 to show in all other cases. For more … Read more

Add_action to wp_head via functions.php

The reason the code posted is not working is that $post is not referencing the global $post variable, which is the goal here. Using get_the_ID() is a good way of accessing the ID associated with the current post. That’s how I’d suggest doing it, but there are other ways too: add_action ( ‘wp_head’, ‘hook_inHeader’ ); … Read more