如何更改显示wordpress文章标题



我需要一个关于wordpress的技巧。我想替换我在整个网站上显示的帖子标题中的一个单词。(不永久或不在数据库中。仅在浏览器中显示临时。(例如:我想把文章单词改为文本这里列出了我的文章标题:

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

我该怎么做?

在您的函数中.pp:

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,对于每一个,您都可以用single.php中使用的特定函数更改标题转换器函数

您可以使用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
}

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

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;
}

通过这种方式,我的菜单保持正常,我可以更改我需要的内容。你可以根据自己的需要进行调整。

最新更新