I'm working on a system that forces users (unfamiliar with wp) to choose a category before creating a new post, and then adds some appropriate html layout styling to help them.
The following code works if they choose category "x". How can it be adapted to cope with a couple of alternative category situations?
Human Code Required:
- If the new post is category X: add this html.
- If the new post is category Y: add this html instead.
- If the new post is any other category: add this html.
Existing Code - Inserts some html to new posts if the category is "X":
// This function will only be called when creating an empty post,
// via `get_default_post_to_edit()`, called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
wp_set_post_categories( $post_id, $_REQUEST['category_id'] );
}
add_filter( 'default_content', 'wpa70073_default_products_content' );
function wpa70073_default_products_content( $content ) {
// change this to your desired category ID
$products_category_id = 4;
if(
isset( $_REQUEST['category_id'][0] )
&& $products_category_id == $_REQUEST['category_id'][0]
)
return 'some html styling';
}
UPDATE: Tried adding this to the end, but it didn't work...
// change this to your desired category ID
$products_category_id = 4;................
if(
isset( $_REQUEST['category_id'][0] )
&& $products_category_id == $_REQUEST['category_id'][0]
)
return 'Some html styling';
if ( $products_category_id = '6' ) {
return 'Some alternative html styling';
} else {
return 'Some default html styling';
}
}
Also tried editing the code along the lines of the cpt thread suggested below, but again no luck :(
add_filter( 'default_content', 'wpa70073_default_products_content' );
function wpa70073_default_products_content( $content ) {
switch($content->category){
// change this to your desired category ID
case'4':
$content='cat 4 content';
break;
case'6':
$content='news content';
break;
default:
$content='default content';
break;
}
return $content;
}
