get ID of the page that is set as "home" (7 posts)

  1. mores
    Posted 7 years ago #

    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 ) {
    			$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?

  2. stonegauge
    Posted 7 years ago #

    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?

  3. $frontpage_id = get_option('page_on_front');

    That help?

  4. mores
    Posted 7 years ago #

    @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 :)

  5. stonegauge
    Posted 7 years ago #

    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.

  6. @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.

  7. mores
    Posted 7 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic