PHP / WordPress:覆盖foreach循环中的page_id

时间:2012-07-16 21:06:15

标签: php wordpress

我有一个WordPress网站,目前它设置的代码基于页面标题创建标题。标题是:室内绘画,外墙绘画和商业绘画。我想覆盖标题删除“房子”这个词。这是当前的代码:

<?php
// interior_painting: 18
// exterior_painting: 25
// other services: 36
$page_ids = array(18, 25, 36);
$images = array('servicesInterior.jpg', 'servicesExterior.jpg', 'servicesOther.jpg');
foreach ($page_ids as $key => $page_id) {
    $page_post = get_post($page_id);
    $page_custom_key = 'home_page_info';
    $page_link = $page_post->post_name;
    $li_class = $page_id == 36 ? 'noMargin' : '';
    $title = $page_id == 18 ? 'Interior Painting' : $page_post->post_title . " ";
    $title = $page_id == 36 ? 'Commercial Projects' : $page_post->post_title . " ";
    $title = $page_id == 25 ? 'Exterior Painting' : $page_post->post_title . " ";


    echo '<li class="' . $li_class . '"> <a href="' . $page_link . '">';
    echo '<img class="alignright size-full wp-image-349" title="servicesInterior" src="/wp-content/uploads/2011/04/' . $images[$key] . '" alt="" width="200" height="95" /></a>';
    echo '<h2>' . $title . '</h2>';
    echo get_post_meta($page_id, $page_custom_key, true);
    echo '<a class="btn-find-more" href="' . $page_post->post_name . '">FIND OUT MORE</a></li>';
}

?>

输出是这样的:室内绘画,外墙绘画,商业绘画。如何从“室内绘画”中删除“房子”?

3 个答案:

答案 0 :(得分:0)

只需使用preg_replace('House'|| 'house','',/*Variable Page title is in*/);

即可

答案 1 :(得分:0)

您可以尝试更换该行:

echo '<h2>' . $title . '</h2>';

有这样的事情:

if (is_page('Interior House Painting')) { echo '<h2>Interior Painting</h2>'; } 
if (is_page('Exterior House Painting')) { echo '<h2>Exterior Painting</h2>'; } 
else { echo '<h2>' . $title. '</h2>'; }

答案 2 :(得分:0)

$title = ucwords(trim(strtolower(str_replace('house', '', $page_post->post_title))));

尽管如此,我有点好奇为什么你不想完全删除标题中的这个词。

另外:其他人提到使用preg_replace。这也是完全可能的,但最好是进行不区分大小写的搜索:

$title = trim(preg_replace('/house/i', '', $page_post->post_title));