• 欢迎访问趣尚博客、分享互联网、软件、编程、创业、英语、VPS 推荐使用最新版火狐浏览器和Chrome浏览器访问本网站
  • 如果您喜欢我的博客,那么请使用Ctrl+D 收藏吧!

WordPress创建新的文章类型

WooCommerce开发 qushang 3年前 (2017-12-14) 158次浏览

https://codex.wordpress.org/Function_Reference/register_post_type
使用方法

<?php register_post_type( $post_type, $args ); ?>

假设我们注册一个产品的类型

<?php
// Register Custom Post Types
add_action('init', 'register_custom_posts_init');
function register_custom_posts_init() {
    // Register Products
    $products_labels = array(
        'name'               => 'Products',
        'singular_name'      => 'Product',
        'menu_name'          => 'Products'
    );
    $products_args = array(
        'labels'             => $products_labels,
        'public'             => true,
        'capability_type'    => 'post',
        'has_archive'        => true,
        'supports'           => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' )
    );
    register_post_type('products', $products_args);
}

前台调用代码

<?php
  $args = array(
    'post_type' => 'products',
    'post_status' => 'publish',
    'posts_per_page' => '10'
  );
  $products_loop = new WP_Query( $args );
  if ( $products_loop->have_posts() ) :
    while ( $products_loop->have_posts() ) : $products_loop->the_post();
      // Set variables
      $title = get_the_title();
      $description = get_the_content();
      $download = get_field(‘download’);
      $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
      $product_image1 = $featured_image[0];
      $product_image2 = get_field(‘product_image’);
      // Output
      ?>
      <div class=”product”>
        <img src=”<?php echo $product_image1;  ?>” alt=”<?php echo $title; ?>”>
        <h2><?php echo $title; ?></h2>
        <img src=”<?php echo $product_image1;  ?>” alt=”product-detail” class=”product-detail align-right”>
        <?php echo $description; ?>
        <p><a href=”<?php echo $download; ?>” target=”_blank” name=”Spec Sheet”>Download Spec Sheet</a></p>
      </div>
      <?php
      endwhile;
    wp_reset_postdata();
  endif;

 


极客公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress创建新的文章类型
喜欢 (0)
[399397742@qq.com]
分享 (0)