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 slug with additional fields

Copy Code
1import { categories } from 'wix-blog-backend';
2
3/* Sample slug value:
4 * 'my-category'
5 * Sample options value:
6 * {
7 * fieldsets: [
8 * 'URL',
9 * 'SEO'
10 * ]
11 * }
12 */
13
14export async function getCategoryBySlugFunction(slug, options) {
15 try {
16 const result = await categories.getCategoryBySlug(slug, options);
17 const label = result.category.label;
18 const slugId = result.category._id;
19 const slugUrl = result.category.url;
20 console.log('Retrieved Result:', result);
21 return result;
22 } catch (error) {
23 console.error(error);
24 }
25}
26
27/* Promise resolves to:
28 * {
29 * "category": {
30 * "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",
31 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
32 * "description": "my category description",
33 * "displayPosition": 0,
34 * "label": "My Category",
35 * "language": "en",
36 * "postCount": 1,
37 * "seoData": {
38 * "tags": [
39 * {
40 * "type": "meta",
41 * "props": {
42 * "name": "description",
43 * "content": "this is a category description"
44 * },
45 * "children": "",
46 * "custom": false,
47 * "disabled": false
48 * }
49 * ]
50 * },
51 * "slug": "my-category",
52 * "title": "My Title",
53 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e",
54 * "url" : "http://https://tadasz7.wixsite.com/blog-velo-events/my-blog/categories/my-category"
55 * }
56 * }
57 */
58