Forums

[resolved] Transform h2 tags to h3 tags on the index page (21 posts)

  1. Tekamolo
    Member
    Posted 11 months ago #

    Hi there,

    I am using the Imagination theme on my WordPress website. I've heard that from an SEO point of view it's best when a page has just one h1 tag. However, the titles of the posts on my site were in fact h1 tags on the index page, so I decided to re-write the index.php, transforming the h1 tags (the posts' titles) into h2 tags.

    All of that is fine, my problem is, I also have posts where I've got h2 tags within the content of the posts. Therefore, on the index page the posts' titles and the within-content h2 tags look the same, and are all h2 tags. I think this looks rather spammy.

    What I'd like to do is transform the h2 tags within the content of the posts to h3, but only on the index page. The ideal would be: The titles of the posts would be h2 on the index page, but h1 on the respective post pages and the h2 tags from within the content would be h3 on the index page, but h2 on the post page.

    I hope it's not too complicated. Thank you.

  2. Tekamolo
    Member
    Posted 11 months ago #

    This is a part of the code from my index.php:

    get_header(); ?>

    <?php if (have_posts()) : ?>

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

    <div <?php post_class('pngfix clearfix curved') ?> id="post-<?php the_ID(); ?>">
    <h2>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></h2>

    <div class="entry">
    <?php the_content('Continue Reading..'); ?>
    </div>

    I'm very noobish at php and don't undestand a single element of the code. But I suppose, the code above somehow makes the titles of the posts h2 tags, but only for the index page. All I want is change the h2 from within the content of the posts to h3, again, only for the index page.

    Thanks.

  3. Tekamolo
    Member
    Posted 11 months ago #

    Anyone?

  4. keesiemeijer
    moderator
    Posted 11 months ago #

    The titles of the posts would be h2 on the index page, but h1 on the respective post pages

    In single.php and page.php change the headings to H1.

    And put this in your theme's functions.php to change the post content:

    add_filter('the_content', 'change_heading');
    function change_heading($content){
    if(is_home()) {
    return preg_replace('/<h2(.*?)>(.*?)<\/h2>/','<h3\1>\2</h3>', $content);
    } else {
    return $content;
    }
  5. Tekamolo
    Member
    Posted 11 months ago #

    I made the change to the functions.php, but it ruined my site. I couldn't login to WordPress, nor could I view my website. However, I backed up the functions.php before making the change, and via Filezilla uploaded it back to the root directory. This made logging into my WP account possible. Also, I can now view my site, but my theme settings are gone.

    The day before, I backed up my WP database. Today, I uploaded it via phpMyAdmin. But that didn't change a thing, so when I'm logged in in my site's WP, I still get error messages at the top and at the bottom. The theme settings are lost, as I already said.

    Is there any way I can restore the site to how it looked before making the change to the functions.php?

  6. keesiemeijer
    moderator
    Posted 11 months ago #

    Can we have a link to your website. What theme are you using?

    The code works fine on my testserver.

  7. keesiemeijer
    moderator
    Posted 11 months ago #

    Oops I see I missed a bracket when I posted it here:

    add_filter('the_content', 'change_heading');
    function change_heading($content){
    if(!is_home()) {
    return preg_replace('/<h2(.*?)>(.*?)<\/h2>/','<h3\1>\2</h3>', $content);
    } else {
    return $content;
    }
    }
  8. Tekamolo
    Member
    Posted 11 months ago #

    I figured how to fix the problem I describe in my last post. There were two functions.php files, one was in a sub-folder of the theme directory, and when I uploaded the backed up functions.php, I overwrote the wrong file. Fortunately, before that I also backed the file I was overwriting, and now it's all solved out :).

    I use theme Imagination. It is literally written in the functions.php of the theme that any change made to the file will make the theme useless ("**** Changing the content of this file will render this theme useless. ****").

    So I think the missing bracket isn't the case here, since it seems like I can't change the file anyhow.

    The original problem with header tags still persists.

  9. keesiemeijer
    moderator
    Posted 11 months ago #

    Try putting the code (with the extra bracket) before: ob_start();

  10. Tekamolo
    Member
    Posted 11 months ago #

    Now with the extra bracket it works (post titles are h2 on the index page and h1 on the post pages). Thank you for that.

    Now if there is any way to transform the h2 tags from within the content of the posts to h3 on the index page (while remaining h2 on the post pages), I would be even more grateful :)

  11. keesiemeijer
    moderator
    Posted 11 months ago #

    Now with the extra bracket it works (post titles are h2 on the index page and h1 on the post pages). Thank you for that.

    Did you change that in single.php and page.php?

    Because the function changes all h2's (in the content) on pages other than the index page to h3's. (I thought that is what you wanted).
    try changing this: if(!is_home()) { to if(is_home()) {

  12. Tekamolo
    Member
    Posted 11 months ago #

    try changing this: if(!is_home()) { to if(is_home()) {

    Man you rock! Now, without the exclamation mark, it works exactly how I wanted. Thank you!

    Would it be possible to also change the within-content h3 to h4 on the index?

  13. keesiemeijer
    moderator
    Posted 11 months ago #

    Try this:

    add_filter('the_content', 'change_heading');
    function change_heading($content){
    if(is_home()) {
    $patterns = array('/<h2(.*?)>(.*?)<\/h2>/','/<h3(.*?)>(.*?)<\/h3>/');
    $replacements = array('<h3\1>\2</h3>','<h4\1>\2</h4>');
    return preg_replace($patterns, $replacements, $content);
    } else {
    return $content;
    }
    }
  14. Tekamolo
    Member
    Posted 11 months ago #

    It worked, except it also changed the h3 tags on the index page to h4, which was not what I wanted.

  15. keesiemeijer
    moderator
    Posted 11 months ago #

    try with this:

    $patterns = array('/<h3(.*?)>(.*?)<\/h3>/','/<h2(.*?)>(.*?)<\/h2>/');
    $replacements = array('<h4\1>\2</h4>','<h3\1>\2</h3>');
  16. Tekamolo
    Member
    Posted 11 months ago #

    Now it works like a charm! :)

    But I've got one final demand: I have a sticky post at the top of the index page, which is always at the top even after adding a new article. The theme I'm using doesn't allow me to have a h1 tag on the index page. Would it be possible to change the title of the sticky post on the index page (which is h2 at the moment), so that it were h1, with all the other header tags remaining as they are now?

  17. keesiemeijer
    moderator
    Posted 11 months ago #

    try with conditional tags in index.php inside the loop:

    if(is_sticky()) {
    <h1><?php the_title(); ?></h1>
    } else {
    <h2><?php the_title(); ?></h2>
    }
  18. Tekamolo
    Member
    Posted 11 months ago #

    Where exactly do I add the conditional tags inside the loop?

  19. keesiemeijer
    moderator
    Posted 11 months ago #

    change this in index.php :

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

    to:

    if(is_sticky()) {
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
    } else {
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    }
  20. keesiemeijer
    moderator
    Posted 11 months ago #

    Sorry that's wrong. Change it to:

    <?php if(is_sticky()) : ?>
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
    <?php else : ?>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <?php endif; ?>
  21. Tekamolo
    Member
    Posted 11 months ago #

    Now it all works to my satisfaction.

    Thank you very much, now I finally have an index page with the h1 tags :)

Reply

You must log in to post.

About this Topic