-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
59 lines (55 loc) · 1.44 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
* Plugin Name: KGR Shortcode
* Plugin URI: https://github.com/constracti/kgr-shortcode
* Description: Provides various shortcodes.
* Version: 0.4
* Requires at least: 4.5.0
* Requires PHP: 8.0
* Author: constracti
* Author URI: https://github.com/constracti
* Licence: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
if ( !defined( 'ABSPATH' ) )
exit;
add_shortcode( 'kgr_shortcode_post', function( array $atts ): string {
$atts = wp_parse_args( $atts, [
'id' => NULL,
'field' => NULL,
'trim' => NULL,
] );
if ( is_null( $atts['id'] ) )
return '';
$post = get_post( $atts['id'] );
if ( is_null( $post ) )
return '';
$ret = match ( $atts['field'] ) {
'permalink' => get_permalink( $post ),
'title' => get_the_title( $post ),
'excerpt' => get_the_excerpt( $post ),
'thumbnail' => get_the_post_thumbnail_url( $post ),
'menu_url' => get_post_meta( $post->ID, '_menu_item_url', TRUE ),
default => '',
};
if ( !is_null( $atts['trim'] ) )
$ret = wp_trim_words( $ret, $atts['trim'] );
return $ret;
} );
add_shortcode( 'kgr_shortcode_term', function( array $atts ): string {
$atts = wp_parse_args( $atts, [
'id' => NULL,
'field' => NULL,
] );
if ( is_null( $atts['id'] ) )
return '';
$term = get_term( $atts['id'] );
if ( is_null( $term ) )
return '';
$ret = match ( $atts['field'] ) {
'permalink' => get_term_link( $term ),
'name' => $term->name,
default => '',
};
return $ret;
} );