doufuxi7093 2015-06-17 23:11
浏览 77
已采纳

如何删除Wordpress子主题中的代码行?

I've been messing around with child themes in Wordpress and while I know how to add or change code from the parent theme I'm having a bit of a problem removing certain code. For example I'm using a theme that uses featured images for thumbnails in posts. I want the added picture for the thumbnail but it also includes it at the top of the actual post which I am trying to remove. I can comment out part of the code to achieve this in the parent functions.php like this

if (!function_exists('mh_featured_image')) {
    function mh_featured_image() {
        global $post;
        if (has_post_thumbnail() && !is_attachment()) {
            $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'content');
            // echo "
" . '<div class="post-thumbnail">' . "
";
            //  echo '<img src="' . esc_url($thumbnail[0]) . '" alt="' . esc_attr(get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true)) . '" title="' . esc_attr(get_post(get_post_thumbnail_id())->post_title) . '" />' . "
";
            //  if (get_post(get_post_thumbnail_id())->post_excerpt) {
            //      echo '<span class="wp-caption-text">' . esc_attr(get_post(get_post_thumbnail_id())->post_excerpt) . '</span>' . "
";
            //  }
            // echo '</div>' . "
";
        }
    }
   }

So when I comment out the actual code out in the parent functions.php as shown above everything works perfectly. My issue is how do I translate this to the child functions.php? I just want to keep everything but edit out those six lines that are echoed.

  • 写回答

1条回答 默认 最新

  • dtuct88226 2015-06-18 00:49
    关注

    im not sure i understand the issue correctly, have you set up a child theme ?

    If so, the child themes functions are loaded before the parent themes allowing you to overwrite the parent functions that are wrapped in:

    if (!function_exists('myfunction')) { 
           function myfunction(){}; 
    }
    

    Which is basically saying, if the function myfunction() does not exist (note the ! operator which means not in a if statement), it has not been defined in a earlier file (in this case your child theme, could also be a plugin which are also loaded before theme files) and the function is then defined. As you may or may not know you cannot redeclare a function in php so by only defining the function if it has not already been defined allows for your own definition to be made.

    Therefore to change the function to the version you want, you place the following in the child themes functions.php file

    function mh_featured_image() {
        global $post;
        if (has_post_thumbnail() && !is_attachment()) {
            $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'content');
        }
    }
    

    And it should work, although your function doesn't actually return or output or store any values, so you need to consider this in your expected result

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单