首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数/筛选器/替换woocommerce产品标题的操作

函数/筛选器/替换woocommerce产品标题的操作
EN

Stack Overflow用户
提问于 2016-10-19 10:31:32
回答 3查看 2.2K关注 0票数 0

我有这个功能

代码语言:javascript
复制
function my_product_title($title, $id)
    {
if(in_the_loop() && is_product())
{
    return '<span class="border">FooBar</span>';
}
return $title;
}
add_filter( 'the_title', 'my_product_title', 5, 2);

它可以用return '<span class="border">FooBar</span>';代替产品的标题。

我还在"mycustomtitle.php“中有一个自定义脚本,它可以修改产品标题,我的脚本也可以将修改后的标题回显为$mycustomtitle

我想用我的$mycustomtitle替换原来的产品标题,而不改变核心文件中的任何内容。

我尝试过将return '<span class="border">FooBar</span>';更改为$mycustomtitle,但它只删除了原来的标题,根本没有输出.

谢谢!

更新2016-10-20与自定义代码:

代码语言:javascript
复制
<?php

include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';

$tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php'); 

$tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');

$title_keys = array($tit1,$tit2);
$title_key11 = $title_keys[array_rand($title_keys)];

if(!function_exists(spin11)){
function spin11($string11) {
    while(true) {
        if(!preg_match_all('/({([^\{]*?)\})/', $string11, $matches))
            break;
        foreach($matches[2] as $i => $match) {
            $parts = explode('|', $match);
            $string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
        }
    }
    return $string11;
}
}
if(!function_exists(str_replace_once11)){
function str_replace_once11($from,$to,$str)
{
    $str = explode($from,$str,2);
    return $str[0].$to.$str[1];
}  
$title_id11  = get_the_ID();
$fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
if(!file_exists($fileLocation11)){
$file11 = fopen($fileLocation11,"w");
$content11 = spin11($title_key11);
fwrite($file11,$content11);
fclose($file11);
}
if(file_exists($fileLocation11)){
$myFile11 = $fileLocation11;
$fh11 = fopen($myFile11, 'r');
$theData11 = fread($fh11, filesize($myFile11));
fclose($fh11);
}
}
?>
EN

回答 3

Stack Overflow用户

发布于 2016-10-19 10:54:06

在函数中插入自定义代码。

代码语言:javascript
复制
function my_product_title($title, $id) {
    include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';

    $tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php'); 

    $tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');

    $title_keys = array($tit1,$tit2);
    $title_key11 = $title_keys[array_rand($title_keys)];

    if(!function_exists(spin11)){
        function spin11($string11) {
            while(true) {
                if(!preg_match_all('/({([^\{]*?)\})/', $string11, $matches))
                    break;
                foreach($matches[2] as $i => $match) {
                    $parts = explode('|', $match);
                    $string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
                }
            }
            return $string11;
        }
    }
    if(!function_exists(str_replace_once11)){
        function str_replace_once11($from,$to,$str)
        {
            $str = explode($from,$str,2);
            return $str[0].$to.$str[1];
        }  
        $title_id11  = get_the_ID();
        $fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
        if(!file_exists($fileLocation11)){
            $file11 = fopen($fileLocation11,"w");
            $content11 = spin11($title_key11);
            fwrite($file11,$content11);
            fclose($file11);
        }
        if(file_exists($fileLocation11)){
            $myFile11 = $fileLocation11;
            $fh11 = fopen($myFile11, 'r');
            $theData11 = fread($fh11, filesize($myFile11));
            fclose($fh11);
        }
    }
    if(in_the_loop() && is_product()) {
        return $theData11;
    }
    return $title;
}
add_filter( 'the_title', 'my_product_title', 5, 2);
票数 1
EN

Stack Overflow用户

发布于 2016-10-19 10:42:17

$mycustomtitle是全局变量还是在另一个函数/方法/类中定义?如果它是全局变量,这可能有助于:

代码语言:javascript
复制
function my_product_title($title, $id)
{
      global $mycustomtitle;
      if(in_the_loop() && is_product())
      {
        return $mycustomtitle;
      }
      return $title;
}
票数 0
EN

Stack Overflow用户

发布于 2016-10-19 10:54:59

代码语言:javascript
复制
<?php

include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';

$tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php'); 

$tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');

$title_keys = array($tit1,$tit2);
$title_key11 = $title_keys[array_rand($title_keys)];

if(!function_exists(spin11)){
function spin11($string11) {
    while(true) {
        if(!preg_match_all('/({([^\{]*?)\})/', $string11, $matches))
            break;
        foreach($matches[2] as $i => $match) {
            $parts = explode('|', $match);
            $string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
        }
    }
    return $string11;
}
}
if(!function_exists(str_replace_once11)){
function str_replace_once11($from,$to,$str)
{
    $str = explode($from,$str,2);
    return $str[0].$to.$str[1];
}  
$title_id11  = get_the_ID();
$fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
if(!file_exists($fileLocation11)){
$file11 = fopen($fileLocation11,"w");
$content11 = spin11($title_key11);
fwrite($file11,$content11);
fclose($file11);
}
if(file_exists($fileLocation11)){
$myFile11 = $fileLocation11;
$fh11 = fopen($myFile11, 'r');
$theData11 = fread($fh11, filesize($myFile11));
fclose($fh11);
}
}
//echo $theData11;
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40128831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档