Roy
Roy

Reputation: 745

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:

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.

Upvotes: 2

Views: 4488

Answers (2)

alexmcfarlane
alexmcfarlane

Reputation: 1148

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'
        )
    );
}

Upvotes: 0

transporter_room_3
transporter_room_3

Reputation: 2633

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'
            )
        );
    }

}

Upvotes: 2

Related Questions