Hot answers tagged

16

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


14

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() &&...


12

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' ) ){ // do ...


12

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


12

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 ...


11

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.


11

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 ...


11

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([ ...


9

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.


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 ...


7

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" (...


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 }


6

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'; }...


6

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, '...


6

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-...


5

Just check for the global $numpages variable: <?php global $numpages; if ( is_singular() && $numpages > 1 ) { // This is a single post // and has more than one page; // Do something. } ?>


5

get_ancestors() should do what you need: So, you should do something like this: $ancestors = get_ancestors($term_id, 'projects_category)


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

I used this little trick to obtain the taxonomy image given the ID of the term: <?php $images = get_option('taxonomy_image_plugin'); $img_url = wp_get_attachment_url( $images[$term_id] ); ?>


5

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 ...


5

You are using an incorrect check here. is_singular() returns true when a post is from the specified post type or post types or the default post types when none is specified. You cannot target specific single posts with is_singular() You have to use is_single to target a specific post if ( is_single( 'post-a' ) { // Do something for post-a } elseif ( ...


4

is_single() returns TRUE or FALSE, not a string. Additionally, you can test for a specific post with is_single() function by putting the post slug into the function call: if ( is_single( 'your-post-slug' ) ) { # do something } If you want to test for the proper post type use: if ( is_singular() and 'your-post-type' === get_post_type() ) or just: ...


4

Add this to your functions.php: /* Flush rewrite rules for custom post types. */ add_action( 'after_switch_theme', 'bt_flush_rewrite_rules' ); /* Flush your rewrite rules */ function bt_flush_rewrite_rules() { flush_rewrite_rules(); } Then change your theme to a different theme and then set it back again to your custom theme. This will flush the ...


4

The very broad answer for why this doesn't work is that the internet is stateless. Basically each request for a page is a separate and unique instance from other page requests. Example Let's say post-1 is in category-1 and category-2. When Wordpress loads post-1 on single.php it has no way of knowing how the user arrived at the page. It only knows that ...


4

As the WordPress Codex for have_posts points out: As a side effect, have_posts starts, steps through, or resets The Loop. At the end of the loop, have_posts returns 0 after calling rewind_posts. Looking at the source as it stands today: along with calling rewind_posts() it also fires the loop_end action (which plugins may rely upon) as well as set a ...


4

If I understand well you want to show the last post (one post) from one of the 3 post types you have, using a dynamic url like http://example.com/latest. First of all lets add a filter to 'do_parse_request' filter: add_filter( 'do_parse_request', function( $bool, WP $wp ) { $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); $path = substr(...


4

next_posts_link and previous_posts_link (plural) are for archive pagination. For single posts you want next_post_link and previous_post_link (singular).


4

EDIT NOTE If you need only the parent and first level child terms, the answer from @Roberthue should work great. If you need all level child terms, then my solution should work) I have updated my code to be more effecient. Run get_ancestors() only if we cannot find the parent term or its direct children Stop the execution of the foreach loop ...


4

get_post_format and get_post_type are completely different. Post Formats can be one of the following: 'standard' (default one) 'aside' 'chat' 'gallery' 'link' 'image' 'quote' 'status' 'video' 'audio' And shopping is the post type you have created and not post format. You can add post format for the post type(shopping) like this add_post_type_support( '...



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