-
Notifications
You must be signed in to change notification settings - Fork 0
/
mka_display_post_format_link_shortcode.php
70 lines (63 loc) · 1.65 KB
/
mka_display_post_format_link_shortcode.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
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Plugin Name: Monkey Kode - Post Format Link Shortcode
* Version:
* Description: Display a list of Post Format Link Posts grabbing the link frlom the content.
* Author: Jull Weber
* Author URI: https://monkeykode.com/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: mka_display_post_format_link_shortcode
* Domain Path: /languages
*
* @package MKA
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
if ( ! function_exists( 'mka_display_post_format_link_shortcode' ) ) {
/**
* Display the html shortcode.
*/
function mka_display_post_format_link_shortcode() {
/**
* Generate shortcode.
*
* @param object $atts attributes passed.
*
* @return string
*/
function mka_add_post_format_links( $atts ) {
// Attributes.
$atts = shortcode_atts(
array(
'catname' => '',
'number' => - 1,
),
$atts
);
ob_start();
$args_links = array(
'category_name' => $atts['catname'],
'posts_per_page' => $atts['number'],
);
$links_query = new WP_Query( $args_links );
if ( $links_query->have_posts() ) {
echo '<ul>';
while ( $links_query->have_posts() ) {
global $post;
$links_query->the_post();
preg_match_all( '/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $links );
echo '<li>';
echo '<a href="' . esc_url( $links[1][0] ) . '" target="_blank">';
the_title();
echo '</li>';
echo '</a>';
}
echo '</ul>';
}
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'add_links', 'mka_add_post_format_links' );
}
mka_display_post_format_link_shortcode();
}