最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

json - How do I use URL to fetch all posts of a particular custom post type?

matteradmin9PV0评论

I'm trying to be able to fetch all the posts of a particular post type on my website. The post type should be called 'opportunities'. I'm new to wordpress so a bit out of my depth. The posts in question actually have their on button on the admin sidebar so they could be something entirely different from a 'custom_post_type'. Any help I could get on this would be much appreciated.

*edit: These are the notes I got from a developer I'm trying to help:

"To access the database with opportunities from the UK office we need them to make these available through an API. This can be done by their developer by changing a few lines in their code. It’s explained at: / after making the changes I need to know the URL where I can retrieve the opportunities through JSON."

I'm not a developer, I'm more or less just the guy that got asked to help with website in our office.

I'm trying to be able to fetch all the posts of a particular post type on my website. The post type should be called 'opportunities'. I'm new to wordpress so a bit out of my depth. The posts in question actually have their on button on the admin sidebar so they could be something entirely different from a 'custom_post_type'. Any help I could get on this would be much appreciated.

*edit: These are the notes I got from a developer I'm trying to help:

"To access the database with opportunities from the UK office we need them to make these available through an API. This can be done by their developer by changing a few lines in their code. It’s explained at: https://developer.wordpress/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/ after making the changes I need to know the URL where I can retrieve the opportunities through JSON."

I'm not a developer, I'm more or less just the guy that got asked to help with website in our office.

Share Improve this question edited Oct 29, 2018 at 13:56 Luke asked Oct 26, 2018 at 13:43 LukeLuke 11 bronze badge 10
  • Your question is not clear. Can you please elaborate more. So, We'll help you. – maheshwaghmare Commented Oct 26, 2018 at 13:47
  • These are the notes I got from a developer I'm trying to help: "To access the database with opportunities from the UK office we need them to make these available through an API. This can be done by their developer by changing a few lines in their code. It’s explained at: developer.wordpress/rest-api/extending-the-rest-api/… after making the changes I need to know the URL where I can retrieve the opportunities through JSON." – Luke Commented Oct 26, 2018 at 13:59
  • Do you have set the 'show_in_rest' => true and 'rest_controller_class' => 'WP_REST_Posts_Controller' parameters while registering your post type opportunities? If yes the you can access them with rest api endpoint like: example/wp-json/wp/v2/opportunities – maheshwaghmare Commented Oct 26, 2018 at 14:06
  • Like default post rest api endpoint - yoursite/wp-json/wp/v2/posts. Change http://yoursite with your domain and try yourself. It'll works! If not then share your website URL for reference. – maheshwaghmare Commented Oct 26, 2018 at 14:07
  • Thanks for your quick answers. Where would I find that in the code to make sure it's set to true? I have put it in the theme functions but not sure if that was right place. – Luke Commented Oct 26, 2018 at 14:17
 |  Show 5 more comments

1 Answer 1

Reset to default 0

I think what you mean is do an onclick event.

So, you can do this with JavaScript. Create your link as per usual <a id=“myCustomLink” href=“#” onclick=“MyFunction();return false;”>huh! I’m a link!</a>

And now some script

<script>
function MyFunction() {

//Make it do whatever here! For example:

document.getElementById("demo").innerHTML = "Hello World";
}
</script>

Rerun false; will prevent the browser directing to the link... unless you want it to? If so then just remove this and add a link in-place of the # after the href.

However! A much cleaner way of doing this would be to do it as JQuery and attach the ID as an onclick handler. Like so:

$(‘#myLink’).click(function(){ MyFunction(); return false; });

Hope this helps!

Post a comment

comment list (0)

  1. No comments so far