dqnf28092 2016-10-19 10:31
浏览 19

功能/过滤器/动作来替换woocommerce的产品标题

I have this function

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);

and it can replace the product title with return '<span class="border">FooBar</span>';.

I also have a custom script in "mycustomtitle.php" that can modify the products titles and my script can echo that modified title as $mycustomtitle

I want to replace the original product title with my $mycustomtitle without changing anything in the core files.

I've tried to just change return '<span class="border">FooBar</span>'; to $mycustomtitle but it only removes the original title and gives no output at all...

Thanks!

UPDATE 2016-10-20 With custom code:

<?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);
}
}
?>
  • 写回答

3条回答 默认 最新

  • dsadsa123111 2016-10-19 10:42
    关注

    Is $mycustomtitle global variable or it is defined inside another function/method/class? If it is global variable this may help:

    function my_product_title($title, $id)
    {
          global $mycustomtitle;
          if(in_the_loop() && is_product())
          {
            return $mycustomtitle;
          }
          return $title;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致