在wordpress中,在用作主页的特定页面中,我有一段HTML用于发布公告。我想让某人能够轻松地从仪表板自定义此部分,以及打开/关闭它。
要求:
只有这1条公告才会存在(无需显示多条)
在H3部分内,用户可以编辑文本,允许用户更改“了解更多”按钮文本,允许用户指定按钮URL初始方法:插件注册一个post\\u类型的“公告”添加一个元框以包含开/关开关、url和按钮文本使用帖子内容作为公告中的数据现在,问题开始了:
我正在做的事情是否会影响绩效没有一个完整的自定义插件,有没有更简单的方法因为我刚刚注册了一个自定义的短代码,所以我在前端注册了短代码这个代码会在没有短代码的页面上执行吗
My plugin code is below, or take a look at the complete code
Complete Plugin Code (https://pastebin.com/N5BgUSqa)
<section class="d-none mt-n7 p-0 position-relative mb-5" style="z-index:999;">
<div class="container">
<div class="d-block d-md-flex bg-dark-grad p-4 p-sm-5 all-text-white border-radius-3">
<div class="align-self-center text-center text-md-left">
<h3 class="mb-0">Placeholder For Special Offer or News (Shown When Activated)</h3>
</div>
<div class="mt-3 mt-md-0 text-center text-md-right ml-md-auto align-self-center">
<button class="btn btn-white mb-0">Learn more!</button>
</div>
</div>
</div>
</section>
我的插件自定义公告的相关部分。php
注册自定义帖子类型:
$args = array(
\'labels\' => $labels,
\'singular_label\' => __(\'Announcement\', \'custom-announcements\'),
\'public\' => true,
\'capability_type\' => \'post\',
\'rewrite\' => false,
\'supports\' => array(\'title\', \'editor\'),
);
register_post_type(\'announcements\', $args);
配置元数据库:
add_action( \'add_meta_boxes\', \'ca_add_metabox\' );
function ca_metabox( $post ) {
$values = get_post_custom( $post->ID );
$banner_enabled = isset( $values[\'ca_banner_enabled\'] ) ? esc_attr( $values[\'ca_banner_enabled\'][0] ) : \'\';
$button_url = isset( $values[\'ca_url\'] ) ? esc_attr( $values[\'ca_url\'][0] ) : \'\';
$button_title = isset( $values[\'ca_button_title\'] ) ? esc_attr( $values[\'ca_button_title\'][0] ) : \'\';
wp_nonce_field( \'ca_metabox_nonce\', \'metabox_nonce\' );
连接短代码以显示公告的功能:
function ca_announce(){
global $post;
$html = \'\';
$ca_a = new WP_Query( array(
\'post_type\' => \'announcements\',
\'posts_per_page\' => 1,
\'meta_query\' => array(
array(
\'key\' => \'ca_banner_enabled\',
\'value\' => \'yes\'
)
)
));
if( $ca_a->have_posts() ) : while( $ca_a->have_posts() ) : $ca_a->the_post();
$html .= \'<section class="mt-n7 p-0 position-relative mb-5" style="z-index:999;">\';
$html .= \'<div class="container">\';
$html .= \'<div class="d-block d-md-flex bg-dark-grad p-4 p-sm-5 all-text-white border-radius-3">\';
$html .= \'<div class="align-self-center text-center text-md-left">\';
$html .= \'<h3 class="mb-0">\' . get_the_content() . \'</h3>\';
$html .= \'</div><div class="mt-3 mt-md-0 text-center text-md-right ml-md-auto align-self-center"><button class="btn btn-white mb-0">\';
$html .= get_post_meta($ca_a->ID, \'ca_button_title\', true );
$html .= \'</button></div></div></div></section>\';
$html .= \'\';
$html .= \'\';
endwhile; endif;
return $html;
}
add_shortcode( \'ca\', \'ca_announce\' );