Custom title background in WordPress using custom fields

In my last WordPress implementation I had to show different backgrounds for the page title. One title said “Who we are” with a background of several people. Another page title said “Our services” and the background image was only one man and one girl engaging in a conversation. To achieve this I used the custom fields.

Basic styling

#main-content h1 { background:url(g/htxt.jpg) no-repeat; }

This would be the common background if we don’t apply the custom fields technique.

In the Write Page area

The job in Write Page is simple, I uploaded an image using “add and image” and copied the link. I added a custom field with key “fondo” and the link as the value.

Adding code to page.php

I found this line in page.php, the template file that WordPress uses to define the layout for pages


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

after the line, I added this


<?php $estilo = get_post_meta($post->ID, 'fondo', true); ?>

There’s one variable $estilo, that is assigned the value of the custom field “fondo”, according to the post ID. Now, I could’ve go and style everything using the variable, but what happens if someone creates a new page and forgets to add a custom background? in that case we would see the image broken. We need to check whether the variable has the value we need in order to proceed. If it doesn’t, the regular background from our stylesheet will show. If it does, we will style the <h1> tag. Let’s find the title tag and wrap it with the necessary code:


<?php if ($estilo != '') { ?>
<h1 style="background:url(<?php echo $estilo; ?>) no-repeat;">
<?php }else{ ?>
<h1>
<?php } ?>
<?php the_title(); ?>
Title Text
</h1>

Since $estilo has the full path that we copied from the media manager all we have to do is a simple echo. That’s it, of course, you should set up all the background relative options since the style applied will overwrite everything leaving in a blank state before applying the background style. If you had defined a color, define it again, if the background repeats or not, position, etc.


“Custom title background in WordPress using custom fields” received 11 comments! Add yours.

  1. Matt November 18th, 2008

    Excelente el theme del sitio, felicitaciones! me encanta y buen tutorial.

    Saludos

  2. admin November 18th, 2008

    Muchas gracias Matt, y mil gracias por el link en Twitter!
    Estuve husmeando en tu sitio y encontré las functions.php que ofreces. La shorten_text me va a servir muchísimo en mi proyecto actual.
    Gracias, saludos!

  3. Stanly November 25th, 2008

    WoW! It’s Great. I’ll certainly test it in one of my future projects.

  4. Intennaextido December 17th, 2008

    Хорошо пишете. Я бы конечно некоторые моменты оспорила, ну да ладно.

    Translated by Google T: Good writing. I would certainly challenged some points, well yes OK.

  5. admin December 17th, 2008

    Thanks Intenna, if you want to point something just do it my friend.

  6. diszk April 16th, 2009

    Nice work, thanks for the tutorial

  7. Online Shopping Centre May 5th, 2009

    Now this is what I call innovation in terms of coming up with original great looking backgrounds.

  8. Corey Chapman May 20th, 2010

    i love Online shopping because it is so convenient and it is just one click away.;-`

  9. Zoe Murphy September 13th, 2010

    i love to both shop online and offline, you can say that i am a shopaholic*’~

  10. get ex bf back March 11th, 2011

    [...] Custom title background in WordPress using custom fields | WordPress | ilovecolors [...]

  11. Brice September 14th, 2011

    I am definitely impressed along with your writing skills as well as with the page layout on your blog post. Is it a paid design or do you customize it yourself? In any case continue fantastic superior writing, it is unusual to discover a good web site just like this today..

Leave a comment