Selamat siang para suhu, mau tanya nih…

Selamat siang para suhu, mau tanya nih
sya mau ambil data post berdasarkan category, tpi dsitu utk category hnya d’sediakan id sedangkan sya pngennya berdasarkan slug. Bisa atau tdk yaah?? jika bisa gmna caranya??


Source

4 comments

  1. Bisa pake conditional tag is_category ato in_category, Googling aja wordpress conditional tag. Di situ lengkap by id slug dll.

  2. Pake ini.. nanti Ada tambahan categories_names .

    function wpid_register_categories_names_field()
    {
    register_rest_field(
    array(‘post’),
    ‘categories_names’,
    array(
    ‘get_callback’ => ‘wpid_get_categories_names’,
    ‘update_callback’ => null,
    ‘schema’ => null,
    )
    );
    }

    add_action(‘rest_api_init’, ‘wpid_register_categories_names_field’);

    function wpid_get_categories_names($object, $field_name, $request)
    {
    $formatted_categories = array();

    $categories = get_the_category($object[‘id’]);

    foreach ($categories as $category) {
    $formatted_categories[] = $category->name;
    }

    return $formatted_categories;
    }

Leave a comment

Your email address will not be published.