BEGIN 主题可以给文章分类页面、独立页面以及文章详情页面添加标题图片,但是在搜索结果页面及 404 页面却无法通过配置添加标题图片,本文简单分享一下给搜索结果页面和 404 页面添加标题图片,以及批量给文章页面添加标题图片的实现方法。

BEGIN 主题搜索结果页面添加标题图片-图片1

给搜索页面添加标题图片

给搜索结果页面、标签页面及 404 页面添加标题图片,可以通过修改源码的形式实现。

1、实现方法

编辑 header-slider.php,位置:wp-content/themes/begin/template/header-slider.php,在最末端最后一行下面添加:

  1. <?php if ( is_search() || is_tag() || is_404() ) { ?>
  2. <div class="header-sub">
  3. <div class="cat-des wow fadeInUp">
  4. <div class="cat-des-img">
  5. <img src="https://oss.pieruo.com/2020/07/07/iHRs.jpg" />
  6. </div>
  7. <div class="header-title-main bgt">
  8. <h1 class="header-title bgt wow fadeInUp">
  9. <?php
  10. if(is_search()) {
  11. echo '搜索结果';
  12. }elseif (is_tag()) {
  13. echo single_tag_title('', false);
  14. }elseif (is_404()) {
  15. echo '亲,你迷路了';
  16. }else {
  17. echo '';
  18. }
  19. ?>
  20. </h1>
  21. </div>
  22. <div class="clear"></div>
  23. </div>
  24. </div>
  25. <?php }?>

2、效果预览

预览效果如下:

BEGIN 主题搜索结果页面添加标题图片-图片2

给文章添加标题图片

文章标题图片可以在文章编辑页面添加,那么,如何批量给文章添加标题图片的方式呢?

思路:给文章批量添加标题图片,需要先确认哪些文章已添加标题图片,如果有的话可以先删除,然后通过 SQL 语句批量添加。

1、查询

查询已添加标题图片的文章:

  1. select * from wp_postmeta m where m.meta_key = 'header_bg'
  2. and m.post_id in (
  3. SELECT t.id from wp_posts t where t.post_type = 'post' and t.post_status = 'publish'
  4. )
  5. # limit 100

建议添加 limit 语句, 防止查询的数据量过大。

2、删除

删除已经存在的文章标题图片:

  1. delete from wp_postmeta m where m.meta_key = 'header_bg'
  2. and m.post_id in (
  3. SELECT t.id from wp_posts t where t.post_type = 'post' and t.post_status = 'publish'
  4. )

3、批量添加

确认文章都没有标题图片后,给文章批量添加标题图片:

  1. INSERT INTO wp_postmeta
  2. (post_id, meta_key, meta_value)
  3. SELECT
  4. t.id, 'header_bg', 'https://oss.pieruo.com/2020/07/07/iHRs.jpg'
  5. from wp_posts t where t.post_type = 'post' and t.post_status = 'publish'

4、效果预览

效果如下:

BEGIN 主题搜索结果页面添加标题图片-图片3

5、文章标题及面包屑不显示解决方法

给文章添加标题图片后,会发现正文部分不显示文章标题和面包屑,因为文章标题和面包屑默认是在标题图片位置显示的。

a、显示文章标题

编辑 content.php,位置:wp-content/themes/begin/template/content.php,查找:

  1. <?php if ( get_post_meta($post->ID, 'header_img', true) || get_post_meta($post->ID, 'header_bg', true) ) { ?>
  2. <?php } else { ?>
  3. <?php if ( get_post_meta($post->ID, 'mark', true) ) { ?>
  4. <?php the_title( '<h1 class="entry-title">', '<span class="t-mark">' . $mark = get_post_meta($post->ID, 'mark', true) . '</span></h1>' ); ?>
  5. <?php } else { ?>
  6. <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
  7. <?php } ?>
  8. <?php } ?>

修改为:

  1. <?php if ( get_post_meta($post->ID, 'mark', true) ) { ?>
  2. <?php the_title( '<h1 class="entry-title">', '<span class="t-mark">' . $mark = get_post_meta($post->ID, 'mark', true) . '</span></h1>' ); ?>
  3. <?php } else { ?>
  4. <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
  5. <?php } ?>

b、显示面包屑

同样,查找:

  1. <?php if ( ! get_post_meta($post->ID, 'header_img', true) && !get_post_meta($post->ID, 'header_bg', true) ) : ?>
  2. <?php if (zm_get_option('meta_b')) {
  3. begin_single_meta();
  4. } else {
  5. begin_entry_meta();
  6. } ?>
  7. <?php endif; ?>

修改为:

  1. <?php if ( ! get_post_meta($post->ID, 'header_img', true) && !get_post_meta($post->ID, 'header_bg', true) ) { ?>
  2. <?php if (zm_get_option('meta_b')) {
  3. begin_single_meta();
  4. } else {
  5. begin_entry_meta();
  6. } ?>
  7. <?php } else { ?>
  8. <?php if (zm_get_option('meta_b')) {
  9. begin_single_meta();
  10. } else {
  11. begin_entry_meta();
  12. } ?>
  13. <?php }?>

3、效果预览

效果如下:

BEGIN 主题搜索结果页面添加标题图片-图片4

结束语

本文简单分享了一下给搜索结果页面和 404 页面添加标题图片,以及批量给文章页面添加标题图片的实现方法,供有此需求的朋友参考。如果有任何意见或建议,欢迎在下方评论处留言。

本文已通过「原本」原创作品认证,转载请注明文章出处及链接。

夏日阳光
  • 本文由 夏日阳光 发表于 2022年6月4日
  • 本文为夏日阳光原创文章,转载请务必保留本文链接:https://www.pieruo.com/217.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证
加载中...