Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
How do i use custom post types in separate JS file?

I have made a custom Wordpress post type recepies and I'm trying to use them in a separate JS file but can't find an easy way to access them? (I'm also using JQuery if that makes it easier?)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can access the data of your custom post types in a javascript file using Rest API and jQuery.

For example, you want to display the ID of your recipe in a div with the class of "the_id" and the title of the recipe in a div with the class of "the_title". Moreover I'm assuming your post type has the name of "recipes" and we just want to get one recipe.

$(document).ready(function() {
    $.ajax({
        url: "http://yoururl.com/wp-json/wp/v2/recipes?per_page=1"
    }).then(function(data) {
       $('.the_id').append(data[0].id);
       $('.the_title').append(data[0].title.rendered);
    });
});

You can reduce the number of posts you are getting with the "per_page" parameter.

In data you can now access everything the Rest API gives you, in the example the ID (via data.id) and the title (via data.title.rendered).

This might interest you: https://developer.wordpress.org/rest-api/reference/

Here the fields you can access by default: https://developer.wordpress.org/rest-api/reference/posts/

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda