Wordpress Custom Post Type Template - wordpress

I am working on a wordpress website. I need to add custom Post types. I have created a custom post type products which has a taxonomy product_type which is similar to category . There are various taxonomy value for product_type . Some of them are flowers, extracts etc .
Now I am trying to visit this link http://farma.mechadigital.net/products/product_type/flowers/ and it doesnt work for me.
I have added some files.
archive-products => This should be the custom post template
taxonomy-product_type.php => This should be the taxonomy Template
taxonomy-product_type-flowers.php => This should be the template for the term value flowers
Here is the code that I have included in functions.php. I dont know where I am doing it wrong.
functions.php
function farma_products() {
$labels = array(
// List of arguments
);
$args = array(
// list of arguments
'rewrite' => array( 'slug' => 'products' ),
);
register_post_type( 'products', $args );
flush_rewrite_rules(false);
}
add_action( 'init', 'farma_products_type' );
function farma_products_type() {
register_taxonomy(
'product_type',
'products',
array(
'label' => __( 'Product Type' ),
'rewrite' => array( 'slug' => 'products/product_type' ),
'hierarchical' => true,
)
);
}

Related

rename cpt in wordpress

Hello i have one cpt which is case studies but i want to rename it as portfolio .and i have created 10 post under case studies cpt .. so can i rename it cpt ? actually i want change in url .. currently my url is http://www.praxinfo.com/case-studies-page/whatscrackin/ but i want http://www.praxinfo.com/portfolio/whatscrackin/. so how to do this in wordpress?
In your register_post_type use the rewrite option to change the url but not the actual name of the cpt. This way, you won't run into database problems and your users will still see the new name.
Example from the WordPress codex:
add_action( 'init', 'create_posttype' );
function create_posttype() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'products'),
)
);
}
Here, the url would be /products/ even though the name of the cpt is acme_product.

Category for only one CPT

I have created CPT and added a taxonomy category.
The problem is that those categories from CPT appear also in my blog.
I would like to have diffrent categories in my blog and diffrent in my CPT portfolio.
Here is my current code
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'portfolio',
array(
'labels' => array(
'label' => 'portfolio',
'name' => __( 'Portfolio' ),
'singular_name' => __( 'portfolio' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array('category','post_tag'),
'supports' => array( 'title', 'comments', 'excerpt', 'custom-fields', 'thumbnail' )
)
);
};
Quoting the codex,
"Even if you register a taxonomy while creating the post type, you
must still explicitly register and define the taxonomy using
register_taxonomy().
Basically, you're creating a new taxonomy called category for your Portfolio CPT. This will function exclusive of the default category taxonomy of the Posts.

How to register an attribute to Woocommerce programmatically

I'm not talking about adding an attribute to a product but I want to add an attribute itself..
You can do that from the UI under Products -> Attributes but from the code how is it done (e.i: what functions to call or tables to update)?
An attribute is merely a custom taxonomy, so I think you should be able to do this the same as you'd register any custom taxonomy. WooCommerce prefaces their attributes taxonomy names with 'pa_' (probably standing for product attibute)
add_action( 'init', 'create_product_attribute' );
function create_product_attribute() {
register_taxonomy(
'pa_genre',
'product',
array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
)
);
}

Cannot access neither $post nor get_post_type() inside template include filter for my custom post type

I'm trying to apply a template_include filter to inflate a template from my plugin.
I've seen a lot of people using the following with success:
function include_template_files($template_file) {
global $post;
$plugindir = dirname( __FILE__ );
if ('mycustomposttype' == get_post_type()){
$templatefilename = 'mytemplate.php';
$template = $plugindir . '/theme_files/' . $templatefilename;
return $template;
}
return $template_file;
}
add_filter( 'template_include', 'include_template_files' );
get_post_type() returns empty, and $post is not instantiated for my custom post. This works fine for WP types (posts, pages, etc.).
What am I doing wrong?
I'm on WP 3.7.1 and I'm using the default twenty thirteen theme.
UPDATE:
I register my type this way:
function register_mycustom_post_type() {
register_post_type( 'mycustomposttype', array(
'labels' => array(
'name' => 'My posts',
'singular_name' => 'My post',
'menu_name' => 'My posts',
'add_new' => 'New custom post',
'add_new_item' => 'Add new custom post',
),
'public' => true,
'show_ui' => true,
'menu_icon' => plugins_url('myicon.png',__FILE__),
'supports' => array( 'title' ,'thumbnail', 'editor' ),
) );
}
add_action('init','register_mycustom_post_type');
SECOND UPDATE (PARTIAL SOLUTION):
The problem is given by permalinks rewrite. If I use the default URLs (index.php?...) it works fine.
SOLUTION
Adding the right rewrite options to my post type solved the problem:
'rewrite' => array(
'slug' => 'mytype',
'with_front' => false
),
Try using the get_queried_object() function like this
$queried = get_queried_object();
print_r($queried);
You should get and object with information from the current page
I answer myself. The problem was permalink rewrite.
Adding the following to register_post_type options solved the problem:
'rewrite' => array(
'slug' => 'mytype',
'with_front' => false
),
Sometimes this can work as well:
get_query_var( 'post_type' );

WordPress 3 Custom Post Type: How to get comments

I am using 4 Custom Post Types each having this kind of declaration:
register_post_type( 'texts', array(
'labels'=>array('name'=>'Texts','singular_name'=>'Text'),
'public'=>true,
'has_archive'=>true,
'menu_position'=>5
) );
The problem I have is that posts in these pages do not get Comment links, saying comments are closed.
There is a Page created called Texts with a slug of /texts/ which has a custom template for blog posts, but that does enable comments.
How can I get comments to work please?
See the 'supports' parameter here,
http://codex.wordpress.org/Function_Reference/register_post_type
or:
http://codex.wordpress.org/Function_Reference/add_post_type_support
for example:
add_post_type_support('texts','comments');
My example code, copied from my theme's functions.php:
// uses sd_register_post_type, which is from http://somadesign.ca/projects/smarter-custom-post-types/, not necessary anymore with WP3.1
add_action( 'init', 'create_post_types', 0 ); // before sd_register_post_type's actions
function create_post_types(){
$post_supports = array(
'title'
,'editor'
,'author'
,'thumbnail'
,'excerpt'
,'trackbacks'
,'custom-fields'
,'comments'
,'revisions'
);
$post_type_slug = 'my-news'; // max 20 chars!
$post_type_slug_plural = $post_type_slug;
sd_register_post_type( $post_type_slug, array(
'labels' => array(
'name' => 'My News',
'singular_name' => 'My New' // :)
)
,'rewrite' => array(
'slug' => $post_type_slug
,'with_front' => false // don't prepend /blog/...
)
,'public' => true
,'hierarchical' => false
,'supports' => $post_supports
,'menu_position' => 6
,'capability_type' => 'post'
),$post_type_slug_plural);
}
add_action( 'init', 'register_taxonomies_for_custom_post_types', 11 ); // 11=after sd_register_post_type's actions
function register_taxonomies_for_custom_post_types(){
$post_type_slug = 'my-news'; // max 20 chars!
register_taxonomy_for_object_type('category', $post_type_slug);
register_taxonomy_for_object_type('post_tag', $post_type_slug);
}
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies(){
$post_types = array( 'post', /*'page',*/ 'my-news' );
register_taxonomy( 'city', $post_types,
array(
'public' => true
,'labels' => array(
'name' => 'Cities',
'singular_name' => 'City'
)
,'hierarchical' => true
,'rewrite' => array(
'slug' => 'my-news/cities'
,'with_front' => false // don't prepend /blog/...
)
)
);
}

Resources