0

好的,我正在使用高级自定义字段插件为各种类别创建自定义字段。我已经在网上搜索了几个小时的解决方案,以便将它们实际集成到模板 php 中以显示。我将它应用到archive-product.php基于当前类别的自定义字段中调用和拉取的页面。

这是我用来拉取名为的自定义字段的代码slide_title_1

<?php the_field("slide_title_1", $category_id); ?>

我是否需要在其他地方添加代码或变量才能使其正常工作?

非常感谢任何帮助。先感谢您,

4

1 回答 1

0

我怀疑这不起作用,因为您尚未定义 $category_id。

请执行下列操作:

<?php
// Your code is in an archive template file so make sure a category is shown.
if ( is_product_category() ) {
    // Get the category ID.
    $category_id = get_query_var( 'product_cat' );

    // Output the custom field.
    the_field( 'slide_title_1', 'product_cat_' . $category_id );
} ?>

此外,模板文件将用于名为“产品”的自定义帖子类型。您是否知道内置类别和自定义分类之间的区别?

更新:由于您使用的是 WooCommerce 产品类别,因此我更新了代码。

于 2014-02-19T21:38:29.373 回答