Gets a category by the specified slug.
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.
function getCategoryBySlug(
slug: string,
options: GetCategoryBySlugOptions,
): Promise<GetCategoryBySlugResponse>;
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 specifying which fields to return.
import { categories } from "wix-blog-backend";
/* Sample slug value:
* 'my-category'
*/
export async function getCategoryBySlugFunction(slug) {
try {
const result = await categories.getCategoryBySlug(slug);
const label = result.category.label;
const slugId = result.category._id;
console.log("Retrieved Result:", result);
return result;
} catch (error) {
console.error(error);
}
}
/* Promise resolves to:
* {
* "category": {
* "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",
* "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
* "description": "my category description",
* "displayPosition": 0,
* "label": "My Category",
* "language": "en",
* "postCount": 1,
* "slug": "my-category",
* "title": "My Category",
* "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e"
* }
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.