【问题标题】:How to use ACF Pro Flexible Content with Single Page?如何在单页中使用 ACF Pro 灵活内容?
【发布时间】:2023-03-29 03:20:01
【问题描述】:

我正在开发一个内容灵活的网站。我基本上将它用作构建器。

我在页面部分,例如投资组合,我可以“添加行”。惊人的!所有这些页面都使用page.php 显示此代码:

<?php 

if( have_rows('template_blocks') ):

while ( have_rows('template_blocks') ) : the_row();

    if( get_row_layout() == 'header_page' ):
       get_template_part( 'templates/section', 'header-page' );

    elseif( get_row_layout() == 'header_main' ):
       get_template_part( 'templates/section', 'header-main' );

    elseif( get_row_layout() == 'services_excerpt' ):
       get_template_part( 'templates/section', 'services-excerpt' );

    elseif( get_row_layout() == 'contact' ):
       get_template_part( 'templates/section', 'contact' );

    elseif( get_row_layout() == 'services_tabs' ):
       get_template_part( 'templates/section', 'services-tabs' );

    elseif( get_row_layout() == 'standards' ):
       get_template_part( 'templates/section', 'standards' );

    elseif( get_row_layout() == 'contact' ):
       get_template_part( 'templates/section', 'contact' );

    elseif( get_row_layout() == 'footer' ):
       get_template_part( 'templates/section', 'footer' );

    elseif( get_row_layout() == 'bar_secondary' ):
       get_template_part( 'templates/section', 'bar-secondary' );

    elseif( get_row_layout() == 'process' ):
       get_template_part( 'templates/section', 'process' );

    elseif( get_row_layout() == 'empty_bar' ):
       get_template_part( 'templates/section', 'empty-bar' );

    elseif( get_row_layout() == 'form_section' ):
       get_template_part( 'templates/section', 'form-section' );

    elseif( get_row_layout() == 'callout' ):
       get_template_part( 'templates/section', 'callout' );

    elseif( get_row_layout() == 'ready_bar' ):
       get_template_part( 'templates/section', 'ready-bar' );

    elseif( get_row_layout() == 'portfolio_excerpt' ):
       get_template_part( 'templates/section', 'portfolio-excerpt' );

    elseif( get_row_layout() == 'services_card' ):
       get_template_part( 'templates/section', 'services-card' );

    elseif( get_row_layout() == 'about_author' ):
       get_template_part( 'templates/section', 'about-author' );

    elseif( get_row_layout() == 'work_featured' ):
       get_template_part( 'templates/section', 'work-featured' );

    elseif( get_row_layout() == 'work_all' ):
       get_template_part( 'templates/section', 'work-all' );

    elseif( get_row_layout() == 'work_tab' ):
       get_template_part( 'templates/section', 'work-tab' );

    elseif( get_row_layout() == 'action_block' ):
       get_template_part( 'templates/section', 'action-block' );
    endif;

endwhile; endif;
?>

但是,现在我创建了一个自定义帖子类型

自定义字段 -> 在以下位置显示此字段组: 帖子类型[等于] [项目]

所以我继续创建几个项目。现在,在代码中,我得到了 WP Query,所以我可以接受这些项目。

但是,现在我点击一个项目,它会去哪里?它转到single.php 页面!如何编辑single.php 页面?没有像普通页面那样的选项。

怎么说呢,为作品集添加单页,添加灵活的内容?

当然,我可以添加与page.php 相同的代码,但那又如何?我在哪里可以查看 WP 管理仪表板中的单个页面?

我知道这是可能的。

我一直在尝试解决这个问题,但我无法解决。

如果有帮助,这里是 github 存储库:https://github.com/AurelianSpodarec/aurelianMegaPortfolio2018

目前,我有:主页、服务、流程、关于、联系人、投资组合。

他们都在使用page.php,当我进入管理仪表板页面时,我只是“添加行”,这样我就可以为我提到的每个页面实现独特的外观,除了single.php ,因为我如何为项目制作single.php?还是服务?

【问题讨论】:

  • 您应该展示您是如何创建该自定义帖子类型的。当您创建自定义帖子类型时,您必须定义一些参数 - 这可能会修改您的自定义帖子类型是“post-like”还是“page-like”。它们具有不同的属性,处理方式也不同。
  • 而且你最好使用 PHP switch 而不是大量 if-elseif-elseif... 和最后一个 endif; 在代码块的末尾 - 它是否有一个开头 if
  • Switch 在这种情况下不起作用,如果我是正确的,但无论哪种方式,我都可能会采用另一种方式,一种更清晰的方式,到目前为止,我只是想让它工作。这是回购 - github.com/AurelianSpodarec/aurelianMegaPortfolio2018 这是一些屏幕截图 - imgur.com/a/b8Ajm 问题是,如果我将其设置为例如投资组合这是自定义帖子类型但是结构模板块,需要在每个单上显示 - [ projects].php 到目前为止,这只会显示在单个项目上
  • 我正在编写自己的每一件作品,但问题是,single-[custom-post-type.php 因为没有选项可以改变我的意思.. 你可以做到,但是它将反映在一页上。因此,如果我将其设置为项目页面、模板块,我将需要访问每个项目页面......这是一种可怕的方式。

标签: wordpress advanced-custom-fields


【解决方案1】:

自定义帖子类型不使用page.php 文件,默认使用single.php。您可以通过创建附加了自定义帖子类型名称的文件版本来指定唯一的文件模板,如下所示:single-{post-type-name}.php

在你的情况下,听起来是single-projects.php

您很可能只使用您的 page.php 的副本创建此页面。

这里是引用 WordPress 模板结构的链接。 https://developer.wordpress.org/themes/basics/template-hierarchy/

【讨论】:

  • 不知道我应该做什么。我将自定义帖子类型设置为显示在 page.php 上,这意味着我可以选择或做任何我想做的事情,例如添加一个新字段。是的,我知道如何做到这一切。我知道模板层次结构,但我的观点是,如何在单个 [custom-post-type].php 上使用“灵活内容”?你知道page.php,我可以使用灵活的内容,“添加行”,页面将是样式。每个页面都会有所不同,具体取决于我“添加”到它的内容。他们都将使用page.php。现在,我无法在 UI 中控制 single-cusomt-post-type.php 页面,就像 page.php 一样。
猜你喜欢
  • 2018-09-07
  • 2018-05-31
  • 1970-01-01
  • 2016-04-07
  • 2020-07-10
  • 2018-05-13
  • 1970-01-01
  • 2017-01-07
  • 2016-04-07
相关资源
最近更新 更多