Search...
getCategory( )
Gets a category by the specified ID.
Description
The getCategory()
function returns a Promise that resolves to a category whose ID matches the specified ID.
Syntax
function getCategory(categoryId: string, options: GetCategoryOptions): Promise<GetCategoryResponse>
getCategory Parameters
NAME
TYPE
DESCRIPTION
categoryId
string
Category ID.
options
Optional
GetCategoryOptions
Options specifying which fields to return.
Returns
Return Type:
Promise<
GetCategoryResponse
>NAME
TYPE
DESCRIPTION
category
Category
Category info.
Was this helpful?
Get a category by ID
Copy Code
1import { categories } from 'wix-blog-backend';23/* Sample categoryId value:4 * 'f489bf39-3297-4854-8429-e19dbefdca0e'5 */67export async function getCategoryFunction(categoryId) {8 try {9 const result = await categories.getCategory(categoryId);10 const label = result.category.label;11 const slug = result.category.slug;12 console.log('Retrieved Result:', result);13 return result;14 } catch (error) {15 console.error(error);16 }17}1819/* Promise resolves to:20 * {21 * "category": {22 * "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",23 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",24 * "description": "my category description",25 * "displayPosition": 0,26 * "label": "My Category",27 * "language": "en",28 * "postCount": 1,29 * "slug": "my-category",30 * "title": "My Category",31 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e"32 * }33 * }34 */35
Get a category by ID (export from backend code)
Copy Code
1import { Permissions, webMethod } from 'wix-web-module';2import { categories } from 'wix-blog-backend';34/* Sample categoryId value:5 * 'f489bf39-3297-4854-8429-e19dbefdca0e'6 */78export const getCategoryFunction = webMethod(Permissions.Anyone, async (categoryId) => {9 try {10 const result = await categories.getCategory(categoryId);11 const label = result.category.label;12 const slug = result.category.slug;13 console.log('Retrieved Result:', result);14 return result;15 } catch (error) {16 console.error(error);17 }18});1920/* Promise resolves to:21 * {22 * "category": {23 * "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",24 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",25 * "description": "my category description",26 * "displayPosition": 0,27 * "label": "My Category",28 * "language": "en",29 * "postCount": 1,30 * "slug": "my-category",31 * "title": "My Category",32 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e"33 * }34 * }35 */
Get a category by ID with additional fields
Copy Code
1import { Permissions, webMethod } from 'wix-web-module';2import { categories } from 'wix-blog-backend';34/* Sample categoryId value:5 * 'f489bf39-3297-4854-8429-e19dbefdca0e'6 *7 * Sample options value:8 * {9 * fieldsets: [10 * 'URL',11 * 'SEO'12 * ]13 * }14 */1516export const getCategoryFunction = webMethod(Permissions.Anyone, async (categoryId, options) => {17 try {18 const result = await categories.getCategory(categoryId, options);19 const label = result.category.label;20 const url = result.category.url;21 console.log('Retrieved Result:', result);22 return result;23 } catch (error) {24 console.error(error);25 }26});2728/* Promise resolves to:29 * {30 * "category": {31 * "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",32 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",33 * "description": "my category description",34 * "displayPosition": 0,35 * "label": "My Category",36 * "language": "en",37 * "postCount": 1,38 * "seoData": {39 * "tags": [40 * {41 * "type": "meta",42 * "props": {43 * "name": "description",44 * "content": "this is a category description"45 * },46 * "children": "",47 * "custom": false,48 * "disabled": false49 * }50 * ]51 * },52 * "slug": "my-category",53 * "title": "My Category",54 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e",55 * "url" : "http://https://tadasz7.wixsite.com/blog-velo-events/my-blog/categories/my-category"56 * }57 * }58 */