简体   繁体   English

WordPress的永久链接不链接到content.php

[英]Wordpress permalink not linking to content.php

I'm developing a Wordpress custom theme from scratch. 我正在从头开始开发Wordpress自定义主题。 I've managed to get a custom grid display to work in index.php. 我已经设法使自定义网格显示在index.php中工作。 Each post shows an image with the post's title and category as subtitle. 每篇文章都显示一张图像,其中该文章的标题和类别为副标题。 Here's index.php code: 这是index.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(); ?> 

The post's thumbnail contains a link to the post's permalink, that should link to content.php. 帖子的缩略图包含指向该帖子的永久链接的链接,该链接应链接到content.php。 Here's content.php: 这是content.php:

 <div class="container" style="padding:100px;"> <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> 

The problem is that when I click on the index.php thumbnail, it links to a smaller version of itself, instead of the content.php page. 问题是,当我单击index.php缩略图时,它链接到自身的一个较小版本,而不是content.php页面。 My guess is that I need to tell the permalink where to go but I can't really find where to put this configuration. 我的猜测是,我需要告诉永久链接该去哪里,但我真的找不到在哪里放置此配置。

Thanks! 谢谢!

In WordPress, the_permalink() or get_permalink() returns the single page URL for a post or page. 在WordPress中, the_permalink()get_permalink()返回帖子或页面的单页URL。 It's a WordPress template hierarchy that single post pages will load the single.php file. 这是一个WordPress 模板层次结构 ,单个帖子页面将加载single.php文件。

You've two options here, 您在这里有两个选择,

1) Copy your code from content.php to single.php and you'll be good to go. 1)将您的代码从content.php复制到single.php ,这样您就可以顺利进行了。 Of course you need to include get_header() and get_footer() to make the page load properly. 当然,您需要包括get_header()get_footer()才能正确加载页面。

2) In your single.php inside the loop, you can include content.php as template part. 2)在循环内的single.php ,可以包含content.php作为模板部分。 The following goes in your single.php , replacing the loop. 以下内容进入您的single.php ,替换了循环。

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

Also, in the code above, I am assuming your content.php is placed in the theme directory root. 另外,在上面的代码中,我假设您的content.php位于主题目录的根目录中。

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

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