How do you target a specific page in WordPress functions.php?

I am currently using the Multi Post Thumbnails plugin for WordPress, but I only want the extra thumbnails provided by the plugin to show on one specific page. The plugin does not appear to natively support this functionality but it seems like something that would be pretty easy to add, I’m just not sure of the right way to go about it as I’m fairly new to WordPress development.

The code for Multi Post Thumbnails is the following, which simply goes in functions.php:

Read More
if (class_exists('MultiPostThumbnails')) {
    new MultiPostThumbnails(
        array(
            'label' => 'Secondary Image',
            'id' => 'secondary-image',
            'post_type' => 'page'
        )
    );

    new MultiPostThumbnails(
        array(
            'label' => 'Tertiary Image',
            'id' => 'tertiary-image',
            'post_type' => 'page'
        )
    );
}

It seems to me it would just be a simple case of wrapping this in a check so that it only runs for a specific page ID, but I’m not quite sure how to go about doing that.

Related posts

Leave a Reply

2 comments

  1. This is probably somewhat of a hack. To my knowledge post/page id’s are not accessible from inside functions.php.

    // get the id of the post/page based on the request uri.
    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $post_id = url_to_postid($url);
    
    // the id of the specific page/post.
    $specific_post_id = 3; 
    
    // check if the requested post id is identical to the specific post id.
    if ($post_id == $specific_post_id) {
    
        if (class_exists('MultiPostThumbnails')) {
            new MultiPostThumbnails(
                array(
                    'label' => 'Secondary Image',
                    'id' => 'secondary-image',
                    'post_type' => 'page'
                )
            );
    
            new MultiPostThumbnails(
                array(
                    'label' => 'Tertiary Image',
                    'id' => 'tertiary-image',
                    'post_type' => 'page'
                )
            );
        }
    
    }
    
  2. This is also probably a hack but it worked for me. I got stung by the AJAX ‘post_id’ back to the admin page once the image has been selected. My usage was for a slug but the function could easily be modified for a post ID.

    function is_admin_edit_page( $slug ){
    
        if( ( isset($_GET) && isset($_GET['post']) ) || ( isset($_POST) && isset($_POST['post_id']) ) )
        {
            $post_id = 0;
    
            if(isset($_GET) && isset($_GET['post']))
            {
                $post_id = $_GET['post'];
            }
            else if(isset($_POST) && isset($_POST['post_id']))
            {
                $post_id = $_POST['post_id'];
            }
    
            if($post_id != 0)
            {
                $c_post = get_post($post_id);
    
                if( $c_post->post_name == $slug )
                {
                    return true;
                }
            }
        }
    
        return false;
    }
    
    if( is_admin_edit_page('work') ) {
        new MultiPostThumbnails(
            array(
                'label' => 'Hero 1 (2048px x 756px JPEG)',
                'id' => 'am-hero-1',
                'post_type' => 'page'
            )
        );
    }