我是创建Wordpress主题的新手,我有一个问题,我无法通过谷歌找到它。我想将内联样式更改为wp_add_inline_style函数。更改后,我的样式现在在帖子页面显示非常漂亮,但由于帖子ID,不要在首页中更改任何内容。我试图使用get_the_ID(),get_queried_object_id()并且没有任何反应。我该如何解决这个问题?
function metabox_inline_style() {
$id = get_the_ID();
if( 'quote' == get_post_format() || is_front_page() ) {
$quote_active = get_post_meta($id, 'q_active', true);
$quote_color = get_post_meta($id, 'q_color', true);
$quote_bgcolor = get_post_meta($id, 'q_bgcolor', true);
$font_size = get_post_meta($id, 'q_font', true);
$custom_metabox_css = "
.quote-style {
color: {$quote_color};
background-color: {$quote_bgcolor};
font-size: {$font_size};
}
.quote-style p {
color: {$quote_color};
}";
wp_add_inline_style( 'custom-style', $custom_metabox_css );
}
}
add_action( 'wp_enqueue_scripts', 'metabox_inline_style' );
更多信息。 此函数位于functions.php文件中。 我使用page-home.php和SiteOrigin页面构建器来创建首页。 我尝试过Global $ post和$ id = $ post-> ID,但它仍然不起作用。并且,我检查print_r($ id)。它在首页(ID = 43)和帖子页面(ID = 104)中不显示相同的ID。
答案 0 :(得分:0)
在我更正我的代码之后,最后我可以按照以下方式执行此操作。谢谢大家的慷慨建议。
function option_save_post($post_id) {
$myOptions = get_option('MyOptions');
$myOptions[$post_id]['q_color'] = get_post_meta($post_id, 'q_color', true);
$myOptions[$post_id]['q_bgcolor'] = get_post_meta($post_id, 'q_bgcolor', true);
$myOptions[$post_id]['q_font'] = get_post_meta($post_id, 'q_font', true);
update_option('MyOptions', $myOptions);
}
add_action( 'save_post', 'option_save_post', 10, 3 );
function metabox_inline_style($post_id) {
$myOptions = get_option('MyOptions');
foreach ( $myOptions as $myOption ) {
$custom_metabox_css = "
.quote-style {
color: {$myOption['q_color']};
background-color: {$myOption['q_bgcolor']};
font-size: {$myOption['q_font']};
}
.quote-style p {
color: {$myOption['q_color']};
}";
}
wp_add_inline_style( 'custom-style', $custom_metabox_css );
}
add_action( 'wp_enqueue_scripts', 'metabox_inline_style');