简体   繁体   English

自定义帖子类型的单独单页模板

[英]Separate Single page template for custom post type

I have a wordpress websites which shows latest posts with title, image, description and read more button in the blog page. 我有一个wordpress网站,该网站在博客页面上显示带有标题,图像,描述和更多按钮的最新帖子。 when the read more button is clicked it will be directed to the single.php and its working fine. 单击“更多”按钮后,它将定向到single.php及其正常工作。

Now i have created a new custom post type called "Products" where i can add products. 现在,我创建了一个新的自定义帖子类型,称为“产品”,可以在其中添加产品。 it have similar layout of the single.php, but i need some changes and additional things in the single page of the products. 它具有与single.php类似的布局,但是我需要在产品的单个页面中进行一些更改和其他操作。 So, I am planning to create a separate file single_product.php . 因此,我打算创建一个单独的文件single_product.php。

I want the read more button under the product to automatically link to the single_product.php as the blog posts are linked to the single.php 我希望产品下的“更多阅读”按钮自动链接到single_product.php,因为博客文章已链接到single.php

Here is the code for : Single.php 这是代码:Single.php

<?php get_header(); ?>
<!--BEGIN #content -->
<div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
<!--BEGIN .hentry -->
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">   
<!--BEGIN .post-header-->
<div class="post-header">
  <div class="inner"> <span class="meta-category">
    <?php the_category(', '); ?>
    </span>
    <h1 class="post-title">
      <?php the_title(); ?>
      <?php $format = get_post_format(); ?>
      <?php if ($format == "image" || $format == "gallery" || $format == "video") : ?>
      <span class="icon"><img src="<?php echo get_template_directory_uri(); ?>/images/icon-<?php echo $format; ?>.png" alt="<?php echo $format; ?>" /></span>
      <?php endif; ?>
    </h1>
    <span class="meta-published">
    <?php _e('Posted', 'engine') ?>
    <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' '.  __('ago', 'engine'); ?></span> <span class="meta-author">
    <?php _e('by', 'engine') ?>
    <?php the_author_posts_link(); ?>
    </span> </div>

  <!--END .post-header --> 
  </div>

  <!--BEGIN .featured-image -->
  <div class="featured-image <?php echo get_post_format(); ?>">
  <?php if (get_post_format() == 'video' && get_post_meta(get_the_ID(), 'dt_video', true) != '') : ?>
  <?php
                global $wp_embed;
                $video_url = get_post_meta(get_the_ID(), 'dt_video', true);
                $video_embed = $wp_embed->run_shortcode('[embed width="620"]'.$video_url.'[/embed]');
                ?>
  <div id="video-<?php the_ID(); ?>"><?php echo $video_embed; ?></div>
  <?php elseif (get_post_format() == 'gallery') : ?>

  <!--BEGIN #slides -->
  <div id="single-slides" class="clearfix">
    <?php

                    $args = array(
                        'orderby' => 'menu_order',
                        'post_type'      => 'attachment',
                        'post_parent'    => get_the_ID(),
                        'post_mime_type' => 'image',
                        'post_status'    => null,
                        'numberposts'    => -1,
                    );

                    $attachments = get_posts($args);

                    ?>
      <?php if ($attachments) : ?>
      <div class="slides_container">
      <?php foreach ($attachments as $attachment) : ?>
      <?php
      $format = get_post_format();
      $src = wp_get_attachment_image_src( $attachment->ID,
      array( '9999','9999' ), false, '' );
                            $src = $src[0];
  $image = dt_resize($attachment->ID, $src, 620, '', true);?>
      <div> <span class="overlay-icon overlay-<?php echo $format; ?>">
      <a rel="group-<?php the_ID(); ?>" 
      title="<?php echo $attachment->post_title;?>"     
      class="colorbox-<?php echo $format; ?>" 
      href="<?php echo $src; ?>"></a></span>
      <img height="<?php echo $image['height']; ?>"
      width="<?php echo $image['width']; ?>"
      alt="<?php echo apply_filters('the_title', $attachment->post_title); ?>"
                        src="<?php echo $image['url']; ?>"
                        /> </div>
      <?php endforeach; ?>
      </div>

      <!--BEGIN .slide-cntrols-->
      <div id="slide-controls"> <a href="#" class="next">Next</a>
      <a href="#" class="prev">Prev</a> 
      <!--END .slide-cntrols--> 
      </div>
      <?php endif; ?>
      <!--END #slides --> 
      </div>
      <?php elseif (has_post_thumbnail() && get_option('dt_blog_image') != 'false'):?>
      <?php $thumb = get_post_thumbnail_id(get_the_ID());
  $image = dt_resize( $thumb, '', 620, '', true );
  echo '<img src="'.$image['url'].'" width="'.$image['width'].'"     
      height="'.$image['height'].'" alt="" />';?>
      <?php endif; ?>
      <!--END .featured-image --> 
      </div>
      <!--BEGIN .post-content -->
      <div class="post-content">
      <?php the_content(); ?>
      <!--END .post-content --> 
      </div>

      <!--BEGIN .post-footer-->
      <div class="post-footer"> <span class="meta-published">
      <?php echo human_time_diff( get_the_time('U'), current_time('timestamp')).'    
     '.  __('ago', 'engine'); ?></span> <span class="meta-comments">
      <?php comments_number(__('No Comments', 'engine'), 
      __('1 Comment','engine'), __('% Comments', 'engine')); ?>
      </span> 
      <!--END .post-footer--> 
      </div>
      <!--END .hentry--> 
      </div>
      <?php comments_template('', true); ?>
      <?php endwhile; else : ?>
      <p>
      <?php _e('No posts found', 'engine'); ?>
      </p>
      <?php endif; ?>
      </div>
      <!-- #content -->
      <?php get_sidebar(); ?>
      <?php get_footer(); ?>

How can i do this ? 我怎样才能做到这一点 ?

Try to use like this : 尝试这样使用:

1.  archive-{post_type}.php
2.  single-{post_type}.php

Check this link for more detail. 检查此链接以获取更多详细信息。

See the Page Hierarchy in an image: 请参阅图像中的页面层次结构

enter image description here

  1. archive-{custompost_type_name}.php /* like archive-brand.php */ archive- {custompost_type_name} .php / *类似于archive-brand.php * /

  2. single-{custompost_type_type}.php /* like single-brand.php */ single- {custompost_type_type} .php / *类似于single-brand.php * /

  3. taxonomy-{custompost_type_category_name}.php /* like taxonomy-brand_category.php*/ taxonomy- {custompost_type_category_name} .php / *类似于taxonomy-brand_category.php * /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM