【问题标题】:how to add more text below blog title in WordPress如何在WordPress中的博客标题下方添加更多文本
【发布时间】:2013-08-11 19:23:23
【问题描述】:

我想在我的博客标题下方添加一些文字,我研究了一天的大部分时间,但无法弄清楚如何去做,我不是 PHP 专家,但我对所有的地方都有一个基本的了解php 文件需要,并且我已经有一个子主题,我在子主题文件夹中使用 functions.php 文件构建了该子主题。 我的孩子主题是建立在 WordPress 21-11 之上的!

截图如下:

我正在尝试的网站是here

【问题讨论】:

  • 友情提示:您应该更改博客上的标题图片并尝试整体改进设计
  • 我的客户喜欢它原来的样子,所以除非他们要求,否则我不会更改任何东西!!!

标签: wordpress


【解决方案1】:

有一种方法可以在不安装插件的情况下添加这个简单的功能。

除非我读错了,否则您要做的就是添加一个新的元框,您可以在其中插入子标题并将其显示在该帖子上。

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
}

接下来要做的是创建保存功能。同样在 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;
}

【讨论】:

  • 感谢 Joe Buckle 的帮助,我没有 the_title();在我的 single.php 文件中。我的 content-single.php 文件中确实有它,我尝试添加您的代码 --->$subtitle = get_post_meta(get_the_ID(), 'sub_title', true); if(isset($subtitle)) { echo $subtitle; } 进去,它并没有给我一个新的副标题字段。我就是这样做的 --->

    对不起,我不知道如何使用这个论坛的 CODE 框:(
  • 这是一个屏幕截图供您查看 :) pctechsupporthelp.com/screenshots/child-theme-pages-ss.html 谢谢乔 :) 如果您需要其他任何内容,请告诉我。
  • 不用担心。您能否在添加我上面建议的内容时向我展示functions.php?您能否确认在编辑帖子时会出现一个额外的元框,您可以在其中添加子标题?
【解决方案2】:

我建议研究一个允许您添加副标题或类似内容的插件,这意味着您需要创建一个自定义字段,以便您可以在逐个帖子的标题下添加文本。我强烈推荐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(); ?>

并在其下方添加此内容。我怀疑这会是个问题,但要使辅助函数工作,它需要在循环内。

<?php echo rwmb_meta('subtitle'); ?>

【讨论】:

  • 哇这个插件太让我头疼了掌握如何使用它或把它放在哪里。***感谢 Rathkej 提供的信息,我会继续阅读、研究和研究这个!
  • 我继续更新我的答案以完成您的需求。一旦你了解它正在做什么,它真的不是那么糟糕的插件。我建议去了解它。
  • 再次感谢 Rathkej :)) 我完成了前半部分,安装了插件,创建了 custom-meta.php 文件并将其放入我的子主题文件夹并上传,将上面的代码添加到该文件中,添加了包含“custom-meta.php”;代码到我的子主题文件夹中的functions.php文件的底部,它破坏了我的网站,我在我的网站上看不到或做任何事情,关于我可能做错了什么或遗漏的任何建议?最好的问候,托尼
  • 是的,那是我的错,我在急于为您格式化时不小心删除了需要在其中的一行,但它已经更新了。再次尝试将那一大段代码复制并粘贴到您的 custom-meta.php 中。
  • 非常感谢您的帮助 Rathkej,我现在将其粘贴 :)
猜你喜欢
  • 2015-10-05
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2022-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多