• Resolved Brandon

    (@glassanemone)


    I would like to setup a custom field to define the color of the post title on a per post basis. I have already setup a custom field to define the background color on a per post basis by adding this line:

    style="background-color:<?php echo(get_post_meta($post->ID,
    'custom-bgcolor', true));?>;">

    Directly after this line in my content.php

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>

    I know next to nothing about this and everything I have done is the result of Google searches and trial and error.

    I did try adding the following lines to my content.php and it worked:

    <header id="entry-title a<?php the_ID(); ?>" <?php post_class(); ?>
    style="color:<?php echo(get_post_meta($post->ID,
    'custom-title', true));?>;">
    <header id="entry-title a:hover<?php the_ID(); ?>" <?php post_class(); ?>
    style="color:<?php echo(get_post_meta($post->ID,
    'custom-title-hover', true));?>;">

    But it also added extra padding to the top and bottom of all my posts. I assume this was the result of using header id.

    I’m using a child theme and my site is here

    For reference, here is a pastebin of my content.php as is with only the custom background.

    And here is a pastebin of the content.php that worked but added extra padding.

    Can anyone help me out or point me in the right direction?

    Thanks.

Viewing 1 replies (of 1 total)
  • Thread Starter Brandon

    (@glassanemone)

    I finally figured this out. I added a few lines to my child theme content.php. The summary is below:

    Replace this block of code:

    <?php if ( is_single() ) : ?>
    		<h1 class="entry-title"><?php the_title(); ?></h1>
    		<?php else : ?>
    		<h1 class="entry-title">
    			<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
    		</h1>

    with this block of code:

    <?php $title_color = get_post_meta($post->ID, 'title_color', true)?>
    		<?php if ( is_single() ) : ?>
    		<h1 class="entry-title" style="color: <?php echo $title_color ?>"><?php the_title(); ?></h1>
    		<?php else : ?>
    		<h1 class="entry-title">
    			<a href="<?php the_permalink(); ?>" rel="bookmark" style="color: <?php echo $title_color ?>"><?php the_title(); ?></a>
    		</h1>

    Then within the post you want to change the title color of:

    custom field name = title_color
    custom field value = color hex value

    I made similar changes to my page template to allow the same functionality.

Viewing 1 replies (of 1 total)
  • The topic ‘Use custom field to define post title color on a per post basis’ is closed to new replies.