【问题标题】:IF ELSE header images with ACF Pro, showing wrong image for archive page使用 ACF Pro 的 IF ELSE 标题图像,显示存档页面的错误图像
【发布时间】:2016-01-08 14:33:19
【问题描述】:

我试图根据用户访问的页面显示不同的标题图像。下面的代码很好用,除了 is_archive(listings)

出于某种原因,它想使用来自 is_archive(team) 的图像

<div class="header-images">
   <?php if (is_archive('team')) { ?>
<img src="<?php the_field('header_image_team', 'options'); ?>" class="img-responsive bkg" />

   <?php } elseif (is_singular('team')) { ?>
<img src="<?php the_field('header_image_team', 'options') ?>" class="img-responsive bkg" /> 

   <?php } elseif (is_archive('listings')) { ?>
<img src="<?php the_field('header_image_listings', 'options'); ?>" class="img-responsive bkg" />

   <?php } elseif (is_singular('listings')) { ?>
<img src="<?php the_field('header_image_single') ?>" class="img-responsive bkg" />

   <?php } elseif (is_home()) { ?>
<img src="<?php the_field('header_image_ic', 'options') ?>" class="img-responsive bkg" /> 
   <?php } ?>

对于它的价值,我将它与 Advanced Custom Fields PRO 一起使用。所以我为自定义帖子类型设置了一个选项页面:团队和列表。在每个帖子类型菜单中都有一个设置面板,可以上传图片。

单数“列表”从每个帖子中提取一张图片,然后当然 is_home(默认博客)有一个选项面板,其中包含要上传的图片。

总的来说,如果我没有声明“列表”的存档页面应该显示团队形象,那么它不应该显示任何东西,对吧?

这是 ACF Pro 选项的功能。

if( function_exists('acf_add_options_page') ) {
  acf_add_options_page(array(
    'page_title'  => 'Theme General Settings',
    'menu_title'  => 'Theme Settings',
    'menu_slug'   => 'theme-general-settings',
    'capability'  => 'edit_posts',
    'redirect'    => false
  ));

  acf_add_options_sub_page(array(
    'page_title'  => 'Theme Header Settings',
    'menu_title'  => 'Header',
    'parent_slug' => 'theme-general-settings',
  ));

  acf_add_options_sub_page(array(
    'page_title'  => 'Theme Footer Settings',
    'menu_title'  => 'Footer',
    'parent_slug' => 'theme-general-settings',
  ));

  acf_add_options_sub_page(array(
    'title'      => 'Team Settings',
    'parent'     => 'edit.php?post_type=team',
    'capability' => 'manage_options'
  ));

  acf_add_options_sub_page(array(
    'title'      => 'Listings Settings',
    'parent'     => 'edit.php?post_type=listings',
    'capability' => 'manage_options'
  ));

  acf_add_options_sub_page(array(
    'title'      => 'Industry Coverage Settings',
    'parent'     => 'edit.php',
    'capability' => 'manage_options'
  ));
}

【问题讨论】:

    标签: wordpress if-statement advanced-custom-fields


    【解决方案1】:

    您缺少几个分号,在 listings 之前的 elseif 语句中也是如此。也许这就是你的问题。

    使用正确的语法:

    <?php } elseif (is_singular('team')) { ?>
    <img src="<?php the_field('header_image_team', 'options'); ?>" class="img-responsive bkg" /> 
    

    【讨论】:

    • 抱歉,重新添加这些分号并没有解决任何问题。
    • 嗯,好的。据我所知,您的选项页面代码中一定有错误。通读 ACF 文档,看起来还不错。
    • 我已将选项页面代码功能添加到我的原始帖子中。在我看来一切都很好。也许你看到了我没有看到的东西。
    【解决方案2】:

    如果您没有 archive.php 或 achive-your_custom_post.php,Wordpress 使用 index.php 来显示页面。

    然后,如果您没有看到正确的图像,这很正常。 导致你的情况是 is_home()。

    【讨论】:

    • 我确实有两个存档页面用于“团队”和“列表”。这就是我在这些自定义帖子类型存档页面上显示图像的方式。
    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多