【问题标题】:Custom title tag in custom post type wordpress自定义帖子类型wordpress中的自定义标题标签
【发布时间】:2014-04-29 22:51:20
【问题描述】:

我有一个问题:

我创建了一个自定义帖子类型。 我想要一个自定义标题标签(自定义帖子元)。

怎么做?

我尝试在网上关注教程和其他问题,但没有。

我也尝试过修改 header.php,但它显示“默认”wordpress

你能帮帮我吗?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    将自定义元框添加到自定义帖子类型。

    将此添加到您的 functions.php - 将 YOUR_CUSTOM_POST_TYPE 更改为您的自定义帖子类型

    function add_page_custom_meta_box() {  
    $boxTitle = 'Custom Title';
    $postType = 'YOUR_CUSTOM_POST_TYPE';
        add_meta_box(  
            'page_custom_meta_box', // $id  
            $boxTitle, // $title   
            'show_page_custom_meta_box', // $callback  
            $postType, // $post_type 
            'normal', // $context  
            'high'); // $priority  
    }  
    add_action('add_meta_boxes', 'add_page_custom_meta_box'); 
    $prefix = 'custom_';  
    $custom_meta_fields_page = array(
    array(  
        'label'  => 'Custom Title',  
        'desc'  => 'Custom Title of the Page.',  
        'id'    => $prefix.'title',  
        'type'  => 'text'  
    ),  
    );  
    function show_page_custom_meta_box() {  
    global $custom_meta_fields_page, $post;  
    // Use nonce for verification  
    echo '<input type="hidden" name="page_custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
          echo '<table class="form-table">';
        // Begin the field table and loop    
        foreach ($custom_meta_fields_page as $field) {  
            // get value of this field if it exists for this post  
            $meta = get_post_meta($post->ID, $field['id'], true);  
            // begin a table row with  
            echo '<tr> 
                    <th>
                    <style>label[for=custom_link-text-to]{color:#00bbe2; text-transform:uppercase;}</style>
                    <label for="'.$field['id'].'">'.$field['label'].'</label></th> 
                    <td>';  
                    switch($field['type']) {
    case 'text':  
        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> 
            <br /><span class="description">'.$field['desc'].'</span>';  
    break;  
    } //end switch  
            echo '</td></tr>';  
        } // end foreach  
        echo '</table>'; // end table  
    }
    
      function save_page_custom_meta($post_id) {  
        global $custom_meta_fields_page;  
    
        // verify nonce  
        if (!wp_verify_nonce($_POST['page_custom_meta_box_nonce'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if ('page' == $_POST['post_type']) {  
            if (!current_user_can('edit_page', $post_id))  
                return $post_id;  
            } elseif (!current_user_can('edit_post', $post_id)) {  
                return $post_id;  
        }  
    
        // loop through fields and save the data  
        foreach ($custom_meta_fields_page as $field) {  
            $old = get_post_meta($post_id, $field['id'], true);  
            $new = $_POST[$field['id']];  
            if ($field['type'] == 'tripple_repeatable')
                $new = array_values($new);
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field['id'], $new);  
            } elseif ('' == $new && $old) {  
                delete_post_meta($post_id, $field['id'], $old);  
            }  
        } // end foreach  
    }  
    add_action('save_post', 'save_page_custom_meta');
    

    然后你希望标题显示在哪里:

    <?php if ($custom_title = get_post_meta($post->ID, 'custom_title', true)){echo $custom_title;}?>
    

    【讨论】:

      猜你喜欢
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多