获得第一循环外自定义帖子类型中的最后一个帖子 [英] Getting first & last post in custom post type outside of loop

查看:21
本文介绍了获得第一循环外自定义帖子类型中的最后一个帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WP,并且有一个脚本,当单击图像时,使用 .load() 加载单个帖子内容.浏览每个帖子的箭头位于使用 .load() 加载的 .project div 内.

I'm using WP, and have a script where when an image is clicked, the single post content loads using .load(). The arrows to navigate through each single post is located inside the .project div that is being loaded using .load().

问题是,在第一个和最后一个帖子上,我只想显示某些箭头.

Problem is, on the first and last posts, I only want to display certain arrows.

例如,帖子中的第一项被加载,它不应该有上一个"箭头,因为没有以前的帖子.与上一篇文章和下一个"箭头相同.

For example, the first item in the post gets loaded in, it shouldn't have the 'previous' arrow because there are no previous posts. Same with the last post and the 'next' arrow.

所以基本上是为了解决这个问题,我试图想出一个 PHP 语句(没有在循环中)来判断当前帖子是自定义帖子类型中的最后一个帖子还是第一个帖子.

So basically to work around this, I'm trying to come up with a PHP statement (without being in the loop) to tell if the current post is the last post or first post in the custom post type.

这是我到目前为止所拥有的……只是不知道如何在循环之外获取第一篇文章和最后一篇文章的 ID.除此之外的一切都已经过测试和工作.下面主要是代码背后的逻辑".

Here's what I have so far.. just not sure how to get ID's of first post and last post outside of the loop. Everything else besides that has been tested and works. Below is just the 'logic' behind the code mostly.

<?php
// Get other posts in post type
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();

// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;

// if($next_id == $previous_id) because on first/last posts, get_next_post
// and get_previous_post return the same value.
if($next_id == $previous_id) {
    if() { 
        // if last post in custom post type
    } else() {
        // if first post in custom post type
    }
}
?>

<?php if(isnt first post) { ?>
    <li class="left" data-projectid="<?php echo $next_id; ?>"></li>
<?php } ?>
<li class="grid"></li>
<?php if(isnt last post) { ?>
    <li class="right" data-projectid="<?php echo $previous_id; ?>"></li>
<?php } ?>

推荐答案

我对 WP 的使用不多,但由于 WP 模板都是 PHP 文件,而且 WP 向用户公开了自己的 API,因此您可以在其中使用任何 PHP 语法他们.如果您不担心每次浏览页面时都运行两个查询,那么这将有助于您了解这个想法.

I haven't worked with WP so much, but since WP templates are all PHP files and WP exposes its own API to user you can use any PHP syntax in them. If you're not concerned about running two queries each time you navigate your page then this will help you to get the idea.

<?php

global  $wpdb;
$last_one = FALSE;
$first_one = FALSE;

// Get last one
$last_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` DESC LIMIT 0, 1", ARRAY_A);
if($last_result){ if($last_result['id'] == $next_post){ $last_one = TRUE; } }

// Get first one
$first_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` ASC LIMIT 0, 1", ARRAY_A);
if($first_result){ if($first_result['id'] == $previous_post){ $first_one = TRUE; } }

?>

记得检查字段和表的名称,因为我不知道名称.

Remember to check the names of fields and tables as I don't know the names.

这篇关于获得第一循环外自定义帖子类型中的最后一个帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆