WP儿童主题怪异

时间:2014-03-21 20:32:50

标签: php html wordpress woocommerce

好的,所以我为一些主题调整制作了一个非常基本的儿童主题。问题是,当我更换其中一个woocommerce模板时,它会导致完全意外的HTML呈现。谁能告诉我哪里出错了?

原始模板:

<?php
/**
 * Single Product Price, including microdata for SEO
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $product;
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">

    <p class="price"><?php echo $product->get_price_html(); ?></p>

    <meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
    <meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
    <link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />

</div>

呈现HTML:

<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">

    <p class="price"><span class="amount">&#36;1.60</span>&ndash;<span class="amount">&#36;25.00</span></p>

    <meta itemprop="price" content="1.6" />
    <meta itemprop="priceCurrency" content="USD" />
    <link itemprop="availability" href="http://schema.org/InStock" />

</div>

儿童模板:

<?php
/**
 * Single Product Price, including microdata for SEO
 *
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $product;
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">

<?php if (is_product() and $post->post_name == 'club-high-fear'): ?>

      <p class="price"> <?php echo $product->get_price_html(); ?> per month</p>

<?php elseif (is_product() and $post->post_name == 'club-medium-fear'): ?>

      <p class="price"> <?php echo $product->get_price_html(); ?> per month</p>

<?php elseif (is_product() and $post->post_name == 'club-low-fear'): ?>

      <p class="price"> <?php echo $product->get_price_html(); ?> per month</p>

<?php elseif (is_product() and $post->post_name == 'self-study-online-course'): ?>

      <p class="price"> <?php echo $product->get_price_html(); ?> per course</p>

<?php elseif (is_product() and $post->post_name == '10-week-workshop'): ?>

      <p class="price"> <?php echo $product->get_price_html(); ?> per workshop</p>

<?php elseif (is_product() and $post->post_name == 'coaching'): ?>

      <p class="price"> <?php echo $product->get_price_html(); ?> per minute or $25 per email</p>

<?php else: ?>
       <p class="price"> <?php echo $product->get_price_html(); ?></p>
<?php endif; ?>

    <meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
    <meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
    <link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />

</div>

子模板呈现HTML:

<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">

    <p class="price"><span class="amount">&#36;1.60</span>&ndash;<span class="amount">&#36;25.00</span></p>
              <p class="price"><span class="amount">&#36;1.60</span>&ndash;<span class="amount">&#36;25.00</span> per minute or $25 per email</span>


    <meta itemprop="price" content="1.6" />
    <meta itemprop="priceCurrency" content="USD" />
    <link itemprop="availability" href="http://schema.org/InStock" />

</div>

我不明白为什么价格会被输出两次,或者为什么meta标签被拉进p标签内甚至没有关闭,任何见解都会非常感激。如果我做错了什么的话,我在这里的第一个问题也是如此。“

1 个答案:

答案 0 :(得分:0)

在每个if语句中,尝试将and替换为&&所以

<?php if (is_product() and $post->post_name == 'club-high-fear'): ?>

会变成

<?php if (is_product() && $post->post_name == 'club-high-fear'): ?>

还尝试用if括号括起if语句的内容。

<?php if (is_product() and $post->post_name == 'club-high-fear') { ?>

  <p class="price"> <?php echo $product->get_price_html(); ?> per month</p>

<?php } ....?>

您可以使用||或符号来缩短代码。

<?php if (is_product()  && $post->post_name == 'club-high-fear' || $post->post_name == 'club-medium-fear' ||  $post->post_name == 'club-low-fear'){ ?>

  <p class="price"> <?php echo $product->get_price_html(); ?> per month</p>

<?php } elseif (is_product() && $post->post_name == 'self-study-online-course'){ ?>

  <p class="price"> <?php echo $product->get_price_html(); ?> per course</p>

<?php } elseif (is_product() && $post->post_name == '10-week-workshop'){ ?>

  <p class="price"> <?php echo $product->get_price_html(); ?> per workshop</p>

<?php } elseif (is_product() && $post->post_name == 'coaching') { ?>

  <p class="price"> <?php echo $product->get_price_html(); ?> per minute or $25 per email</p>

<?php } else { ?>
   <p class="price"> <?php echo $product->get_price_html(); ?></p>
<?php } ?>