Forums

Getting Post ID on Homepage (2 posts)

  1. Shifty88
    Member
    Posted 2 years ago #

    I'm having trouble getting a Post ID on the homepage. I have my wordpress configured to only show a single post on the home page, and I need the ID of that post to fetch the custom stylesheet for that particular post. The code works fine on a single page, but on the homepage I'm not sure whats going on...

    So how can I grab post meta data on the homepage from the latest post (in a certain category)??

    <?php
        $id = $wp_query->post->ID;
        $css_docs = get_post_meta($id, 'custom_css', false);
        $js_docs = get_post_meta($id, 'custom_javascript', false);
        if (!empty($css_docs))
        {
            echo '<!--Home Page Custom css for '.get_the_title().'.-->'."\n";
            foreach ($css_docs as $css)
            {
                        echo '<link rel="stylesheet" href="'.$css.'" type="text/css" media="screen" />'."\n";
                }
        }
        if (!empty($js_docs))
        {
            echo '<!--Custom javascript for '.get_the_title().'.-->'."\n";
            foreach ($js_docs as $js)
            {
                echo '<script src="'.$js.'" type="text/javascript"></script>'."\n";
            }
        }
        ?>
  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Assuming you don't use sticky posts, then this will show you the post ID for the 1st post in the $posts object.

    if ($posts) {
    echo 'post id for first post in array ' . $posts[0]->ID;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.