Model directive - MurhafSousli/ngx-wordpress GitHub Wiki

Use this directive to get a single item (posts, pages, categories...) by ID

<div wpModel="posts" [wpId]="14" (wpResponse)="post = $event.data">

  {{post?.title?.rendered}}

</div>

We just got a single post by ID

The model directive fires the request when wpId is set, and it will fire a new request every time we change it.


Model Directive Options:


Advanced Example:

Get a single post by ID with a loading indicator

<div wpModel="posts" [wpId]="3128" [wpArgs]="args"
    (wpResponse)="onWpResponse($event)" (wpLoading)="loading = $event">

  <div *ngIf="post">
     {{post.title.rendered}}
  </div>

  <div *ngIf="loading">Loading...</div>
</div>
 post;
 args = {
    _embed: true
 };

 /** Handle WP Response */
 onWpResponse(res) {
    if (res) {
      if (res.error) {
        console.log('[WP post response]:', res.error);
      }
      if (!res.data) {
        console.log('[WP post response]: Empty response!');
      } else {
        this.post = res.data;
      }
    }
 }

Once you get this, check WpPost Chapter


NOTE

To get a single post by slug use the collection directive, check this example

⚠️ **GitHub.com Fallback** ⚠️