-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpcn-previous-next-post-block.php
More file actions
153 lines (119 loc) · 3 KB
/
Copy pathwpcn-previous-next-post-block.php
File metadata and controls
153 lines (119 loc) · 3 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Plugin Name: WPConstructor Previous Next Post Block
* Plugin URI: https://wpconstructor.com/plugins/wpconstructor-previous-next-post-block/
* Description: WPConstructor Previous Next Post Block adds a previous and next post navigation block with featured images and post titles for WordPress.
* Version: 0.1.0
* Requires at least: 6.0
* Requires PHP: 7.6
* Author: WPConstructor
* Author URI: https://wpconstructor.com
* License: GPL-3.0-or-later http://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: wpcn-previous-next-post-block
* Domain Path: /languages
*
* @package WPConstructor\PreviousNextPostBlock
*/
namespace WPConstructor\PreviousNextPostBlock;
if ( ! defined( 'WPINC' ) ) {
die();
}
define( 'WPCN_PREVIOUS_NEXT_POST_BLOCK_VERSION', '0.1.0' );
define( 'WPCN_PREVIOUS_NEXT_POST_BLOCK_FILE', __FILE__ );
define( 'WPCN_PREVIOUS_NEXT_POST_BLOCK_DIR', plugin_dir_path( __FILE__ ) );
define( 'WPCN_PREVIOUS_NEXT_POST_BLOCK_URL', plugin_dir_url( __FILE__ ) );
/**
* Register block.
*/
add_action(
'init',
function () {
wp_register_script(
'wpcn-previous-next-post-block-editor',
WPCN_PREVIOUS_NEXT_POST_BLOCK_URL . 'assets/js/editor.js',
array(
'wp-blocks',
'wp-element',
'wp-i18n',
),
WPCN_PREVIOUS_NEXT_POST_BLOCK_VERSION,
true
);
register_block_type(
'wpcn/previous-next-post',
array(
'editor_script' => 'wpcn-previous-next-post-block-editor',
'render_callback' => __NAMESPACE__ . '\\render_previous_next_post_block',
)
);
}
);
/**
* Render block output.
*
* @return string
*/
function render_previous_next_post_block() {
wp_enqueue_style(
'wpcn-previous-next-post-block'
);
$prev_post = get_previous_post();
$next_post = get_next_post();
if ( ! $prev_post && ! $next_post ) {
return '';
}
ob_start();
?>
<nav class="wpcn-post-navigation-images">
<?php if ( $prev_post ) : ?>
<a href="<?php echo esc_url( get_permalink( $prev_post ) ); ?>" class="wpcn-post-nav-image prev">
<h3>< Previous Post</h3>
<?php
echo get_the_post_thumbnail(
$prev_post,
'large',
array(
'class' => 'wpcn-feature-image',
)
);
?>
<span class="wpcn-post-nav-title">
<?php echo esc_html( get_the_title( $prev_post ) ); ?>
</span>
</a>
<?php endif; ?>
<?php if ( $next_post ) : ?>
<a href="<?php echo esc_url( get_permalink( $next_post ) ); ?>" class="wpcn-post-nav-image next">
<h3>Next Post ></h3>
<?php
echo get_the_post_thumbnail(
$next_post,
'large',
array(
'class' => 'wpcn-feature-image',
)
);
?>
<span class="wpcn-post-nav-title">
<?php echo esc_html( get_the_title( $next_post ) ); ?>
</span>
</a>
<?php endif; ?>
</nav>
<?php
return ob_get_clean();
}
/**
* Register block styles.
*/
add_action(
'init',
function () {
wp_register_style(
'wpcn-previous-next-post-block',
WPCN_PREVIOUS_NEXT_POST_BLOCK_URL . 'assets/css/style.css',
array(),
WPCN_PREVIOUS_NEXT_POST_BLOCK_VERSION
);
}
);