skip to Main Content

I am working on a WordPress site where multiple posts are to be displayed in a single page. The title of each post is displayed with h2 tags. Now I want to be able to give each of these an id (html id attribute), preferably of my choice so I can link to them (use as anchor).

Link to my page is
https://www.risingidiots.com/photoshop-opening/

2

Answers


  1. Chosen as BEST ANSWER

    That's how I did it :)

    First of all in my case what I had to modify was a plug-in- file not a theme file.

    <!-- <?php 
    $title = the_title_attribute( 'echo=0' );
    $title2 = str_replace(' ', '_', $title);
    
    
    ?> -->
    <h2 class="entry-title"
     id="<?php echo $title2; ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    

    What I have done here is to use the title itself as an ID


  2. If you wanted to add id in h2 tag you need to change the code in page-blog.php file.

    there is two way to add the id.

    if you wanted to add different id for each post as you want then you can just add custom field in each and every post.

    then edit file and add the custom field value in h2 tag as ID.
    as below.

    <?php 
       $post_id = get_post_meta( get_the_ID(), 'post_id', true ); 
    ?>   
    <h2 id="<?php echo $post_id; ?>"><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
    

    another way is you just use post id as id for h2 tag.

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