在wordpress中添加自定义帖子类型

时间:2017-09-14 09:52:36

标签: jquery html css wordpress twitter-bootstrap

我正在尝试将一个引导程序的单页模板转换为wordpress模板,我想添加自定义帖子类型,显示服务组合中的项目,同样的css ....这里是我的服务部分.///我跟着答案下来,这是我的部门代码

   <!-- Services -->
<section id="services" class="services bg-primary text-white">
  <div class="container">
    <div class="row text-center">
      <div class="col-lg-10 mx-auto">
        <h2>Our Services</h2>

        <hr class="small">
        <div class="row">

          <?php
              $args = array( 'post_type' => 'service', 'posts_per_page' => 10 );
              $the_query = new WP_Query( $args );
              ?>
              <?php if ( $the_query->have_posts() ) : ?>
              <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

              <div class="col-md-3 col-sm-6">
                                      <div class="service-item">
                                          <span class="fa-stack fa-4x">
                                              <i class="fa fa-circle fa-stack-2x"></i>
                                              <i class="fa fa-flask fa-stack-1x text-primary"></i>
                                          </span>
                                          <h4><?php the_title(); ?></h4>
                                          <p><?php the_content(); ?> </p>
                                          <a href="<?php the_permalink(); ?>" class="btn btn-light">Learn More</a>
                                      </div>
                                  </div>

              <?php wp_reset_postdata(); ?>
              <?php else:  ?>
              <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
              <?php endif; ?>


        </div>
        <!-- /.row (nested) -->
      </div>
      <!-- /.col-lg-10 -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container -->
</section>

我很困惑我必须添加什么功能以及如何?????????以及我必须在index.php文件中替换不同的部分。和functions.php代码..

// Our custom post type function
 function create_posttype() {

     register_post_type( 'service',
     // CPT Options
         array(
             'labels' => array(
                 'name' => __( 'Services' ),
                 'singular_name' => __( 'Service' )
             ),
             'public' => true,
             'has_archive' => true,
             'rewrite' => array('slug' => 'service'),
         )
     );
 }
 // Hooking up our function to theme setup
 add_action( 'init', 'create_posttype'

);

现在显示Syntex错误

1 个答案:

答案 0 :(得分:1)

注册帖子类型将此代码添加到function.php中。 (推荐创建seprate插件文件)

   // Our custom post type function
    function create_posttype() {

        register_post_type( 'service',
        // CPT Options
            array(
                'labels' => array(
                    'name' => __( 'Services' ),
                    'singular_name' => __( 'Service' )
                ),
                'public' => true,
                'has_archive' => true,
                'rewrite' => array('slug' => 'service'),
            )
        );
    }
    // Hooking up our function to theme setup
    add_action( 'init', 'create_posttype'

 );

使用以下代码显示服务:

<?php 
$args = array( 'post_type' => 'service', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="col-md-3 col-sm-6">
                        <div class="service-item">
                            <span class="fa-stack fa-4x">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-flask fa-stack-1x text-primary"></i>
                            </span>
                            <h4><?php the_title(); ?></h4>
                            <p><?php the_content(); ?> </p>
                            <a href="<?php the_permalink(); ?>" class="btn btn-light">Learn More</a>
                        </div>
                    </div>
<?php endwhile; ?> 
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>