ACF 和侧边栏(WordPress)
ACF and sidebar(WordPress)
我使用 ACF 插件:https://wordpress.org/plugins/advanced-custom-fields/
有一个名为 sidebar.php 的简单 HTML 文件。
这个 sidebar.php 文件有一个地方可以通过 ACF 显示图像:
<figure class="Sidebar_Block">
<img class="ACF_Img" src=" <?php the_field('sidebar-latest') ?> "> // ****** place to display a picture ******
</figure>
在其他文件中 (home.php, category.php) 我通过命令调用 sidebar.php
get_sidebar();
图片显示仅适用于主页(home.php)
而在category.php文件中,通过ACF显示图片是不行的。
问题是:
如何将 ACF 连接到 category.php 并通过 WordPress 管理员显示图像?
问题是您仅为主页保存了 sidebar-latest
字段。我的意思是,它附在主页上。当您调用 the_field
并且没有在第二个参数中传递 page/post ID 时,它将采用当前的 ID。
所以主页可以工作,因为侧边栏图像是为 home_page 保存的,但在您更改页面时不会保存,为了使其工作,将主页的 post ID 传递给第二个参数:
<img class="ACF_Img" src="<?php the_field('sidebar-latest', $home_page_id); ?>">
所以它会在所有页面上工作,另外,记得像我的示例一样删除任何额外的 space。
我使用 ACF 插件:https://wordpress.org/plugins/advanced-custom-fields/
有一个名为 sidebar.php 的简单 HTML 文件。 这个 sidebar.php 文件有一个地方可以通过 ACF 显示图像:
<figure class="Sidebar_Block">
<img class="ACF_Img" src=" <?php the_field('sidebar-latest') ?> "> // ****** place to display a picture ******
</figure>
在其他文件中 (home.php, category.php) 我通过命令调用 sidebar.php
get_sidebar();
图片显示仅适用于主页(home.php) 而在category.php文件中,通过ACF显示图片是不行的。
问题是: 如何将 ACF 连接到 category.php 并通过 WordPress 管理员显示图像?
问题是您仅为主页保存了 sidebar-latest
字段。我的意思是,它附在主页上。当您调用 the_field
并且没有在第二个参数中传递 page/post ID 时,它将采用当前的 ID。
所以主页可以工作,因为侧边栏图像是为 home_page 保存的,但在您更改页面时不会保存,为了使其工作,将主页的 post ID 传递给第二个参数:
<img class="ACF_Img" src="<?php the_field('sidebar-latest', $home_page_id); ?>">
所以它会在所有页面上工作,另外,记得像我的示例一样删除任何额外的 space。