Created
June 21, 2024 20:26
-
-
Save hxueh/f3caae66d4913ca6bbf3188ee6482967 to your computer and use it in GitHub Desktop.
A catalog generator for WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Catelog | |
Description: Generates a dynamic page showing categories and their posts | |
Version: 0.1 | |
Author: hxueh | |
Author URI: https://www.hxueh.net | |
*/ | |
// Prevent direct access to the plugin file | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
function catelog() | |
{ | |
$output = '<ul>'; | |
// Get all categories, ordered alphabetically | |
$categories = get_categories( | |
array( | |
'orderby' => 'name', | |
'order' => 'ASC' | |
) | |
); | |
foreach ($categories as $category) { | |
// add category url to output | |
$output .= '<li><strong>Category:</strong> <a href="' . esc_url(get_category_link($category->term_id)) . '">' . esc_html($category->name) . '</a>'; | |
// Get posts for this category, ordered by creation time | |
$posts = get_posts( | |
array( | |
'category' => $category->term_id, | |
'orderby' => 'date', | |
'order' => 'DESC', | |
'numberposts' => -1, | |
) | |
); | |
if ($posts) { | |
$output .= '<ul>'; | |
foreach ($posts as $post) { | |
$post_date = get_the_date('F j, Y', $post->ID); | |
$output .= '<li>'; | |
$output .= '<a href="' . esc_url(get_permalink($post->ID)) . '">' . esc_html($post->post_title) . '</a>'; | |
$output .= ' - <span class="post-date">' . esc_html($post_date) . '</span>'; | |
$output .= '</li>'; | |
} | |
$output .= '</ul>'; | |
} | |
$output .= '</li>'; | |
} | |
$output .= '</ul>'; | |
return $output; | |
} | |
add_shortcode('catelog', 'catelog'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment