WordPress.org

Support

Support » Plugins and Hacks » get ID of the page that is set as “home”

get ID of the page that is set as “home”

  • I use a script that extracts header images from the files attached to a post or page.
    Now, if none is set, I want to use the one from the front page.
    Not hard-coded in the functions.php.

    This is my header-image-extraction-function:

    function postheader() {
    	if ( $images = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'numberposts' => '-1',
    		'order' => 'DESC',
    		'orderby' => 'ID',
    		'post_mime_type' => 'image',)))
    	{
    		foreach( $images as $image ) {
    			$attachmenturl=wp_get_attachment_url($image->ID);
    			$attachmentimage=wp_get_attachment_image($image->ID, 'full' );
    			$img_title = $image->post_title;
    
    			if (stristr($img_title, '[header]')){
    				echo "<img src=$attachmenturl width=630 height=112>";
    			}
    		}
    	} else {
    		echo "No Image";
    	}
    }

    Instead of the “no image” bit, I want the same function to go through the home page, not the current id.

    Any Ideas?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Well, if you’re using a static home page, you would use the post ID specifically for the page that serves as the static home page. I’m not sure about the code exactly that you would want to use but that’s what you are aiming for, right?

    $frontpage_id = get_option('page_on_front');

    That help?

    @otto … that returns the url of the home page. Not the ID, that would allow me to check for attachments.

    @stonegauge … that would require me to manually encode the id somewhere. I’m trying to get the thing to be self-contained, so that when a new page is created and defined as home, the function still works.

    I also realized, I need to FIRST check for missing pics, THEN do the outputting. Right now there are 3 states
    attachments exist, with header image
    attachments exist, no header
    no attachments.

    That’s not ideal 🙂

    so in that case you need some type of destinction for the front page — perhaps a template reference, in which case you just need to make sure there is no template conflict between the front page and other pages/posts (meaning you use the template only for the front page, editing it on your merry but not using the template on any other page/post besides the front page)…

    At least that’s another thought of attack on this. Again I apologize because having ideas does nothing when it comes to the code and implementation.

    @otto … that returns the url of the home page. Not the ID, that would allow me to check for attachments.

    No, it doesn’t. It gives the ID number of the page/post that is the front page.

    If you’re getting back a URL then the front page would not even be working properly. WordPress itself assumes that that option is a page ID number. That’s how the whole thing works.

    hmmm … I tested the thing by echoing $frontpage_id and I got an url.
    Okay, will give it another go 🙂

    Thanks otto.

    Stonegauge … no need to apologize. Often one just needs a little push in the right direction.
    In this case however I need code :p

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get ID of the page that is set as “home”’ is closed to new replies.
Skip to toolbar