我有一个自定义插件,可以创建一个管理设置页面,包含3个字段:
帖子、页面ID和消息。
我试图抓取首页列表上显示的第一篇帖子,并将“消息”字段的内容添加到内容的开头。我是在一个插件中完成这项工作的,我不知道如何连接到主页帖子列表,在那里获取第一篇帖子,并在内容之前添加“消息”,然后将其返回。
我想这与pre\\u get\\u posts挂钩有关,但我就是想不出正确的方法。
以下是插件的完整PHP,一切正常,我现在无法找到获取第一篇帖子内容的正确方法:(
<?php
/*
Plugin Name: Custom HTML
Description: Custom HTML in Pages
Version: 1.0.0
*/
class Codeable_Fields_Plugin {
public function __construct() {
// Hook into the admin menu
add_action( \'admin_menu\', array( $this, \'create_plugin_settings_page\' ) );
// Add Settings and Fields
add_action( \'admin_init\', array( $this, \'setup_sections\' ) );
add_action( \'admin_init\', array( $this, \'setup_fields\' ) );
}
public function create_plugin_settings_page() {
// Add the menu item and page
$page_title = \'Custom HTML\';
$menu_title = \'Custom HTML\';
$capability = \'manage_options\';
$slug = \'codeable_fields\';
$callback = array( $this, \'plugin_settings_page_content\' );
$position = 100;
add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
}
public function plugin_settings_page_content() {?>
<div class="wrap">
<?php
if ( isset( $_GET[\'settings-updated\'] ) && $_GET[\'settings-updated\'] ){
$this->admin_notice();
} ?>
<form method="POST" action="options.php">
<?php
settings_fields( \'codeable_fields\' );
do_settings_sections( \'codeable_fields\' );
submit_button();
?>
</form>
</div> <?php
}
public function admin_notice() { ?>
<div class="notice notice-success is-dismissible">
<p>Your settings have been updated!</p>
</div><?php
}
public function setup_sections() {
add_settings_section( \'our_first_section\', \'Custom HTML\', array( $this, \'section_callback\' ), \'codeable_fields\' );
}
public function section_callback( $arguments ) {
switch( $arguments[\'id\'] ){
case \'our_first_section\':
echo \'\';
break;
}
}
public function setup_fields() {
$fields = array(
array(
\'uid\' => \'codeable_pages_field\',
\'label\' => \'Page IDs to Exclude\',
\'section\' => \'our_first_section\',
\'type\' => \'text\',
\'supplimental\' => \'Seperate by comma\',
),
array(
\'uid\' => \'codeable_posts_field\',
\'label\' => \'Post IDs to Exclude\',
\'section\' => \'our_first_section\',
\'type\' => \'text\',
\'supplimental\' => \'Seperate by comma\',
),
array(
\'uid\' => \'codeable_textarea\',
\'label\' => \'Enter Text here\',
\'section\' => \'our_first_section\',
\'type\' => \'textarea\',
)
);
foreach( $fields as $field ){
add_settings_field( $field[\'uid\'], $field[\'label\'], array( $this, \'field_callback\' ), \'codeable_fields\', $field[\'section\'], $field );
register_setting( \'codeable_fields\', $field[\'uid\'] );
}
}
public function field_callback( $arguments ) {
$value = get_option( $arguments[\'uid\'] );
if( ! $value ) {
$value = $arguments[\'default\'];
}
switch( $arguments[\'type\'] ){
case \'text\':
case \'password\':
case \'number\':
printf( \'<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />\', $arguments[\'uid\'], $arguments[\'type\'], $arguments[\'placeholder\'], $value );
break;
case \'textarea\':
printf( \'<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>\', $arguments[\'uid\'], $arguments[\'placeholder\'], $value );
break;
case \'select\':
case \'multiselect\':
if( ! empty ( $arguments[\'options\'] ) && is_array( $arguments[\'options\'] ) ){
$attributes = \'\';
$options_markup = \'\';
foreach( $arguments[\'options\'] as $key => $label ){
$options_markup .= sprintf( \'<option value="%s" %s>%s</option>\', $key, selected( $value[ array_search( $key, $value, true ) ], $key, false ), $label );
}
if( $arguments[\'type\'] === \'multiselect\' ){
$attributes = \' multiple="multiple" \';
}
printf( \'<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>\', $arguments[\'uid\'], $attributes, $options_markup );
}
break;
case \'radio\':
case \'checkbox\':
if( ! empty ( $arguments[\'options\'] ) && is_array( $arguments[\'options\'] ) ){
$options_markup = \'\';
$iterator = 0;
foreach( $arguments[\'options\'] as $key => $label ){
$iterator++;
$options_markup .= sprintf( \'<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>\', $arguments[\'uid\'], $arguments[\'type\'], $key, checked( $value[ array_search( $key, $value, true ) ], $key, false ), $label, $iterator );
}
printf( \'<fieldset>%s</fieldset>\', $options_markup );
}
break;
}
if( $helper = $arguments[\'helper\'] ){
printf( \'<span class="helper"> %s</span>\', $helper );
}
if( $supplimental = $arguments[\'supplimental\'] ){
printf( \'<p class="description">%s</p>\', $supplimental );
}
}
}
}
new Codeable_Fields_Plugin();
add_action (\'the_content\', \'add_to_header\');
function add_to_header(){
$postids = preg_split("/\\s*,\\s*/", get_option(\'codeable_posts_field\'));
$pageids = preg_split("/\\s*,\\s*/", get_option(\'codeable_pages_field\'));
if (is_single($postids )) {
$fullcontent = $content;
}
elseif (is_page($pageids)) {
$fullcontent = $content;
}
elseif (is_single() || is_page()) {
$beforecontent = get_option(\'codeable_textarea\');
$fullcontent = $beforecontent . $content;
}
return $fullcontent;
}
任何帮助都将不胜感激!!