求助:WordPress+Redux框架下评论链接及评论数的a标签输出问题
Hey there! Let's break down how to solve both your problems—first fixing that comment link href glitch, then setting up a proper Redux toggle to hide/show your post meta entirely.
1. Fix the Comment Link Href Problem
The issue here is how you're using comments_link(). This function directly outputs the URL to the page, which means when you put it inside the href attribute, it'll print the URL before the rest of the <a> tag (causing invalid HTML).
Instead, use get_comments_link()—this function returns the URL as a string, so you can safely echo it inside the href attribute. We'll also add esc_url() for security (always sanitize URLs in WordPress!).
Here's your corrected code:
<i class="far fa-comment"></i> <a href="<?php echo esc_url(get_comments_link()); ?>"><?php comments_number('No comments', '1 comment', '% comments'); ?></a>
I also fixed the typo in '1 commen...' to '1 comment' and added the plural % comments for better scalability.
2. Add Redux Toggle to Hide Post Meta
Let's walk through setting up the toggle in your Redux panel, then using it to conditionally show/hide your post meta (date, author, comments).
Step 1: Add the Switch Field to Redux
In your theme's Redux configuration file (usually in functions.php or a dedicated redux-config.php), add this field to your options array:
// Replace 'your_theme_options' with your actual Redux opt_name Redux::addField('your_theme_options', array( 'id' => 'hide_post_meta', 'type' => 'switch', 'title' => __('Hide Post Meta', 'your-theme-textdomain'), 'subtitle' => __('Toggle this to hide or show post metadata (date, author, comments)', 'your-theme-textdomain'), 'default' => false, // Default: show post meta 'on' => __('Hide', 'your-theme-textdomain'), 'off' => __('Show', 'your-theme-textdomain'), ));
Make sure to replace your_theme_options with your Redux instance's name, and your-theme-textdomain with your theme's actual text domain.
Step 2: Conditionally Render Post Meta
Wrap your existing post meta code (date, author, comments) in a conditional check that reads the Redux option. This might go in your single.php, content.php, or a dedicated post-meta.php template part:
// Get the Redux option value $hide_post_meta = Redux::get_option('your_theme_options', 'hide_post_meta'); // Only show post meta if the toggle is NOT enabled if (!$hide_post_meta) { ?> <div class="post-meta-container"> <!-- Your existing date code --> <span class="post-date"><?php echo get_the_date(); ?></span> <!-- Your existing author link code --> <span class="post-author"> By <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"><?php echo get_the_author(); ?></a> </span> <!-- The fixed comment link code --> <span class="post-comments"> <i class="far fa-comment"></i> <a href="<?php echo esc_url(get_comments_link()); ?>"><?php comments_number('No comments', '1 comment', '% comments'); ?></a> </span> </div> <?php }
Quick Notes
- Double-check your Redux instance name (
your_theme_options)—you can find this in the code where you initialize Redux (look forRedux::init('your_theme_options')). - Clear any caching plugins after making changes to ensure the toggle works immediately.
- Always use WordPress sanitization functions like
esc_url()andesc_html()to keep your site secure.
内容的提问来源于stack exchange,提问作者wp-design

