首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >您能在wordpress中将自定义的post类型设置为静态主页吗?

问您能在wordpress中将自定义的post类型设置为静态主页吗?
EN

Stack Overflow用户
提问于 2014-01-30 20:25:17
回答 3查看 4.2K关注 0票数 2

您能在wordpress中将自定义的post类型设置为静态主页吗?

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

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

我是运行多个网站,并希望每个网站的主页是一个“代理”自定义邮政类型下创建的帖子。他们只允许有一个帖子,这将是他们的主页。

我似乎无法得到作为静态主页的帖子。

有什么建议吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-31 14:39:11

首先创建自定义模板,

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

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

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

我希望你明白..。

票数 2
EN

Stack Overflow用户

发布于 2017-11-06 11:49:21

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

1)创建自定义的post类型。landing_page

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

使用创建theme_option.php在wordpress主题自定义器中创建自定义下拉字段作为“选择主页”,并编写以下代码-

代码语言:javascript
复制
    <?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中添加以下函数

代码语言:javascript
复制
  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)

在该调用中,以下语法

代码语言:javascript
复制
    $postid = get_theme_mod( 'home_page_control');

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

如果遇到任何问题,请更改permalink设置并尝试。

票数 0
EN

Stack Overflow用户

发布于 2019-08-14 14:31:55

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

代码语言:javascript
复制
header("Location: https://your-site.com/url-of-post-for-main-page/");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21466753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档