如何更改显示的 WordPress 帖子标题

发布于 2025-01-14 23:20:17 字数 459 浏览 6 评论 0原文

我需要一个关于 wordpress 的技巧。 我想替换整个网站上显示的帖子标题中的一个单词。(不是永久或不在数据库中。仅在浏览器中临时显示。) 例如:我想将 post 单词更改为 text 这是我的帖子标题列表:

1- This is first post title
2- This is second post title
3- This is third post title
4- This is fourth post title

这是我的新帖子标题列表:

1- This is first text title
2- This is second text title
3- This is third text title
4- This is fourth text title

我该怎么做?

i need a trick about wordpress.
i want replace a word from my displaying post title at whole website.(not permantly or not on database. only show temporary at browser.)
for example: i want change post word to text
here is list my post titles:

1- This is first post title
2- This is second post title
3- This is third post title
4- This is fourth post title

here is my new post title list:

1- This is first text title
2- This is second text title
3- This is third text title
4- This is fourth text title

How can i do this?

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

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

发布评论

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

评论(3

不甘平庸 2025-01-21 23:20:17

在你的functions.php中:

function title_changer($title) {
    global $post;
    $title = str_replace('text','post',$post->post_title);  
    return $title;
}
add_action('the_title','title_changer');

转到主题路径中的single.php,

找到get_the_titlethe_title,对于每一个,你都可以使用中使用的特定函数更改title_changer函数single.php

in your functions.php:

function title_changer($title) {
    global $post;
    $title = str_replace('text','post',$post->post_title);  
    return $title;
}
add_action('the_title','title_changer');

go to your single.php in your theme path

find get_the_title or the_title, for each one you may change the title_changer function with the specific functions used in the single.php

梦在深巷 2025-01-21 23:20:17

您可以使用 Jquery 来完成,但是您必须根据 HTML 标签更改一些内容
h2.entry-title 这里你必须更改你的帖子标题CSS类,我希望标题中有一个锚标记,所以我要替换锚标记的HTML,请相应地更改并添加到你的functions.php中文件。

add_action( 'wp_footer', 'replace_title' );

function replace_title() { ?>

    <script>
    (function($) {

    jQuery('h2.entry-title').each(function(index, value) {

    let text = jQuery(this).find("a").html();
    let result = text.replace("post", "W3Schools");
    jQuery(this).find("a").html(result);
});

    })(jQuery);
    </script>

<?php
}

you can do using Jquery, but you've to change few things as according to your HTML tags
h2.entry-title HERE you've to change your post title CSS class and I'm hoping there is an anchor tag inside heading, so I'm replacing the HTML of anchor tag, Please change accordingly and add in your functions.php file.

add_action( 'wp_footer', 'replace_title' );

function replace_title() { ?>

    <script>
    (function($) {

    jQuery('h2.entry-title').each(function(index, value) {

    let text = jQuery(this).find("a").html();
    let result = text.replace("post", "W3Schools");
    jQuery(this).find("a").html(result);
});

    })(jQuery);
    </script>

<?php
}
白日梦 2025-01-21 23:20:17

这是我的方法,我不知道它有多好,但它有效。

function menuarray() {
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ "primary" ] ) && !is_admin()  ) {
        $menu = wp_get_nav_menu_object($locations[ "primary" ]);
        $menuitems = wp_get_nav_menu_items( $menu->term_id );
            $idCats = array_column($menuitems, 'title');
        return $idCats;
    }
}
global $menutem;
$menutem = menuarray();

首先,我在functions.php 中全局创建此函数,以便不在函数内重复它。
之后,我

add_filter('the_title', 'remove_header_metis', 10,2);
function remove_header_metis($title, $id) {

    global $menutem;
    if ( !is_admin() && ('post' == get_post_type($id) || 'page' == get_post_type($id) )  && !in_array($title, $menutem) ) {

            $title = ""; 
    
    }
    return $title;
}

以这种方式创建,我的菜单保持正常,我更改了我需要的内容。您可以根据需要对此进行调整。

This is my approach I don't know how good it is but it works.

function menuarray() {
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ "primary" ] ) && !is_admin()  ) {
        $menu = wp_get_nav_menu_object($locations[ "primary" ]);
        $menuitems = wp_get_nav_menu_items( $menu->term_id );
            $idCats = array_column($menuitems, 'title');
        return $idCats;
    }
}
global $menutem;
$menutem = menuarray();

First I create this function globally in functions.php to I don't repeat it inside the function.
After that I create

add_filter('the_title', 'remove_header_metis', 10,2);
function remove_header_metis($title, $id) {

    global $menutem;
    if ( !is_admin() && ('post' == get_post_type($id) || 'page' == get_post_type($id) )  && !in_array($title, $menutem) ) {

            $title = ""; 
    
    }
    return $title;
}

In this way, my menu stays ok and I change what I needed. You can tweak this for your needs.

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