我想在我的博客标题下面添加一些文本,我已经研究了一天的大部分时间,不知道怎么做,我不是PHP的专家,但我有一个所有的php文件需要的基本理解,我已经有一个子主题,我用我的孩子主题文件夹中的functions.php文件建立。我的孩子主题是建立在WordPress 21-11上的!
这是一个截图:

我尝试这样做的站点是here
发布于 2013-08-13 02:45:51
有一种方法可以在不安装插件的情况下添加这个简单的功能。
除非我读错了,否则你要做的就是添加一个新的元框,你可以在其中插入一个副标题,然后它就会显示在那个帖子上。
在functions.php,中,添加这个来创建一个元框来存放你的副标题字段
function your_sub_title() {
add_meta_box('your_sub_title_metabox', 'Edit Sub Title', 'your_sub_title_metabox', 'post', 'normal', 'default'); ## Adds a meta box to post type
}现在,也可以在functions.php中添加新字段的代码
function your_sub_title_metabox() {
global $post; ## global post object
wp_nonce_field( plugin_basename( __FILE__ ), 'your_sub_title_nonce' ); ## Create nonce
$subtitle = get_post_meta($post->ID, 'sub_title', true); ## Get the subtitle
?>
<p>
<label for="sub_title">Sub Title</label>
<input type="text" name="sub_title" id="sub_title" class="widefat" value="<?php if(isset($subtitle)) { echo $subtitle; } ?>" />
</p>
<?php
}你需要做的下一件事是创建你的save函数。也在functions.php中
function sub_title_save_meta($post_id, $post) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return false; ## Block if doing autosave
if ( !current_user_can( 'edit_post', $post->ID )) {
return $post->ID; ## Block if user doesn't have priv
}
if ( !wp_verify_nonce( $_POST['your_sub_title_nonce'], plugin_basename(__FILE__) )) {
} else {
if($_POST['sub_title']) {
update_post_meta($post->ID, 'sub_title', $_POST['sub_title']);
} else {
update_post_meta($post->ID, 'sub_title', '');
}
}
return false;
}
add_action('save_post', 'sub_title_save_meta', 1, 2);然后,在single.php模板中的the_title()下方
...
the_title();
$subtitle = get_post_meta(get_the_ID(), 'sub_title', true);
if(isset($subtitle)) {
echo $subtitle;
}发布于 2013-08-12 03:51:10
我建议研究一个插件,它允许你添加一个副标题,或者类似的东西,这意味着你需要创建一个自定义字段,这样你就可以在帖子的基础上在标题下添加文本。我强烈推荐Meta-Box。
要安装Meta-Box,你需要做的就是安装Meta-Box插件,就像你在仪表板上安装其他插件一样。完成后,在您的主题目录中创建一个名为"custom-meta.php“的新文件,并复制此代码。
<?php
$prefix = '';
global $meta_boxes;
$meta_boxes = array();
// Custom Post Info Meta Box
$meta_boxes[] = array(
'id' => 'custom_post_info',
'title' => __( 'Custom Post Info', 'rwmb' ),
'pages' => array( 'post', 'page' ),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
'fields' => array(
array(
'name' => __( 'Subtitle', 'rwmb' ),
'id' => "{$prefix}subtitle",
'desc' => __( 'Enter Subtitle Here', 'rwmb' ),
'type' => 'text',
'std' => __( '', 'rwmb' ),
'clone' => false,
),
),
);
/********************* META BOX REGISTERING ***********************/
/**
* Register meta boxes
*
* @return void
*/
function register_meta_boxes()
{
if ( !class_exists( 'RW_Meta_Box' ) )
return;
global $meta_boxes;
foreach ( $meta_boxes as $meta_box )
{
new RW_Meta_Box( $meta_box );
}
}
add_action( 'admin_init', 'register_meta_boxes' );然后更新您的函数文件,使其包含"custom-meta.php“文件,方法是在有意义的地方添加此行。
include 'custom-meta.php';然后返回到你的帖子中,检查是否有新的方框可以插入字幕。如果你这样做,那就太好了!
然后,在每个帖子上都有一个自定义字段后,您所需要做的就是将这些数据放入主题模板中。您将需要进入您的single.php文件,并查找这行代码。
<?php the_title(); ?>然后在它下面添加这个。我怀疑这会是一个问题,但要让helper函数工作,它需要在循环中。
<?php echo rwmb_meta('subtitle'); ?>https://stackoverflow.com/questions/18175791
复制相似问题