【问题标题】:Can you set a custom post type post as a static home page in wordpress?您可以将自定义帖子类型的帖子设置为 wordpress 中的静态主页吗?
【发布时间】:2014-02-23 08:40:48
【问题描述】:

你可以在wordpress中将自定义帖子类型的帖子设置为静态主页吗?

在阅读设置中,在首页显示下,我可以选择使用静态页面。

我可以使用帖子作为我的主页吗?

我正在运行多站点,并希望每个站点的主页都是在“代理”自定义帖子类型下创建的帖子。他们只能有一个帖子,这将是他们的主页。

不过,我似乎无法将帖子作为静态主页。

有什么建议吗?

【问题讨论】:

    标签: wordpress custom-post-type


    【解决方案1】:

    首先创建您的自定义模板,

    在您的模板中,查询您的 CPT(自定义帖子类型),

    现在,在wp-admin 中创建页面并分配您创建的模板。

    现在转到设置->>阅读->选择您在wp-admin中创建的页面...

    希望你能明白。

    【讨论】:

      【解决方案2】:

      是的,我们可以将自定义帖子类型(任何帖子)设置为您网站的主页, 步骤:

      1) 创建自定义帖子类型,例如。登陆页面

      2)添加您喜欢的帖子(主页/服务/关于/任何...)

      使用创建 theme_option.php 在您的 wordpress 主题定制器中将自定义下拉字段创建为“SELECT HOME PAGE”并编写此代码 -

          <?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"; }
      

      如果遇到任何问题,请更改永久链接设置并尝试。

      【讨论】:

        【解决方案3】:

        您可以创建新的静态页面,为该页面创建新模板并在模板中添加

        header("Location: https://your-site.com/url-of-post-for-main-page/");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-17
          • 2016-05-13
          • 1970-01-01
          • 1970-01-01
          • 2016-11-29
          • 2019-01-03
          • 1970-01-01
          • 2016-04-27
          相关资源
          最近更新 更多