【问题标题】:Wordpress is page str replaceWordpress 是页面 str 替换
【发布时间】:2013-11-24 23:06:23
【问题描述】:

我在 wordpress 网站上有页面

http://upsourcemobileservices.com/solutions/

在footer.php中我添加了以下内容:

if(is_page('solutions')){
    echo str_replace("The MVNi","The MVNi solution was created to ensure that wireless communications are available to the smaller Telco operators.","The MVNi");
}

但它不会改变页面的内容。

据我所知,语法是正确的str_replace(find,replace,string)

我做错了什么?

我也尝试过使用 jQuery

function replaceText(jQuery) {
    $(".portfolio-thumbnail-content:nth-child(1)").replaceWith("<div class='portfolio-thumbnail-content'>The MVNi solution was created to ensure that wireless communications are available to the smaller Telco operators.</div>");
    $(".portfolio-thumbnail-content:nth-child(2)").replaceWith("<div class='portfolio-thumbnail-content'>Primarily aimed at existing Telcos our MVNc solution integrates your existing platforms with ours through the use of APIs.</div>");
    $(".portfolio-thumbnail-content:nth-child(3)").replaceWith("<div class='portfolio-thumbnail-content'>UMS turnkey solution is a complete MVNO in a box.</div>");
    $(".portfolio-thumbnail-content:nth-child(4)").replaceWith("<div class='portfolio-thumbnail-content'>This solution allows you to select the products and services that you want and integrate them with your existing platforms.</div>");
}
$(document).ready(replaceText);

但似乎也无法正常工作,这是一个小提琴http://jsfiddle.net/vzy9R/3/

【问题讨论】:

  • 您没有正确使用str_replace。第三个参数应该是你想在其中找到“针”的“干草堆”(第一个参数)。
  • 不确定我理解正确。我想替换整个字符串,而不仅仅是一个单词。
  • 您的目标是在特定的 WordPress 页面上显示不同的页脚吗?
  • 如果您查看该解决方案页面,您会看到其子页面(MVNi、MVNc、MVNO Turnkey 和 MVNO Customized)的 4 个缩略图或“摘录”。主题允许我获取所有子页面的摘录(以字符数为单位),但不为每个子页面设置字符数,所以如果我将摘录字符设置为 8,它将获取每个子页面的前 8 个字符,或者如果我将前 100 个设置为 100,依此类推。我希望每个摘录都以一个句子结尾,这就是为什么我试图用代码中的完整句子替换“The MVNi”。一旦我可以让一个工作,我想为所有 4 个缩略图做同样的事情。
  • 在这种情况下,您希望str_replace (haystack) 中的第三个参数是存储摘录的 PHP 变量。

标签: php jquery wordpress str-replace


【解决方案1】:

我使用 jQuery eq() 选择器查找包含类 portfolio-thumbnail-content 的元素,然后使用 .replaceWith() 我能够加载以完整句子结尾的预告内容(摘录)。

例如关于页面:

(function ($) {
 $(".portfolio-thumbnail-content:eq(0)").replaceWith("<div class='portfolio-thumbail-content'>This is my new complete sentence teaser for Team.</div>");
 $(".portfolio-thumbnail-content:eq(1)").replaceWith("<div class='portfolio-thumbail-content'>This is my new complete sentence teaser for Partners.</div>");
})(jQuery);

然后我必须想办法将这些脚本实现到这些页面的正文中,所以我编写了自己的短代码并将它们添加到 functions.php 中

//[replaceTextAbout]
 function replaceTextAbout_func( $atts ){
  return "<script src='/javascript/custom/replaceText-About.js'></script>";
 }
 add_shortcode( 'replaceTextAbout', 'replaceTextAbout_func' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多