我想在 front-page.php 中添加博客页面。我只想显示带有实际帖子链接的帖子标题。我不知道在代码中放置 the_permalink 的位置。我的代码如下所示:
<div class="profile">
<?php
$query = new WP_query( 'pagename=blog' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ){
$query->the_post();
echo '<h2 class="text-center">' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
?>
</div><!--end blog-content -->
当我放置时,超链接转到 domain.com/get_permalink() 而不是实际的 url。
例如实际的 url 是 domain.com/abc。 当我点击帖子标题时,它会转到 domain.com/get_permalink()
谁能帮我解决我的问题?
最佳答案
可能是您以错误的方式使用了 get_permalink
。尝试以下代码。
<div class="profile">
<?php
$query = new WP_query( 'pagename=blog' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ){
$query->the_post();
echo '<h2 class="text-center"><a href="' . get_permalink() . '">';
the_title();
echo '</a></h2>';
}
}
wp_reset_postdata();
?>
</div><!--end blog-content -->
关于php - 如何在 Wordpress 主题中将永久链接添加到 WP_Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158910/