0

This question probably has simple answer, but I'm not an expert programmer so lemme explain my question chronologically.

I apply custom style for every single post, and it works using the following method. But I have trouble when I want to show my latest post on homepage (included its custom-style). The result I want to, you can see Trent Walton's Blog for reference.

This is my method for custom style on every post:

//Custom CSS Widget
add_action('admin_menu', 'custom_css_hooks');
add_action('save_post', 'save_custom_css');
add_action('wp_head','insert_custom_css');
function custom_css_hooks() {
    add_meta_box('custom_css', 'Custom Styles', 'custom_css_input', 'post', 'normal', 'high');
    add_meta_box('custom_css', 'Custom Styles', 'custom_css_input', 'page', 'normal', 'high');
}
function custom_css_input() {
    global $post;
    echo '';
    echo ''.get_post_meta($post->ID,'_custom_css',true).'';
}
function save_custom_css($post_id) {
    if (!wp_verify_nonce($_POST['custom_css_noncename'], 'custom-css')) return $post_id;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
    $custom_css = $_POST['custom_css'];
    update_post_meta($post_id, '_custom_css', $custom_css);
}
function insert_custom_css() {
    if (is_page() || is_single()) {
        if (have_posts()) : while (have_posts()) : the_post();
            echo 
                '
                    '.get_post_meta(get_the_ID(), '_custom_css', true).'
                ';
        endwhile; endif;
        rewind_posts();
    }
}

And I add some styles on custom field.

And my style in custom field this will be injected in header of single post. I works.

On homepage (by creating home.php) I put my latest post, with this:

     get_header();
     ...loop to show the same content as latest single..
     get_footer();

And my homepage shows my latest post as I want, but the problem is its custom style not included.

Then I try to modify my conditional tags (fro showing custom style): from

function insert_custom_css() {
    if (is_page() || is_single()) {
       ...
    }
}

to

function insert_custom_css() {
    if (is_home() || is_page() || is_single()) {
       ...
    }
}

The result is terrible, it was not showing custom style for the post that currently displayed. The home post is now styled with my first post custom style.

I'm looking for you assistance, thanks.

1

1 Answer 1

0

If you want to get post custom meta for the page assigned as the site front page, use get_option( 'page_on_front' ), which will return that page's ID.

i.e. change this:

get_post_meta(get_the_ID(), '_custom_css', true)

...to this:

get_post_meta( get_option( 'page_on_front' ), '_custom_css', true )

...on the site front page (is_front_page())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.