【问题标题】:How to show thumbnail on archive pages only?如何仅在存档页面上显示缩略图?
【发布时间】:2015-08-13 22:28:54
【问题描述】:

我有一个 Wordpress/Woocommerce 网站,其主题使用以下代码在帖子标题旁边显示特色图片缩略图:

<?php 
    if ( isset( $woo_options['woo_post_content'] ) && 
           $woo_options['woo_post_content'] != 'content' )
    { 
        woo_image( 'width=' . $settings['thumb_w'] . '&height=' . $settings['thumb_h'] . '&class=thumbnail ' . $settings['thumb_align'] ); 
    }
?>

但是,我想仅在存档页面上显示此缩略图,而不是在主页上。是否可以修改代码以检查当前页面是否为主页,如果是,则不显示缩略图?

【问题讨论】:

标签: php wordpress woocommerce thumbnails


【解决方案1】:

您正在寻找WordPress Conditional Tags。这篇 Codex 文章解释了内容如何根据​​条件显示在特定页面上。

此外,问题标题与问题描述相矛盾。您想要 (a) 仅存档页面上的缩略图还是 (b) 除主页以外的所有页面上的缩略图?

虽然jeroen 已回答案例 (b),但我想将其扩展为回答案例 (a)。

使用is_archive()检查您是否在存档页面上,然后显示缩略图。

    if (is_archive()) {
        if ( isset( $woo_options['woo_post_content'] ) && 
               $woo_options['woo_post_content'] != 'content' )
        { 
            woo_image( 'width=' . $settings['thumb_w'] . '&height=' . $settings['thumb_h'] . '&class=thumbnail ' . $settings['thumb_align'] ); 
        }
    }

【讨论】:

    【解决方案2】:

    您可以检查来自 $_SERVER['REQUEST_URI'] 的页面 url 以显示缩略图,如果该 url 与所需页面匹配,请参阅function.preg-match.php

    【讨论】:

      【解决方案3】:

      我假设我们在这里谈论的是 WooCommerce / WordPress。

      在这种情况下,您可以使用is_home()检查您是否在主页上:

      if (!is_home())
      {
          if ( isset( $woo_options['woo_post_content'] ) && 
                 $woo_options['woo_post_content'] != 'content' )
          { 
              woo_image( 'width=' . $settings['thumb_w'] . '&height=' . $settings['thumb_h'] . '&class=thumbnail ' . $settings['thumb_align'] ); 
          }
      }
      

      您当然可以将所有这些结合起来,为了清楚起见,我已经写出来了。

      【讨论】:

      • @HareeshSivasubramanian 感谢您的帮助。它现在正在工作。我之前尝试过if (!is_home ()),但由于语法不正确而出错。
      猜你喜欢
      • 2018-06-14
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多