【问题标题】:Storing woocommerce_page_title in a variable and display it in Woocommerce将 woocommerce_page_title 存储在变量中并在 Woocommerce 中显示
【发布时间】:2018-08-29 17:30:41
【问题描述】:

我想在类别图像上显示页面标题。我复制了这段代码

<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>

从“template-archive-product.php”到“woocommerce-hook.php”如下:

$html .= '<h1 class="page-title">'<?php woocommerce_page_title(); ?>'</h1>';

现在标题没有出现。当我检查开发工具时,标题就像一个 html-comment。

<h1 class="page-title"><!--?php woocommerce_page_title(); ?--></h1>

如果有人可以帮助我,那就太好了。

【问题讨论】:

    标签: php wordpress templates woocommerce page-title


    【解决方案1】:

    function woocommerce_page_title() 模板默认回显,正如您在its source code 中看到的那样...但是有一个可选参数用于返回数据,而不是回显它。为此,您需要将此参数从(默认)true 转换为 false

    您需要在变量中设置此数据时,这很有用,就像您尝试做的那样。所以功能代码应该有点不同:

    $html .= '<h1 class="page-title">' . woocommerce_page_title( false ); . '</h1>';
    

    经过测试并且有效。

    或者你也可以使用 php 缓冲函数来获得同样的东西,这样:

    ob_start();
    ?>
    <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
    <?php
    $html .= ob_get_clean();
    

    不会输出任何内容,数据将附加到$html 变量中。

    【讨论】:

      【解决方案2】:

      试试这个代码:

      $html .= '<h1 class="page-title">' . woocommerce_page_title() .'</h1>';
      

      代替:

      $html .= '<h1 class="page-title">'<?php woocommerce_page_title(); ?>'</h1>';
      

      【讨论】:

      • 但是没有 echo 是不是意味着这个标题会直接打印出来而忽略 = 标记?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2016-11-08
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多