继承的WordPress Twig站点博客列表短代码修改技术问询
videos Category Got it, let's walk through modifying your existing blog_list shortcode to handle the new videos category for both inclusion and exclusion. I'll assume you're using Timber (the most common WordPress Twig integration) since you mentioned a Twig-based site.
Step 1: Locate the Shortcode Definition
First, find where the blog_list shortcode is registered in your theme files—this is usually in functions.php or a dedicated shortcodes.php file. Look for code like this:
add_shortcode('blog_list', 'blog_list_shortcode');
Then find the corresponding callback function (e.g., blog_list_shortcode).
Step 2: Update Parameter Handling & Query Logic
Your existing shortcode already uses category (for inclusion) and category_exclude (for exclusion) parameters. We'll tweak the logic to support single or multiple category slugs (including videos) and ensure robust ID lookup.
Here's the revised callback function:
function blog_list_shortcode($atts) { // Default shortcode attributes $atts = shortcode_atts( array( 'category' => '', 'category_exclude' => '' ), $atts, 'blog_list' ); // Base WP_Query arguments $query_args = array( 'post_type' => 'post', 'posts_per_page' => get_option('posts_per_page'), 'paged' => get_query_var('paged') ?: 1, 'post_status' => 'publish' ); // Handle category inclusion (supports single or comma-separated slugs) if (!empty($atts['category'])) { $query_args['category_name'] = trim($atts['category']); // WordPress automatically handles comma-separated slugs here for multiple categories } // Handle category exclusion (supports single or comma-separated slugs) if (!empty($atts['category_exclude'])) { $exclude_slugs = array_map('trim', explode(',', $atts['category_exclude'])); $exclude_cat_ids = array(); foreach ($exclude_slugs as $slug) { $category = get_category_by_slug($slug); if ($category instanceof WP_Term) { $exclude_cat_ids[] = $category->term_id; } } if (!empty($exclude_cat_ids)) { $query_args['category__not_in'] = $exclude_cat_ids; } } // Run the query $blog_query = new WP_Query($query_args); // Pass data to Twig template (Timber-specific) $context = Timber::get_context(); $context['posts'] = $blog_query; return Timber::compile('templates/blog-list.twig', $context); } add_shortcode('blog_list', 'blog_list_shortcode');
Step 3: Test the Updated Shortcode
You can now use the shortcode with the new videos category:
- Show only videos:
[blog_list category="videos"] - Exclude videos (along with In The News) on the blog homepage:
[blog_list category_exclude="in-the-news,videos"] - Show both In The News and videos:
[blog_list category="in-the-news,videos"] - Show only In The News (existing usage remains valid):
[blog_list category="in-the-news"]
Step 4: Verify Twig Template (If Needed)
Check your blog-list.twig template to ensure there's no hardcoded logic that would block the videos category from rendering. For example, if you have code like {% if post.category.slug != 'videos' %}, you'd need to remove that. Otherwise, standard Twig code like {{ post.category.name }} or {{ post.link }} will work automatically with the new category.
内容的提问来源于stack exchange,提问作者user5853892




