• Resolved geezerd

    (@geezerd)


    I’m using a page background ‘trick’ to display an image as the page background, which stretches to always fit the browser view port.

    It requires this code just after the body tag:
    <div id="page-background"><img src="' . get_bloginfo('template_directory'). '/images/page-id-2.jpg" width="100%" height="100%" alt=""></div>
    And this in the stylesheet:
    #page-background {position:fixed; z-index:0; top:0; left:0; width:100%; height:100%;}

    But I want to dynamically change the image name based on what page you’re on, or if on a single post, display ‘postid’, both of which are spit out as a class for the body, since I’m using the body_class(); tag.

    So I need the php tag to dynamically change the image name
    <div id="page-background"><img src="' . get_bloginfo('template_directory'). '/images/' . what_tag_goes_here?(). '.jpg" width="100%" height="100%" alt=""></div>

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael

    (@alchymyth)

    have you tried:

    $post->ID

    i.e.

    <div id="page-background"><img src="' . get_bloginfo('template_directory'). '/images/page-id-' . $post->ID . '.jpg" width="100%" height="100%" alt=""></div>
    Thread Starter geezerd

    (@geezerd)

    Great! Thanks!

    For anyone else reading this, wondering how to do this groovy “hack”:

    <body <?php body_class(); ?>>
    
    <?php  if (is_page()) { echo '
    <div id="page-background"><img src="' . get_bloginfo('template_directory'). '/images/page-id-' . $post->ID . '.jpg" width="100%" height="100%" alt=""></div> ' ;} ?>
    
    <?php  if (is_single()) { echo '
    <div id="page-background"><img src="' . get_bloginfo('template_directory'). '/images/postid-' . $post->ID . '.jpg" width="100%" height="100%" alt=""></div> ' ;} ?>

    And of course you’ll need to make background images and title them “postid-23.jpg”, “page-id-5.jpg”, etc, and put them in your themes images folder, and put #page-background {position:fixed; z-index:0; top:0; left:0; width:100%; height:100%;} in your style.css.

    Thanks again, alchymyth!

    Thread Starter geezerd

    (@geezerd)

    Is there a way to have it also echo what is just before $post->ID ?
    So if it’s a page, it will return “page-id-44” and if it’s a post, will return “postid-77” ?

    That way I can have less code, I won’t need the if else.

    i don’t think your code could be much reduced – look at the way wordpress generates the body_class() – with a lot of conditional tags as well:
    ( /wp-includes/post-template.php; line 372++ )
    https://phpxref.ftwr.co.uk/wordpress/wp-includes/post-template.php.source.html#l379

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Need to echo page-id and postid from body_class results’ is closed to new replies.