Home add custom post type in wordpress
Reply: 1

add custom post type in wordpress

Shaharyar Zafar
1#
Shaharyar Zafar Published in 2017-09-14 09:52:35Z

i am trying to convert a bootstrap one page template to wordpress template and i want to add custom post type which show items in services portfolio whith same css.... here is my services section ./// I Followed The Answer Down And Here Is My Section Code Now

   <!-- 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>

i am confused what function i have to add and how????????? and what i have to replace in index.php file for different sections. and functions.php code..

// 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'

);

And Now Its Showing Syntex Error

Vasim Vanzara
2#
Vasim Vanzara Reply to 2017-09-14 11:34:07Z

Register post type add this code into function.php. (Recommanded create seprate plugin file)

   // 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'

 );

Display Service using below code:

<?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; ?>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300937 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO