Changing the title of post with code

Am actually new to wordpress development. I am just trying to change title of the individual post..So i have used the function get_the_ID() to fetch a specific post.

I know the name of the title can be changed by a user manually,But i just want to weather this can be done with the code.

So i have tried adding a function in functions.php

function afunction( $post )
{
if ( empty ( $post->post_title ) )
    return;

$new_title = mb_convert_case( $post->post_title, MB_CASE_TITLE, "UTF-8" );

if ( $post->post_title === $new_title )
    return;

wp_update_post(
    array (
        'ID'         => $post->ID,
        'post_title' => $new_title
    )
);

// $post is passed by reference, so we update this property in real time
$post->post_title = $new_title;
}

So in my index.php the code looks like

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>




                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

                    <div class="single-post-content"><?php the_content(); ?></div>
                            <br><br>
<?php  echo afunction( $post ) ?>
                    <?php
                    $hon = array(
                        'post_title' => 'asad'
                        );
                 $name = get_the_ID();
                 if($name  == 19) {
                  $post->title = 'sfsdf';
                  echo $post->title;
                 }
                 ?>



            <?php endwhile; ?>

Thanx for your valuable help.

Read more here: Changing the title of post with code

Leave a Reply

Your email address will not be published. Required fields are marked *