0
votes

I'm working on a sage template on bedrock boilerplate, which uses laravel blade and controller. I'm getting posts data from WordPress API and displaying it by category. This gets done on controller. I want to pass a parameter on the blade template so controller knows which category to retrieve. Here is the code.

public function getposts3($category = 2) {
        
        $args = array(
            'orderby' => 'title',
            'per_page' => 10,
            'categories' => $category,
            );
         
          $url = add_query_arg( $args, 'https://localhost/bedrock/web/index.php/wp-json/wp/v2/posts');
          
          $stuff = wp_remote_get($url, array('sslverify' => FALSE));
          
          $body = wp_remote_retrieve_body($stuff);
          
          $posts = json_decode($body);

          return $posts;
    }
@foreach ($getposts3 as $post)
  <div class="w-1/2  bg-purple-light rounded m-auto mb-5">
    <h1>{{$post->title->rendered}}</h1>
    <div>{!!$post->content->rendered!!}</div>
  </div>
@endforeach

I come from JavaScript so I tried doing $getposts3(3) to pass the parameter but it doesn't work. What is the correct way to do this?

Also I want to put a button that will change the parameter and it should call the function again and reload all the posts from the new category. How can I achieve this?

1

1 Answers

0
votes

Create a route for getposts3

function getposts3(Request $request) {
 // curl to wordpress api
 // you can get category id that client choosen from $request
 return response()->json($curlResponse);
}

Then from client side you have to make an api call to getposts3 and append it to html body