douguachi0056 2013-09-26 20:21
浏览 93

在循环外的自定义帖子类型中获取第一篇和最后一篇文章

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.

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.

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 } ?>
  • 写回答

2条回答 默认 最新

  • douyan2680 2013-09-26 21:06
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法