有些主题的特色图像会显示在文章内页里面,多数情况我们不想让它显示在内页里,那么我们可以添加一个选项控制显示。代码如下:
function prefix_featured_image_meta( $content ) { global $post; $text = __( 'Don\'t display image in post.', 'prefix' ); $id = 'hide_featured_image'; $value = esc_attr( get_post_meta( $post->ID, $id, true ) ); $label = '<label for="' . $id . '" class="selectit"><input name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $value . ' "'. checked( $value, 1, false) .'> ' . $text .'</label>'; return $content .= $label; } add_filter( 'admin_post_thumbnail_html', 'prefix_featured_image_meta' ); function prefix_save_featured_image_meta( $post_id, $post, $update ) { $value = 0; if ( isset( $_REQUEST['hide_featured_image'] ) ) { $value = 1; } // Set meta value to either 1 or 0 update_post_meta( $post_id, 'hide_featured_image', $value ); } add_action( 'save_post', 'prefix_save_featured_image_meta', 10, 3 );
将以上代码加到functions.php里即可。
然后在single.php里需要判断文章的hide_featured_image字段是否为1(隐藏),剩下功能代码请自行根据自己的主题修改即可。
原文链接:https://www.dqzy.cn/2025/05/26/692.html,转载请注明出处。
1、本站所有源码资源(包括源代码、软件、学习资料等)仅供研究学习以及参考等合法使用,请勿用于商业用途以及违法使用。如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容!
2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,请用户在下载使用前必须详细阅读并遵守软件作者的“使用许可协议”,本站仅仅是一个学习交流的平台。
3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为:www.dqzy.cn。
4、点启资源网是一个免费且专业分享网站源码、图片素材、特效代码、教程文章、站长工具的平台。我们努力给站长提供好的资源!
评论0