请参阅PHP中的WordPress / WooCommerce中的特定属性ID

时间:2014-02-05 05:19:17

标签: php wordpress woocommerce

我有这个功能,可以在每个产品页面上创建一个下载规格表按钮。

if(is_product())
{
    $link = get_the_title();
    $link =  str_replace(' ', '-', $link);
    echo '<div class="download">';
    echo '<a href="';
    echo bloginfo('url');
    echo '/pdf/Spec-Sheet-';
    echo $link . '.pdf';
    echo ' " title="Download Spec Sheet" target="_blank">';
    echo 'DOWNLOAD SPEC SHEET';
    echo '</a>'; 
    echo '</div>'; 
} 

但我现在正在添加一堆不需要规格表的产品。我创建了一个属性machine,我已将其添加到需要此按钮的产品中。 (属性slug是machine并且有一个值,slug是machine-2

这是我试图使用的代码,但它失败了,不知道为什么......任何帮助都会受到高度赞赏。

if(!is_product())
{
    $machines = get_the_terms( $product->id, 'pa_machine');

    if($machines != '')
    {
        $link = get_the_title();
        $link =  str_replace(' ', '-', $link);
        echo '<div class="download">';
        echo '<a href="';
        echo bloginfo('url');
        echo '/pdf/Spec-Sheet-';
        echo $link . '.pdf';
        echo ' " title="Download Spec Sheet" target="_blank">';
        echo 'DOWNLOAD SPEC SHEET';
        echo '</a>'; 
        echo '</div>'; 
    } 
}

1 个答案:

答案 0 :(得分:0)

我已成功移除了!中的if(!is_product())。只是去显示1个小错误改变了代码的全部含义。如果其他人有类似的问题,这是工作代码。

if(is_product());
{

  $machines = get_the_terms( $product->id, 'pa_machine');

    if ( $machines != '' )
    {
        $link = get_the_title();
        $link =  str_replace(' ', '-', $link);
        echo '<div class="download">';
        echo '<a href="';
        echo bloginfo('url');
        echo '/pdf/Spec-Sheet-';
        echo $link . '.pdf';
        echo ' " title="Download Spec Sheet" target="_blank">';
        echo 'DOWNLOAD SPEC SHEET';
        echo '</a>'; 
        echo '</div>'; 
    }
}