如何加载自定义 single.php? [英] how to load custom single.php?

查看:17
本文介绍了如何加载自定义 single.php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个博客,其中的类别是新闻、事件和灵感.这些类别是使用 acf 上的单选按钮选择的.我想在 single-news.php 上加载新闻,而在 single-events.php 上加载事件,在 single-inspiration.php 上加载灵感

Hi I have a blog where the categories are news, events and inspiration. These categories are selected using radio buttons on acf. I want to load news on single-news.php while events on single-events.php and inspiration on single-inspiration.php

是否可以使用 acf 字段来执行此操作?我遇到了这个带有 wordpress 默认类别的代码来加载自定义 single.php

is it possible to use the acf fields to do this? I have came across this code with wordpress default categories to load custom single.php

$post = $wp_query->post;
if (in_category('1')) {
    include(TEMPLATEPATH.'/single1.php');
} elseif (in_category('2')) {
    include(TEMPLATEPATH.'/single2.php');
} else {
    include(TEMPLATEPATH.'/single_default.php');
}

在使用 acf 时可以用什么来代替‘in_category’?

what can be used instead of the ‘in_category’ while using acf?

谢谢各位.

推荐答案

假设你的 acf 字段被称为 acf_category 你可以这样做..

assuming your acf field is called acf_category you could do this..

$post = $wp_query->post;

$post_cat = get_field('acf_category', $post->ID);
switch($post_cat){
  case "news":
    include(TEMPLATEPATH.'/single-news.php');
    break;
  case "events":
    include(TEMPLATEPATH.'/single-events.php');
    break;
  case "inspiration":
    include(TEMPLATEPATH.'/single-inspriation.php');
    break;
  default:
    include(TEMPLATEPATH.'/single.php');

}

请注意,您可能需要更改 switch 情况(也可以使用 if/else 来完成)以反映您的 acf 的实际 slug,这些是只是猜测.重要的部分是 get_field() 函数调用,它将为给定的帖子 id 拉取适当的 acf 字段.这里是所有 acf 函数的列表,有据可查

note that you will probably have to change the switch cases (this can also be done with if/else) to reflect the actual slugs of your acf, these are just guesses. The important part is the get_field() function call, which will pull the appropriate acf field for the post id given. Here is a list of all the acf functions, very well documented

这篇关于如何加载自定义 single.php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆