在WordPress 6.2.2中使用Twenty Twenty Three主题的自定义CSS下沉字母效果

huangapple go评论67阅读模式
英文:

Custom CSS drop caps in WordPress 6.2.2 with Twenty Twenty Three theme

问题

我正在尝试在WordPress 6.2.2的Twenty Twenty-Three主题中使用首字下沉效果。

所有我在谷歌上找到的文档都是针对旧版本的WordPress,可能是在旧主题上。以前很容易,但我找不到相关的文档来在Twenty Twenty-Three主题中实现这个效果。

接下来,我该如何添加自定义CSS以使用不同的字体来制作首字下沉效果?

我有一些旧版本WP的帖子,其中有首字下沉效果,我曾经通过一个子主题来自定义它们的样式,但我升级到Twenty Twenty Three主题后,所有自定义都消失了。

我通过"工具 > 主题文件编辑器"添加了以下代码,但似乎不起作用。

@import url('https://fonts.googleapis.com/css2?family=Fredericka+the+Great&display=swap');
p.has-drop-cap:not(:focus)::first-letter {
  font-family: 'Fredericka the Great', cursive;
}
英文:

I'm trying to do drop caps in the Twenty Twenty-Three theme on WordPress 6.2.2.

All the docs I find when I google it are for older versions of WordPress, and possibly on an older theme. It used to be easy, but I can't find relevant docs for how to do this with the Twenty Twenty-Three theme.

And following on from that, how do I add custom CSS to use a different font for the drop caps?

I have a couple of older posts from an earlier version of WP that have drop caps and I used to have them styled via a child theme I was using, but I upgraded to the Twenty Twenty Three theme and I lost all my customisations.

I've added the following code via the "Tools > Theme file editor", but it doesn't seem to be working.

p.has-drop-cap:not(:focus)::first-letter
{
  font-family: 'Fredericka the Great', cursive;
}

答案1

得分: 1

目前,TwentyTwentyTwoTwentyTwentyThree主题都不支持dropCaps。由于在某些用户系统上布局看起来不理想,已经达成共识,不需要为任何主题强制支持首字下沉。详细信息请阅读 - WordPress问题:https://github.com/WordPress/twentytwentytwo/issues/180

但是有一种解决方法可用。因为您需要修改主题核心文件,所以使用子主题可能是个不错的主意。否则,更新时可能会覆盖调整。

这个解决方法首次由GitHub用户@colorful-tones在这个评论中指出,在这个讨论串中。相关的CSS来自另一位GitHub用户@justintadlock,您可以在这里阅读更多

因此,以下是启用dropCap支持的步骤:

  1. 由于您正在使用主题文件编辑器,请进入那里并打开 theme.json

  2. 在大约第109行,在typography下,将dropCap的值从false更改为true

  3. 保存文件。

  4. 打开主题的 style.css 并添加:

    .has-drop-cap:not(:focus)::first-letter {
        font-family: var( --wp--custom--drop-cap--typography--font-family, inherit );
        font-size: var( --wp--custom--drop-cap--typography--font-size, 5.5em );
        font-weight: var( --wp--custom--drop-cap--typography--font-weight, 700 );
        font-style: var( --wp--custom--drop-cap--typography--font-style, normal );
        line-height: var( --wp--custom--drop-cap--typography--line-height, .85 );
        margin: var( --wp--custom--drop-cap--spacing--margin, 0.05em 0.1em 0 0 );
        padding: var( --wp--custom--drop-cap--spacing--paddig, 0 );
    }
    
  5. 保存文件。

**注意:**如果您想使用自定义字体,您可能需要theme.json 中的 typography 部分添加您的字体。WordPress.org的支持主题可能会有所帮助。您还可以尝试在CSS部分直接替换所有变量为您自己的值。但是抱歉,我记不清它是否可以这样工作,因为我只使用过这个解决方法一次,而且已经有一段时间了,页面不再存在。您只需自行测试。

最后,请不要忘记将您的 Fredericka the Great 字体正确包含到WordPress中。
希望这对您有所帮助。

英文:

Neither TwentyTwentyTwo nor TwentyTwentyThree currently support dropCaps. Since the layout looks undesirable on certain user systems, it was agreed that dropcap support is not mandatory for either theme. Read more - WordPress issues: https://github.com/WordPress/twentytwentytwo/issues/180

But there's a workaround available. Since you have to touch the theme core files, the use of a child theme is probably not a bad idea. Otherwise, adjustments could be overwritten when updating.

The workaround was first pointed out in a comment by @colorful-tones a user on GitHub, in this thread. The related CSS is from @justintadlock, another GitHub user, you can read more here.

So here are the steps you need to take to enable dropCap support:

  1. Since you are using the theme file editor, go there and open theme.json.
  2. At about line 109, under typography, change the value for dropCap from false to true.
  3. Save the file.
  4. Open the theme's style.css and add:
.has-drop-cap:not(:focus)::first-letter {
    font-family: var( --wp--custom--drop-cap--typography--font-family, inherit );
    font-size: var( --wp--custom--drop-cap--typography--font-size, 5.5em );
    font-weight: var( --wp--custom--drop-cap--typography--font-weight, 700 );
    font-style: var( --wp--custom--drop-cap--typography--font-style, normal );
    line-height: var( --wp--custom--drop-cap--typography--line-height, .85 );
    margin: var( --wp--custom--drop-cap--spacing--margin, 0.05em 0.1em 0 0 );
    padding: var( --wp--custom--drop-cap--spacing--paddig, 0 );
}
  1. Save the file.

NOTE: If you want to use a custom font you may have to add your font to the typography section in theme.json. A support topic from WordPress.org could be helpful hereby. You can also try to replace all variables directly in the CSS part with your own values. But I'm sorry I can't remember if it worked like that, because I used this workaround only once and it was some time ago and the page doesn't exist like that anymore. You'll just have to test it yourself.

Finally, don't forget to properly include your Fredericka the Great font into WordPress.
Hope this works for you.

huangapple
  • 本文由 发表于 2023年6月1日 17:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定