skip to Main Content

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?

3

Answers


  1. 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

    Login or Signup to reply.
  2. 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
    }
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search