首页 > 解决方案 > wp 安装文件夹外的 the_permalink() wordpress

问题描述

这是我在 wordpress 安装目录之外加载 wordpress 帖子的代码。我发现我的先例问题和一个主题非常有用,该主题将讨论 wp 函数在其他独立 php 页面中的这种用法。

PHP查询

<?php

define('WP_USE_THEMES', FALSE);

require 'portfolio/wp-load.php';

$args = array(
'posts_per_page' => 6,
#'offset' => 0,
#'category' => 'portfolio',
#'category_name' => '',
#'orderby' => 'date',
#'order' => 'DESC',
#'include' => '', 'exclude' => '',
#'meta_key' => '',
#'meta_value' => '',
#'post_type' => 'post', 'post_mime_type' => '',
#'post_parent' => '',
#'author' => '',
#'post_status' => 'publish',
#'suppress_filters' => true
);

$portfolio_items = new WP_Query( $args );

?>

html

<div class="row" id="portfolioPosts">

<?php
if($portfolio_items->have_posts()):
  while($portfolio_items->have_posts()):
    $portfolio_items->the_post(); ?>

<div class="col-sm-12 col-md-4 col-lg-4" id="portfolio-grid-el">
<a class="portfolio-link" href="<?php the_permalink(); ?>">

<?php
if(has_post_thumbnail()):
  the_post_thumbnail('post-thumbnail', ['class' => 'card-img-top post-preview', 'title' => 'Feature image']);
endif;
?>
<h4 class="portfolio-post-title text-uppercase"><?php the_title(); ?></h4>
</a>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); wp_reset_query(); ?>
</div>

我的问题是关于the_permalink()功能,如何正确链接它,我已经将 wordpress 配置为使用我的网站索引,该索引是 wp 安装目录之外和网站根目录中的独立页面。现在每次我点击链接时,地址都是http://localhost:8000/mysite/post-name这样,我将无法显示帖子。我想使用一个解决方案,其中<a href="">链接相对于 wordpress 安装路径或相对于将接受帖子名称作为参数并加载内容的页面,正如我通过使用带有自定义查询的循环所理解的那样。是否可以?

标签: phpwordpress

解决方案


使用下面的代码:

<?php the_permalink(); ?>

<?php echo get_permalink($portfolio_items->post->ID); ?>

更多信息


推荐阅读