为自定义帖子类型添加阅读更多链接


WordPress : adding read more link for custom post type

我使用product-template.php从自定义post类型的产品中获取数据,像这样

   <div class="container" style="margin-top: 20px;">
      <h1>Our Products</h1>
        <?php
         $args=array('post_type' => 'products');
         $query= new WP_Query($args);                               
         while ($query-> have_posts() ) : $query->the_post()?>
         <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12">
             <h1><?php echo the_title();?></h1>
             <?php the_post_thumbnail( 'full', array( 'class' => 'innerimages') );?>
         </div>
     <?php                                
        endwhile;
     ?>
   </div> 

现在我想做的是标题应该是可点击的一旦点击下一页,我应该能够获得所有关于被点击产品的细节如何做到这一点?请帮助。

     <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12">
         <h1><a href="<?php echo get_post_permalink() ?>"><?php echo the_title();?></a></h1>
         <?php the_post_thumbnail( 'full', array( 'class' => 'innerimages') );?>
     </div>

如果它不工作尝试:

     <h1><a href="<?php echo get_post_permalink($post->ID) ?>"><?php echo the_title();?></a></h1>

你必须为你的自定义帖子类型使用一个页面。

single-your_custom_post_type.php

在你的例子中:"single-products.php"。然后添加循环

您可以创建一个归档页面,列出所有自定义的帖子类型

archive-your_custom_post_type.php (archive-products.php)并使用相同的循环

<div class="site-content">
    <?php
        if ( have_posts() )
        {
            while ( have_posts() )
            {
                the_post();
                the_title();
                the_content();
            }
        }
    ?>
</div>

在模板页的缩略图后面添加一个永久链接。

echo "<a href='" . get_permalink() . "'>Read more</a>";

ps: the_title()显示标题,您不需要回显它。如果您想要回显,则使用echo get_the_title();

希望对你有帮助。

试试

<a href="<?php echo get_post_permalink($post->ID) ?>">READ MORE</a>

使用get_permalink可以获取文章的永久链接,详细信息请参见wordpress文档