Skip to content

Commit

Permalink
v1.1.3
Browse files Browse the repository at this point in the history
* Added `@hasposts` and `@endhasposts` directives.
* Added `@noposts` and `@endnoposts` directives. (#18)
  • Loading branch information
Log1x committed Aug 25, 2019
1 parent 501265a commit 7faa87e
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 130 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v1.1.3 (08-25-2019)

### Enhancements
- Added `@hasposts` and `@endhasposts` directives.
- Added `@noposts` and `@endnoposts` directives. (#18)

## v1.1.2 (08-07-2019)

### Bug fixes
Expand Down
26 changes: 26 additions & 0 deletions docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ When passing an array of ID's / objects, the posts will be sorted by the order o

If `@query` is not used and an argument is not passed to `@posts`, it will use the main loop from the `$wp_query` global.

## @hasposts

`@hasposts` accepts the same exact arguments as `@posts`, but simply runs a conditional without the while loop. It can be closed using `@endhasposts`.

```php
@hasposts
<ul>
@posts
<li>@title</li>
@endposts
</ul>
@endhasposts
```

## @noposts

`@noposts` again has the exact same arguments as `@posts`, but runs a `!` conditional without the while loop. It can be closed using `@endnoposts`.

```php
@noposts
<div class="py-2 px-4 bg-red-500 text-white">
🏜️
</div>
@endnoposts
```

## @title

`@title` echo's the current posts title using [`get_the_title()`](https://developer.wordpress.org/reference/functions/get_the_title/).
Expand Down
78 changes: 78 additions & 0 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,84 @@
return "<?php endwhile; wp_reset_postdata(); endif; ?>";
},

/*
|---------------------------------------------------------------------
| @hasposts / @endhasposts / @noposts / @endnoposts
|---------------------------------------------------------------------
*/

'hasposts' => function ($expression) {
if (! empty($expression)) {
return "<?php \$posts = collect(); ?>" .

"<?php if (is_a({$expression}, 'WP_Post') || is_numeric({$expression})) : ?>" .
"<?php \$posts->put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>" .
"<?php endif; ?>" .

"<?php if (is_array({$expression})) : ?>" .
"<?php \$posts
->put('ignore_sticky_posts', true)
->put('posts_per_page', -1)
->put('post__in', collect({$expression})
->map(function (\$post) {
return is_a(\$post, 'WP_Post') ? \$post->ID : \$post;
})->all())
->put('orderby', 'post__in');
?>" .
"<?php endif; ?>" .

"<?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>" .
"<?php if (\$query->have_posts()) :";
}

return "<?php if (empty(\$query)) : ?>" .
"<?php global \$wp_query; ?>" .
"<?php \$query = \$wp_query; ?>" .
"<?php endif; ?>" .

"<?php if (\$query->have_posts()) : ?>";
},

'endhasposts' => function () {
return "<?php wp_reset_postdata(); endif; ?>";
},

'noposts' => function ($expression) {
if (! empty($expression)) {
return "<?php \$posts = collect(); ?>" .

"<?php if (is_a({$expression}, 'WP_Post') || is_numeric({$expression})) : ?>" .
"<?php \$posts->put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>" .
"<?php endif; ?>" .

"<?php if (is_array({$expression})) : ?>" .
"<?php \$posts
->put('ignore_sticky_posts', true)
->put('posts_per_page', -1)
->put('post__in', collect({$expression})
->map(function (\$post) {
return is_a(\$post, 'WP_Post') ? \$post->ID : \$post;
})->all())
->put('orderby', 'post__in');
?>" .
"<?php endif; ?>" .

"<?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>" .
"<?php if (! \$query->have_posts()) :";
}

return "<?php if (empty(\$query)) : ?>" .
"<?php global \$wp_query; ?>" .
"<?php \$query = \$wp_query; ?>" .
"<?php endif; ?>" .

"<?php if (! \$query->have_posts()) : ?>";
},

'endnoposts' => function () {
return "<?php wp_reset_postdata(); endif; ?>";
},

/*
|---------------------------------------------------------------------
| @title / @content / @excerpt / @permalink / @thumbnail
Expand Down
Loading

0 comments on commit 7faa87e

Please sign in to comment.