2017-07-04 104 views
0

我从头开始开发Wordpress自定义主题。我设法让自定义网格显示在index.php中工作。每个帖子都会显示一个图片,其中的帖子标题和类别为副标题。这里是index.php的代码:WordPress的永久链接没有链接到content.php

<?php get_header(); ?> 
 

 
<div class="container-fluid bg-3 text-center Site-content" style="padding:60px; padding-top:100px;"> 
 
    <?php 
 
    $counter = 1; //start counter 
 

 
    $grids = 2; //Grids per row 
 

 
    query_posts($query_string . '&caller_get_posts=1&posts_per_page=12'); 
 

 

 
    if (have_posts()) : while (have_posts()) : the_post(); 
 
      ?> 
 
      <?php 
 
//Show the left hand side column 
 
      if ($counter == 1) : 
 
       ?> 
 
       <div class="col-sm-4"> 
 
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
 
         <img src="<?php the_post_thumbnail_url(); ?>" class="img-responsive" style="width:100%;" alt=""></a> 
 
        <strong><?php the_title(); ?></strong> 
 
        <?php 
 
        foreach ((get_the_category()) as $category) { 
 
         echo "<h6>".$category->category_nicename. "</h6>"; 
 
        } 
 
        ?> 
 
       </div>  
 
       <?php 
 
//Show the right hand side column 
 
      elseif ($counter == $grids) : 
 
       ?> 
 
       <div class="col-sm-4"> 
 
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
 
         <img src="<?php the_post_thumbnail_url(); ?>" class="img-responsive" style="width:100%;" alt=""></a> 
 
        <strong><?php the_title(); ?></strong> 
 
        <?php 
 
        // heres just the name and permalink: 
 
        foreach ((get_the_category()) as $category) { 
 
         echo "<h6>".$category->category_nicename. "</h6>"; 
 
        } 
 
        ?> 
 
       </div> 
 
       <?php 
 
       $counter = 0; 
 
      endif; 
 
      ?> 
 
      <?php 
 
      $counter++; 
 
     endwhile; 
 
    endif; 
 
    ?> 
 
</div> 
 
<?php get_footer(); ?>

帖子的缩略图包含一个链接到这篇文章的永久链接,应该链接到content.php。这里的content.php:

<div class="container" style="padding:100px;"> 
 
\t <strong><?php the_title(); ?></strong> 
 
     <?php 
 
     foreach ((get_the_category()) as $category) { 
 
      echo "<h5>" . $category->category_nicename . "</h5>"; 
 
     } 
 
     ?> 
 
     <h5>/<?php the_date(); ?> </h5> 
 
     <h5> <?php the_content(); ?> </h5> 
 
    </div>

的问题是,当我点击缩略图的index.php,它链接到自身的缩小版,而不是content.php页面。我的猜测是,我需要告诉固定链接去哪里,但我无法真正找到放置该配置的位置。

谢谢!

回答

1

在WordPress中,the_permalink()get_permalink()返回一个帖子或页面的单页URL。这是一个WordPress template hierarchy单个帖子页面将加载single.php文件。

您在这里两个选项,

1)你的代码复制从content.phpsingle.php,你会好到哪里去。当然,您需要包含get_header()get_footer()才能正确加载页面。

2)在循环内部的single.php中,可以包含content.php作为模板部分。以下内容在您的single.php中,替换循环。

while (have_posts()) : 
    the_post(); 
    get_template_part('content'); 
endwhile; 

此外,在上面的代码中,我假设您的content.php被放置在主题目录root中。

+0

它工作完美,谢谢!现在,我只是无法将我的外部CSS表应用于生成的内容,但我想这是另一回事。 – rschpdr