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
wpIdis set, and it will fire a new request every time we change it.
-
[wpModel]desired endpoint, read more Endpoints chapter -
[wpId]item's ID -
[wpArgs]query arguments, read more Query arguments chapter -
(wpResponse)response,$event": {data, pagination, error}, read more Response chapter -
(wpLoading)loading state,$event: boolean
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
To get a single post by slug use the collection directive, check this example