2014-01-30 43 views
0

您可以在wordpress中將自定義的帖子類型帖子設置爲靜態主頁嗎?你可以在wordpress中將自定義文章類型文章設置爲靜態主頁嗎?

在閱讀設置中,在Front Page Display下,我可以選擇使用靜態頁面。

我可以使用一篇文章作爲我的主頁嗎?

我正在運行多站點,並希望每個站點的主頁都是在「代理」自定義帖子類型下創建的帖子。他們只能有一個職位,這將成爲他們的主頁。

雖然我似乎無法獲得帖子作爲靜態主頁。

有什麼建議嗎?

回答

1

首先創建自定義模板,

而在你的模板,查詢您的CPT(自定義文章類型),

現在,wp-admin創建頁面,並指定你創建的模板。

現在去設置 - >>閱讀 - >您在wp-admin創建選擇頁面...

我希望你得到它..

+0

這工作完美。非常感謝。 – 4ndy

0

是的,我們可以設置自定義後類型(任何)作爲您網站的主頁, 步驟:

1)創建自定義帖子類型爲例如。 LANDING_PAGE

2)新增職位,只要你喜歡(首頁/服務/約/任何...)

創建自定義下拉領域使用創建theme_option.php在WordPress主題定製「選擇主頁」和寫這個代碼 -

<?php 
     if (class_exists('WP_Customize_Control') && ! class_exists('theme_name_Customize_Misc_Control')) : 
      class theme_name_Customize_Misc_Control extends WP_Customize_Control { 
      public function render_content() { 
      global $post; 
       switch ($this->type) { 
        default: 
        case 'description' : 
         echo ' 
         <p class="description">' . $this->description . '</p>'; 
        break; 

        case 'heading': 
         echo '<h3>' . esc_html($this->label) . '</h3>'; 
        break; 

        case 'line' : 
         echo '<hr />'; 
        break; 
       } 
      } 
     } 
     endif; 


     // Create your custom panel in your theme using customize_register function 
     function theme_name_logo_settings($wp_customize) { 

      $wp_customize->add_section('theme_name_theme_panel', array(
      'title' => 'theme_name Theme Global Setting', 
      'description' => '', 
      'priority' => 120, 
      )); 

     } 
     add_action('customize_register', 'theme_name_logo_settings'); 


    // Creating dropdown 
    if(class_exists('WP_Customize_Control')): 
     class WP_Customize_Latest_Post_Control extends WP_Customize_Control { 
      public $type = 'latest_post_dropdown'; 
      public $post_type = 'custom_post_type_name'; 

      public function render_content() { 

      $latest = new WP_Query(array(
       'post_type' => $this->post_type, 
       'post_status' => 'publish', 
       'orderby'  => 'date', 
       'order'  => 'DESC' 
      )); 

      ?> 
       <label> 
        <span class="customize-control-title"><?php echo esc_html($this->label); ?></span> 
        <select <?php $this->link(); ?>> 
         <?php 
         while($latest->have_posts()) { 
          $latest->the_post(); 
          echo "<option " . selected($this->value(), get_the_ID()) . " value='" . get_the_ID() . "'>" . the_title('', '', false) . "</option>"; 
         } 
         ?> 
        </select> 
       </label> 
      <?php 
      } 
     } 
    endif; 
    function theme_name_pages($wp_customize) { 
    $wp_customize->add_setting('home_page_control'); 
    $wp_customize->add_control(
     new WP_Customize_Latest_Post_Control(
      $wp_customize, 
      'home_page_control', 
      array(
      'label' => __('Select A Home Page', 'theme_name'), 
      'section' => 'theme_name_theme_panel', 
      'settings' => 'home_page_control', 
      'post_type' => 'custom_post_type_name' 
      ) 
     ) 
    ); 
    } 
    add_action('customize_register', 'theme_name_pages'); 


    //Static custom post for landing 
    add_filter('get_pages', function ($pages, $args) 
    { 
     // First make sure this is an admin page, if not, bail 
     if (!is_admin()) 
      return $pages; 

     // Make sure that we are on the reading settings page, if not, bail 
     global $pagenow; 
     if ('options-reading.php' !== $pagenow) 
      return $pages; 

     // Remove the filter to avoid infinite loop 
     remove_filter(current_filter(), __FUNCTION__); 

     // Setup our static counter 
     static $counter = 0; 

     // Bail on the third run all runs after this. The third run will be 2 
     if (2 <= $counter) 
      return $pages; 

     // Update our counter 
     $counter++; 

     $args = [ 
      'post_type'  => 'custom_post_type_name', 
      'posts_per_page' => -1 
     ]; 
     // Get the post type posts with get_posts to allow non hierarchical post types 
     $new_pages = get_posts($args); 

     // If we only need custom post types 
     $pages = $new_pages; 

     return $pages; 
    }, 10, 2); 

而且在functions.php中添加以下功能

add_action('pre_get_posts', 'set_custom_post_type_posts_as_homepage'); 

    function set_custom_post_type_posts_as_homepage($query) { 

     if($query->is_main_query() && $query->is_home()) { 
      $query->set('post_type', array('post', 'custom_post_type_name')); 
     } 
    } 

末創建一個PHP文件(home.php)

在調用以下語法

$postid = get_theme_mod('home_page_control'); 

    if($postid == ""){ echo "Please select a page to show on home page in appearance > customize"; } 

如果得到任何問題,所以更改永久設定和嘗試。

相關問題