2017-07-04 102 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