【问题标题】:Wordpress Page Pagination IF queryWordpress 页面分页 IF 查询
【发布时间】:2015-01-18 17:09:15
【问题描述】:

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}

$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div id="page_nav">
	<div class="wrapper">
		<a href="<?php echo get_permalink($prevID); ?>"><i class="fa fa-arrow-left"></i>Previous Work</a><span>/</span>
		<a id="back_link" href="http://kyleskelly.co.uk/#projects_container">Back to Projects</a><span>/</span>
		<a href="<?php echo get_permalink($nextID); ?>">Next Work<i class="fa fa-arrow-right"></i></a>
	</div>
</div>

我对php的了解不是很好,如果有任何帮助,将不胜感激,谢谢!

我希望我的代码能够查询 -

IF 是投资组合中的第一页,然后上一个链接的 a 标记无效。

else if 作品集中的最后一页,则下一页链接无效。

else我已有的代码

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    我没有尝试过这段代码,但我认为它应该可以工作。如果出现问题,请告诉我。我已经对代码进行了注释,以或多或少地向您解释我所做的事情。

    <?php
    $pagelist = get_pages('sort_column=menu_order&sort_order=asc');
    $current = get_the_ID();// Current Page ID. I assume IDs are unique, and all pages have one. All the IDs are in $pagelist.
    
    $next = $prev = FALSE;
    reset($pagelist);// Pointer to first element.
    while ($page = current($pagelist) ){// Loop through the array. I used while instead of foreach for its convenience to get next element.
        if( $page->ID == $current) {
            $obj = next($pagelist);
            $next = $obj ? $obj->ID : FALSE;// If there's a next, get the ID, otherwise we are the last page.
            break;
       }
    
       $prev = $page->ID;// Previous element
       next($pagelist);// Update pointer
    }
    
    ?>
    
    <div id="page_nav">
        <div class="wrapper">
            <?php if($prev !== FALSE) {// If I have a previous element, print out a ?>
            <a href="<?php echo get_permalink($prev); ?>"><i class="fa fa-arrow-left"></i>Previous Work</a><span>/</span>
            <?php } ?>
            <a id="back_link" href="http://kyleskelly.co.uk/#projects_container">Back to Projects</a><span>/</span>
            <?php if($next !== FALSE) {// If I have a next element, print out a ?>
            <a href="<?php echo get_permalink($next); ?>">Next Work<i class="fa fa-arrow-right"></i></a>
            <?php } ?>
        </div>
    </div>
    

    【讨论】:

    • @kyleskelly 很高兴为您提供帮助 :) 如果问题得到解决,请接受答案
    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多