Search.../

getCategoryBySlug( )

Gets a category by the specified slug.

Description

The getCategoryBySlug() function returns a Promise that resolves to a category whose slug matches the specified slug.

The slug is the end of a category's URL that refers to a specific category. For example, if a category's URL is https://example.com/blog/category/{my-category-slug}, the slug is my-post-slug. The slug is case-sensitive string that is generally derived from the category's label, unless specified otherwise.

Syntax

function getCategoryBySlug(slug: string, options: GetCategoryBySlugOptions): Promise<GetCategoryBySlugResponse>

getCategoryBySlug Parameters

NAME
TYPE
DESCRIPTION
slug
string

Slug of the category to retrieve.

The end of a category's URL. For example, 'https:/example.com/blog/category/{my-category-slug}'. Case sensitive and generally based on the category label if not specified.

options
Optional
GetCategoryBySlugOptions

Options specifying which fields to return.

Returns

Return Type:

Promise<
GetCategoryBySlugResponse
>
NAME
TYPE
DESCRIPTION
category
Category

Category info.

Was this helpful?

Get a category by its slug

Copy Code
1import { categories } from 'wix-blog-backend';
2
3/* Sample slug value:
4 * 'my-category'
5 */
6
7export async function getCategoryBySlugFunction(slug) {
8 try {
9 const result = await categories.getCategoryBySlug(slug);
10 const label = result.category.label;
11 const slugId = result.category._id;
12 console.log('Retrieved Result:', result);
13 return result;
14 } catch (error) {
15 console.error(error);
16 }
17}
18
19/* 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 its slug (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { categories } from 'wix-blog-backend';
3
4/* Sample slug value:
5 * 'my-category'
6 */
7
8export const getCategoryBySlugFunction = webMethod(Permissions.Anyone, async (slug) => {
9 try {
10 const result = await categories.getCategoryBySlug(slug);
11 const label = result.category.label;
12 const slugId = result.category._id;
13 console.log('Retrieved Result:', result);
14 return result;
15 } catch (error) {
16 console.error(error);
17 }
18});
19
20/* 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 slug with additional fields

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { categories } from 'wix-blog-backend';
3
4/* Sample slug value:
5 * 'my-category'
6 * Sample options value:
7 * {
8 * fieldsets: [
9 * 'URL',
10 * 'SEO'
11 * ]
12 * }
13 */
14
15export const getCategoryBySlugFunction = webMethod(Permissions.Anyone, async (slug, options) => {
16 try {
17 const result = await categories.getCategoryBySlug(slug, options);
18 const label = result.category.label;
19 const slugId = result.category._id;
20 const slugUrl = result.category.url;
21 console.log('Retrieved Result:', result);
22 return result;
23 } catch (error) {
24 console.error(error);
25 }
26});
27
28/* 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": false
49 * }
50 * ]
51 * },
52 * "slug": "my-category",
53 * "title": "My Title",
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 */