自定义帖子类型获取网址循环简码

时间:2018-11-30 18:13:18

标签: php wordpress function loops shortcode

我这里有一个PHP脚本,我似乎无法回避,我基本上是在尝试创建一个显示下一个自定义帖子类型url的简码,然后当当前帖子也是最后一个帖子时,它会循环返回将网址拉到第一篇文章。 一切正常,除了您在最终帖子上时,简码显示当前帖子(最终帖子)的ID而不是第一篇帖子的url,我假设这与“'fields'=>'有关ids'“,但我不知道,因为我不写php:

function next_post_url() {
$next_post = get_next_post();
if(!$next_post){
    $args = array(
      'numberposts' => 1,
      'post_type'   => 'portfolio',
      'order'   => 'ASC',
      'fields'  => 'ids'
    );
    $first_post = get_posts( $args );
    $next_post_url = $first_post[0];
}
else{
    $next_post_url = get_next_post()->post_title;
}
return get_permalink($next_post->ID);}add_shortcode( 'next_post_url', 'next_post_url' );

1 个答案:

答案 0 :(得分:0)

在这里,请按如下所示修改您的php代码。

function next_post_url() {
   $next_post = get_next_post();
   if (!$next_post) {
     $args = array(
        'numberposts' => 1,
        'post_type' => 'portfolio',
        'order' => 'ASC',
        'fields' => 'ids'
     );
     $first_post = get_posts($args);
     $post = $first_post[0];
   } else {
     $post = $next_post;
   }
   return get_permalink($post);
}
add_shortcode('next_post_url', 'next_post_url');
相关问题