外贸建站资讯分享

Advanced Custom Fields插件在文章页面 分类页面调用教程

阅读数:2415 时间:30/04/2022 来源:解决方案技术分享 标签:

您所在的位置:

前台获取相应自定义字段的内容的话需要用到

<span class=”token php”><span class=”token delimiter”>&lt;?php</span> <span class=”token function”>the_
$ field_name:要检索的字段的名称(就是上面让填写英文名称的那个)。例如“page_content” (必填)

$ post_id:输入值的特定帖子ID。默认为当前帖子ID不需要(比如在循环获取文章中以及文章页中不需要填写)。这也可以是选项/分类/用户等

其他地方显示的值

$post_id = null; // current post
$post_id = 1; // post 1
$post_id = “option”; // options page
$post_id = “options”; // same as above
$post_id = “category_2”; // target a specific category
$post_id = “event_3”; // target a specific taxonomy (this tax is called “event”)
$post_id = “user_1”; // target a specific user (user id = 1)

the_field( “text_field”, $post_id );
比如你要获取你在分类ID为1创建的字段“_fengmiantu”,那么你就可以这样写:

<?php the_field(‘_fengmiantu’, ‘category_1’); ?>
如果在分类列表页,可以使用获取ID的方法,自动获取不同分类对应的字段,可以这样写;

<?php
$thiscat = get_category($cat);
?>
<?php the_field(‘你的字段名称’,$thiscat);?>
如果你要获取图片字段,首先创建字段的时候,选择图像URL,如下图:

 

 

然后前台用下面的方法获取图片:

<img src=”<?php%20the_field($field_name,%20$post_id);%20?>”>
自定义文章类型如何使用字段
1、自定义文章类型分类页获取分类自定义字段的方法:

<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
?>
/**在你需要获取字段的地方使用下面的代码即可**/
<?php the_field(‘_fmt’,$taxonomy . ‘_’ . $term_id);?> /**只需要将_fmt修改成你的字段名即可**/
2、自定义文章类型文章页获取该自定义分类的字段

<?php
$term_list = wp_get_post_terms($post->ID, ‘product_category’, array(“fields” => “all”));/**product_category是你自定义分类法的名字**/
$term_id = $term_list[0]->term_taxonomy_id ;
?>
/**在你需要获取字段的地方使用下面的代码即可**/
<?php the_field(‘_fmt’,product_category. ‘_’ . $term_id);?> /**只需要将_fmt修改成你的字段名即可(product_category是你自定义分类法的名字,根据你的情况替换修改)**/

 


关注我们
TOP
关闭

网站需求

    您的公司*

    您的姓名*

    您的手机*

    您的需求