将 WordPress 支持的网站中的所有标题转为“大写”案件

发布于 2024-09-05 11:32:30 字数 240 浏览 3 评论 0原文

所以我需要的是关于如何将 wordpress 支持的网站上的所有标题转换为大写字母的查询或一些提示。

我现在拥有的是这样的:

AAAAA BBBBB CCCCC

我希望它是这样的:

Aaaaa Bbbbb Ccccc

我确实尝试在这里谷歌搜索和搜索,但在该任务中失败了,所以非常感谢任何帮助!

更新:

我需要更新数据库内的标题。只是要明确一点。 :)

So what i need is a query or some tip on how to turn all titles on a wordpress powered website into capitalized case.

What i have now is something like this:

AAAAA BBBBB CCCCC

I want it to be like this:

Aaaaa Bbbbb Ccccc

I did try googling and searching here, but have failed at that task so any help is much appreciated!

UPDATE:

I need to update titles inside the database. Just to be clear. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

摘星┃星的人 2024-09-12 11:32:30

MySQL 中没有用于此目的的函数,但您可以创建如下函数:

DROP FUNCTION IF EXISTS proper;
SET GLOBAL  log_bin_trust_function_creators=TRUE;
DELIMITER |
CREATE FUNCTION proper( str VARCHAR(128) )
RETURNS VARCHAR(128)
BEGIN
  DECLARE c CHAR(1);
  DECLARE s VARCHAR(128);
  DECLARE i INT DEFAULT 1;
  DECLARE BOOL INT DEFAULT 1;
  DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';
  SET s = LCASE( str );
  WHILE i < LENGTH( str ) DO 
    BEGIN
      SET c = SUBSTRING( s, i, 1 );
      IF LOCATE( c, punct ) > 0 THEN
        SET BOOL = 1;
      ELSEIF BOOL=1 THEN 
        BEGIN
          IF c >= 'a' AND c <= 'z' THEN 
            BEGIN
              SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
              SET BOOL = 0;
            END;
          ELSEIF c >= '0' AND c <= '9' THEN
            SET BOOL = 0;
          END IF;
        END;
      END IF;
      SET i = i+1;
    END;
  END WHILE;
  RETURN s;
END;
|
DELIMITER ;

From 这里

您可以通过运行轻松更新:

Update wp_posts
Set post_title = proper(post_title)

There is no function in MySQL for this, but you can create one like this:

DROP FUNCTION IF EXISTS proper;
SET GLOBAL  log_bin_trust_function_creators=TRUE;
DELIMITER |
CREATE FUNCTION proper( str VARCHAR(128) )
RETURNS VARCHAR(128)
BEGIN
  DECLARE c CHAR(1);
  DECLARE s VARCHAR(128);
  DECLARE i INT DEFAULT 1;
  DECLARE BOOL INT DEFAULT 1;
  DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';
  SET s = LCASE( str );
  WHILE i < LENGTH( str ) DO 
    BEGIN
      SET c = SUBSTRING( s, i, 1 );
      IF LOCATE( c, punct ) > 0 THEN
        SET BOOL = 1;
      ELSEIF BOOL=1 THEN 
        BEGIN
          IF c >= 'a' AND c <= 'z' THEN 
            BEGIN
              SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
              SET BOOL = 0;
            END;
          ELSEIF c >= '0' AND c <= '9' THEN
            SET BOOL = 0;
          END IF;
        END;
      END IF;
      SET i = i+1;
    END;
  END WHILE;
  RETURN s;
END;
|
DELIMITER ;

From here.

The you can update easily by running:

Update wp_posts
Set post_title = proper(post_title)
此岸叶落 2024-09-12 11:32:30

您可以将其按原样保留在数据库中,并通过 css 强制标题以大写字母显示:

h2 {
  text-transform:capitalize;
}

You could leave it as-is in the database and force the title to display with first-letters in capitals via css:

h2 {
  text-transform:capitalize;
}
掩于岁月 2024-09-12 11:32:30

只需单独使用 ucwords( $title ) - 但要仔细检查您的用例 - 首字母缩略词最终不会像您的用户期望的那样显示。

“TLAs 初学者指南”将变为“TLA 初学者指南”

Just use ucwords( $title ) on their own - but check your use cases very carefully - acronyms do not end up displaying as your users might expect.

"beginners guide to TLAs" will become "Beginners Guide To Tlas"

哆啦不做梦 2024-09-12 11:32:30

您可以尝试使用 ucwordsstrtolower

<?php echo ucwords(strtolower(the_title(null, null, false))); ?>

据我所知,这需要标题值使用 strtolower 将其转换为小写,然后使用 ucwords 将每个单词大写。

我自己没有尝试过,所以我不知道它是否有效,但这就是我尝试的方法。

希望这有助于

编辑:对,我查看了我的一个旧文件,在你的functions.php中,你可以定义一个函数来挂钩save_post操作。使用 post 变量,您应该能够调整该数据,但就像其他人所说的那样,您必须小心,以防它不会产生所需的效果。

add_action('save_post', 'save_postdata');
function save_postdata($post_id) {
    //edit code here
    update_post_meta($post_id, 'title', $title);
}

我正在使用 update_post_meta() 函数,我不完全确定这是否有能够编辑标题,不幸的是我无法运行测试。

你们觉得怎么样?

could you try wrapping wordpresses the_title(); function with ucwords and strtolower

<?php echo ucwords(strtolower(the_title(null, null, false))); ?>

From what i can gather this takes the title value uses strtolower to turn it into lowercase, then ucwords to capitalize each word.

I haven't tried this myself so i don't know it it works but this is how i would try it.

Hope this helps

EDIT: right i've had a look at one of my old files, in your functions.php you could define a function to hook into the save_post action. Using the post variable you should be able to adjust that data, but like others have said you have to be careful incase it doesnt produce the desired effect.

add_action('save_post', 'save_postdata');
function save_postdata($post_id) {
    //edit code here
    update_post_meta($post_id, 'title', $title);
}

I'm using the update_post_meta() function, i'm not entirely sure if this has the ability to edit the title, i don't have to ability to run a test unfortunately.

What do you guys think?

始终不够爱げ你 2024-09-12 11:32:30

根据记录,当最后一个字符单独存在时,上面引用的 MySQL 函数会失败,例如,

Public John q

如果不需要填充,有一个简单的解决方法:

trim(proper(concat(myfield, '    ')))

For the record, the MySQL function quoted above fails when the last character stands alone, e.g.,

Public John q

There's an easy workaround, if you don't need padding:

trim(proper(concat(myfield, '    ')))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文