commit
This commit is contained in:
37
includes/shortcode.php
Normal file
37
includes/shortcode.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
// 注册短代码
|
||||
add_shortcode('friend_links', 'flm_display_friend_links');
|
||||
function flm_display_friend_links($atts) {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'friend_links';
|
||||
|
||||
$atts = shortcode_atts(array(
|
||||
'random' => 'true'
|
||||
), $atts);
|
||||
|
||||
$order_by = ($atts['random'] === 'true') ? 'RAND()' : 'sort_order ASC';
|
||||
$links = $wpdb->get_results("SELECT * FROM $table_name ORDER BY $order_by");
|
||||
|
||||
if (empty($links)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<div class="flm-links-container">
|
||||
<?php foreach ($links as $link): ?>
|
||||
<div class="flm-link-card">
|
||||
<a href="<?php echo esc_url($link->url); ?>" target="_blank" rel="noopener noreferrer">
|
||||
<div class="flm-link-icon-container">
|
||||
<img src="<?php echo $link->icon ?: FLM_PLUGIN_URL . 'assets/images/default-icon.png'; ?>"
|
||||
alt="<?php echo esc_attr($link->name); ?>"
|
||||
class="flm-link-icon">
|
||||
</div>
|
||||
<div class="flm-link-name"><?php echo esc_html($link->name); ?></div>
|
||||
</a>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php
|
||||
return ob_get_clean();
|
||||
}
|
Reference in New Issue
Block a user