【问题标题】:Pre-fill content, title and custom fields to new posts (with custom post-type) in Wordpress在 Wordpress 中为新帖子(带有自定义帖子类型)预填充内容、标题和自定义字段
【发布时间】:2017-06-23 17:44:30
【问题描述】:

每次我在 Wordpress 管理面板中按 ADD NEW 时,有没有办法预先填写新帖子(使用特定的自定义帖子类型)?

因为现在我使用带有预填充内容的帖子,每次我需要添加新帖子时,我都会复制这个旧帖子。但这并不像按 ADD NEW 并预先填充所有内容那样快。

也许有一种方法可以在我按下“添加新”后立即填充我想要的所有内容(文本、选择框、自定义字段...)?甚至在自定义字段中填写 POST_ID。

我尝试使用这个,但我只能填写自定义字段,也许有人可以帮助我扩大其限制并填写所有帖子:D。

function my_editor_content( $content, $post ) {
if ($post->post_type == property) {
$content = 'your content';
        }
        return $content;
    }

我什至使用这个来简化,但我需要每次都复制并粘贴相同的代码。而且我也不知道如何填写自定义字段并选择自定义分类。

add_filter( 'default_title', 'my_editor_title' );
function my_editor_title( $title ){
    $title = 'Default Title';
    return $title;
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果我理解正确的话。看看这些过滤器挂钩-wp_insert_post_data。在functions.php:

     if (isAdmin()){
    
        add_filter('wp_insert_post_data','custom_insert_post', '99', 2);
    
        function custom_insert_post($data, $postarr){ <-- 
            $data['post_title'] = 'default_post_title';
            $data['post_status'] = 'future'; 
            //.... etc            
    
    
        }
    
    
     }
    

    也看看那个钩子 - save_post。

     add_action('save_post', 'custom_insert_meta_values');
    
     function custom_insert_meta_values($post_ID){
       $default_meta_key = '....';
       $default_meta_value = '....';
    
       add_post_meta($post_ID, $default_meta_key, $default_meta_value);
     }
    

    【讨论】:

    • 不幸的是,这不允许将数据插入自定义字段。
    • @Diego 为什么不呢?只是,使用两个钩子。
    • 我会在星期一试试。谢谢!
    • 只是复制和粘贴您的代码不起作用。现在我正在阅读手册,看​​看有什么问题。很抱歉回复晚了,我正在使用新服务器(webmin),完全忘记了这个 wordpress“问题”。
    猜你喜欢
    • 2019-06-12
    • 2015-11-04
    • 2014-03-02
    • 2011-08-11
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多