标签: 图片对齐
正在查看 2 条回复
-
作者帖子
-
-
2021年7月6日 下午10:20 #20719
好吧, 当我使用经典编辑器的时候,该代码有效:
add_action( ‘after_setup_theme’, ‘default_attachment_display_settings’ );
function default_attachment_display_settings() {
update_option( ‘image_default_align’, ‘left’ );
update_option( ‘image_default_link_type’, ‘none’ );
update_option( ‘image_default_size’, ‘full’ );
}但是,使用古登堡编辑器的时候,这个函数无效。
-
2021年7月6日 下午10:56 #20723
https://litepress.cn/plugins/qqworld-auto-save-images
我知道有个保存站外图片的插件可以做到自动居中对齐,上传图片不知道能不能,自己试试呗
-
2021年7月6日 下午11:03 #20728
古腾堡完全使用JS驱动,它有一套单独的基于JS的钩子体系,所以你的PHP代码失效是情理之中的。
首先在你主题的functions.php中加入以下代码以在网页中引入JS。
add_action( 'enqueue_block_editor_assets', function () { // JS文件保存路径,把/js改成你想保存到的主题目录下 $js_dir = get_stylesheet_directory_uri() . '/js'; wp_enqueue_script( 'custom-image-block', $js_dir . '/custom-image-block.js', array( 'wp-blocks', 'wp-dom' ), '', true ); }, 100 );
之后创建这个JS文件,并向其中写入以下代码:
wp.hooks.addFilter( 'blocks.registerBlockType', 'textdomain/change-image-default-alignment', function (settings, name) { if (name !== 'core/image') { return settings; } const newSettings = { ...settings, attributes: { ...settings.attributes, align: { type: 'string', default: 'center' }, }, }; return newSettings; }, );
-
-
作者帖子
正在查看 2 条回复
- 哎呀,回复话题必需登录。