我沒有使用 PHP 的經驗。我正在使用 ACF 選擇欄位為我的客戶提供在單個頁面、帖子和存檔級別選擇樣式表的選項。僅供參考,欄位為singular_css、portfolio_css 和careers_css,具體取決于內容型別,并共享這些下拉值:
/wp-content/themes/hello-theme-child-master/custom-css/white.css : White
/wp-content/themes/hello-theme-child-master/custom-css/black.css : Black
/wp-content/themes/hello-theme-child-master/custom-css/blue.css : Blue
/wp-content/themes/hello-theme-child-master/custom-css/tan.css : Tan
/wp-content/themes/hello-theme-child-master/custom-css/gray.css : Gray
functions.php 中的這個腳本似乎可以按預期作業,以控制在單個帖子/頁面上加載的樣式表,但不允許他們為自定義帖子型別檔案選擇樣式表:
/** Insert Dynamic Stylesheet Into <Head> using ACF Field **/
add_action( 'wp_head', 'add_head_script' );
function add_head_script() { ?>
<?php
$singular_css = get_field('singular_css');
if( $singular_css ): ?>
<link rel="stylesheet" href="<?php echo esc_url( $singular_css ); ?>">
<?php endif; ?>
<?php }
由于我無法以相同的方式控制自定義帖子存檔上的樣式表,因此我為它們創建了選項頁面:
/** Creates ACF Options Pages **/
if( function_exists('acf_add_options_page') ) {
acf_add_options_sub_page(array(
'page_title' => 'Portfolio Style',
'menu_title' => 'Portfolio Style',
'parent_slug' => 'edit.php?post_type=portfolio',
'capability' => 'manage_options'
));
acf_add_options_sub_page(array(
'page_title' => 'Careers Style',
'menu_title' => 'Careers Style',
'parent_slug' => 'edit.php?post_type=careers',
'capability' => 'manage_options'
));
}
并嘗試將樣式表排入佇列,但有些東西不起作用:
/** Enqueue Dynamic Stylesheet using ACF Field **/
function dynamic_style() {
$singular_css = get_field('singular_css');
$portfolio_css = get_field('portfolio_css', 'option');
$careers_css = get_field('careers_css', 'option');
if (is_singular()) {
wp_enqueue_style( 'singular_css', get_stylesheet_directory_uri(). $singular_css );
}
elseif (is_post_type_archive('portfolio')) {
wp_enqueue_style( 'portfolio_css', get_stylesheet_directory_uri(). $portfolio_css );
}
elseif (is_post_type_archive('careers')) {
wp_enqueue_style( 'careers_css', get_stylesheet_directory_uri(). $careers_css );
}
}
add_action( 'wp_enqueue_scripts', 'dynamic_style' );
我也試過這樣寫,但還是不行:
/** Enqueue Dynamic Stylesheet using ACF Field **/
function singular_style() {
$singular_css = get_field('singular_css');
if (is_singular()) {
wp_enqueue_style( 'singular_css', get_stylesheet_directory_uri(). $singular_css );
}
}
add_action( 'wp_enqueue_scripts', 'singular_style' );
function portfolio_style() {
$portfolio_css = get_field('portfolio_css', 'option');
if (is_post_type_archive('portfolio')) {
wp_enqueue_style( 'portfolio_css', get_stylesheet_directory_uri(). $portfolio_css );
}
}
add_action( 'wp_enqueue_scripts', 'portfolio_style' );
function careers_style() {
$careers_css = get_field('careers_css', 'option');
if (is_post_type_archive('careers')) {
wp_enqueue_style( 'careers_css', get_stylesheet_directory_uri(). $careers_css );
}
}
add_action( 'wp_enqueue_scripts', 'careers_style' );
uj5u.com熱心網友回復:
我建議您將下拉值更改為
/custom-css/white.css : White
/custom-css/black.css : Black
/custom-css/blue.css : Blue
/custom-css/tan.css : Tan
/custom-css/gray.css : Gray
然后在functions.php檔案中添加以下代碼
function dynamic_style()
{
if (is_singular()) {
global $post;
$cog_stylesheet = get_field('cog_background_color', $post->ID);
wp_enqueue_style('singular_css', get_stylesheet_directory_uri() . $cog_stylesheet);
}else {
global $post;
$post_type = get_current_post_types($post);
if ($post_type == 'portfolio') {
$portfolio_css = get_field('portfolio_css', 'option');
wp_enqueue_style('portfolio_css', get_stylesheet_directory_uri() . $portfolio_css);
} elseif ($post_type == 'careers') {
$careers_css = get_field('careers_css', 'option');
wp_enqueue_style('careers_css', get_stylesheet_directory_uri() . $careers_css);
}
}
}
add_action('wp_enqueue_scripts', 'dynamic_style');
但是,如果您仍想使用當前下拉值,請將以下代碼添加到 functions.php 檔案中
function dynamic_style()
{
if (is_singular()) {
global $post;
$cog_stylesheet = get_field('cog_background_color', $post->ID);
wp_enqueue_style('singular_css', home_url() . $cog_stylesheet);
} else {
global $post;
$post_type = get_current_post_types($post);
if ($post_type == 'portfolio') {
$portfolio_css = get_field('portfolio_css', 'option');
wp_enqueue_style('portfolio_css', home_url() . $portfolio_css);
} elseif ($post_type == 'careers') {
$careers_css = get_field('careers_css', 'option');
wp_enqueue_style('careers_css', home_url() . $careers_css);
}
}
}
add_action('wp_enqueue_scripts', 'dynamic_style');
并在functions.php上添加以下代碼,以便我們可以在存檔頁面上獲取post_type
function get_current_post_types($object = null)
{
// if a numeric value passed, assume it is a post ID
if (($object) && (is_numeric($object))) {
$object = get_post($object);
}
// if an object is passed, assume to be a post object
if (($object) && (is_object($object))) {
return get_post_type($object);
}
// standard single post type checks
if (is_404()) {
return '';
}
// update: removed this check, handled by is_singular
// if (is_single()) {return 'post';}
if (is_page()) {
return 'page';
}
if (is_attachment()) {
return 'attachment';
}
if (is_singular()) {
return get_post_type();
}
// if a custom query object was not passed, use $wp_query global
if ((!$object) || (!is_object($object))) {
global $wp_query;
$object = $wp_query;
}
if (!is_object($object)) {
return '';
} // should not fail
// if the post_type query var has been explicitly set
// (or implicitly set on the cpt via a has_archive redirect)
// ie. this is true for is_post_type_archive at least
// $vqueriedposttype = get_query_var('post_type'); // $wp_query only
if (property_exists($object, 'query_vars')) {
$posttype = $object->query_vars['post_type'];
if ($posttype) {
return $posttype;
}
}
uj5u.com熱心網友回復:
謝謝比杰!你幫了大忙。只是為別人回顧一下,這是我采取的步驟:
我注冊了這些選項頁面:
/** Creates ACF Options Pages **/
if( function_exists('acf_add_options_page') ) {
acf_add_options_sub_page(array(
'page_title' => 'Posts Style',
'menu_title' => 'Posts Stylesheet',
'parent_slug' => 'edit.php',
'capability' => 'manage_options'
));
acf_add_options_sub_page(array(
'page_title' => 'Portfolio Style',
'menu_title' => 'Portfolio Stylesheet',
'parent_slug' => 'edit.php?post_type=portfolio',
'capability' => 'manage_options'
));
acf_add_options_sub_page(array(
'page_title' => 'Careers Style',
'menu_title' => 'Careers Stylesheet',
'parent_slug' => 'edit.php?post_type=careers',
'capability' => 'manage_options'
));
}
我創建了我的 ACF 欄位:
- 奇異的_css
- 文章_css
- 投資組合_css
- 職業生涯_css
他們都共享這些下拉值:
- /custom-css/white.css : 白色
- /custom-css/black.css : 黑色
- /custom-css/blue.css : 藍色
- /custom-css/tan.css : 譚
- /custom-css/gray.css : 灰色
functions.php 檔案中的這段代碼有效:
/** Enqueue Dynamic Stylesheet using ACF Field **/
function dynamic_style()
{
if (is_singular()) {
global $post;
$singular_css = get_field('singular_css', $post->ID);
wp_enqueue_style('singular_css', get_stylesheet_directory_uri(). $singular_css);
} elseif (is_home()) {
$articles_css = get_field('articles_css', 'option');
wp_enqueue_style('articles_css', get_stylesheet_directory_uri(). $articles_css);
} elseif (is_post_type_archive('portfolio')) {
$portfolio_css = get_field('portfolio_css', 'option');
wp_enqueue_style('portfolio_css', get_stylesheet_directory_uri(). $portfolio_css);
} elseif (is_post_type_archive('careers')) {
$careers_css = get_field('careers_css', 'option');
wp_enqueue_style('careers_css', get_stylesheet_directory_uri(). $careers_css);
} elseif (is_404()) {
wp_enqueue_style('white_css', get_stylesheet_directory_uri(). '/custom-css/white.css');
}
}
add_action('wp_enqueue_scripts', 'dynamic_style', 99);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480618.html
標籤:php WordPress 高级自定义字段 元素 acfpro
上一篇:在WordPress中創建簡碼時如何回傳許多條件字串
下一篇:付款前從結帳頁面更新用戶元資料