Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

326
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda