2014-07-14 38 views
1

我目前使用的是Wordpress的Multi Post Thumbnails插件,但我只想讓插件提供的額外縮略圖顯示在一個特定頁面上。這個插件似乎並不支持這個功能,但它似乎是一件很容易添加的東西,我只是不確定正確的方式去做,因爲我對Wordpress開發相當陌生。你如何針對Wordpress functions.php中的特定頁面?

多縮略圖後的代碼如下,它只是進去的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' 
     ) 
    ); 
} 

在我看來,這純粹是在檢查該包裹的一個簡單的例子,使其只運行一個特定的頁面ID,但我不太確定如何去做這件事。

回答

1

這可能有點黑客。據我所知,帖子/頁面ID不能從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' 
      ) 
     ); 
    } 

} 
+0

我覺得你可以做'if(is_page(3))'。這樣做可能會更容易。 – henrywright

+0

謝謝你,我沒有完全使用代碼,但它使我沿着正確的道路。不確定這是做到這一點的「正確」方式,但是@henrywright的想法不起作用,不幸的是,這個做了一些修改。 – Roy

+0

郵政和頁面ID可用於功能。你只需要在你的函數中用'global $ post;'來調用它,你就可以獲得當前帖子或者頁面中所有可以使用該函數的postdata。如果你只是在id之後,聰明的方法就是使用'get_queried_object_id()'。如果你需要postdata(沒有內容),聰明的方法是使用'get_queried_object()':-) –

0

這也可能是一個黑客,但它爲我工作。一旦圖像被選中,我就會被AJAX'post_id'絆倒回管理頁面。我的用法是爲一個slu but子彈,但功能可以很容易地修改爲一個職位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' 
     ) 
    ); 
} 
相關問題