php - 如何在 Wordpress 主题中将永久链接添加到 WP_Query

标签 php permalinks wordpress

我想在 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/

相关文章:

wordpress - WPML›子类别页面生成404 ‹WordPress

wordpress - 在 URL 中添加多个自定义帖子类型

php - 重定向原始 .gif 文件以通过 .htaccess 处理页面

php - 是否可以使用 mysql 将字符串添加到结果中?

javascript - 更改 URL 后重新加载页面

php - 如何检查wordpress自定义表是否为空

css - Wordpress 自定义菜单小部件和 CSS 样式

php - 根据内容长度用 PHP 更改标签

javascript - 如何为论坛中的永久链接生成二维码?

jekyll - 管理文件夹中的 Jekyll 页面(在 github-pages 上)