如何从下拉菜单中自动选择页面或post parent(ID) ?

function autoset_parent_dropdown() {
    global $pagenow;
    if($pagenow == 'post-new.php') {
        parent_dropdown( '477', '477', '0', null );
    }
}
add_action( 'admin_init', 'autoset_parent_dropdown' );

如何自动从下拉菜单中预先选择”页面”或”发布父代(ID)”?

在post-new.php上, 当创建新帖子时。

我正在尝试上面的代码, 但无法正常工作。它没有使用parent_dropdown函数选择任何父帖子/页面。

我提到了:https://developer.wordpress.org/reference/functions/parent_dropdown/

如果我做错了什么, 请纠正我。


#1


试试这个方法。在Wordpress 5.3.2上进行了测试, 但是应该在所有版本上都可以使用。

add_filter('default_content', 'assign_parent_to_new_post', 10, 2);
/**
 * @param string $post_content
 * @param WP_Post $post
 *
 * @return string
 */
function assign_parent_to_new_post($post_content, $post)
{
    if ($post->post_type != 'page') {
        return $post_content;
    }

    $post->post_parent = 7; //Parent post_id goes here
    wp_update_post($post);
    return $post_content;
}

它背后的想法很简单–挂在空白的帖子创建过程中的某个地方, 替换为post_parent, 然后将其推送到数据库。

来源:

https://www.srcmini02.com/63424.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37634.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?