将自定义帖子类型的所有帖子ID包含到一个数组中

时间:2014-05-20 作者:Kent Miller

我的单身。php(products.php),我尝试构建一个导航,用户可以通过该导航根据元键“price”在产品之间切换。我想将产品帖子类型的所有id放入一个数组中,然后根据价格元键确定导航的上一个和下一个产品。

不知何故,我无法让代码正常工作:

SINGLE.PHP

<?php
// Setting up the loop
$args = array(
\'post_type\' => \'products\', 
\'post_status\' => \'publish\',
\'meta_key\' => \'price\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1
);
$posts_query = new WP_Query($args);

// Setting up the array in which ALL IDs of all product posts will be stored
$posts = array();

// Starting the loop
while ($posts_query->have_posts()) {
    // Including the post IDs one by one    
    $posts[] += $post->ID;
}

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

if (!empty($prevID)) { ?>
<a href="www.example.com/post_type=products&post_id=<?php 
echo $prevID; ?>">previous product</a>
<?php }
if (!empty($nextID)) { ?>
<a href="www.example.com/post_type=products&post_id=<?php 
echo $nextID; ?>">next product</a>
<?php } ?>
有人看到错误吗?

2 个回复
SO网友:jgraup

为自己保存一个循环get_posts\'fields\' => \'ids\'.

// Default Query

function do_query($args = null) {
    $default_args = array( 
        \'numberposts\' => -1,
        \'posts_per_page\' => -1,
        \'post_status\' => \'publish\',
        \'fields\' => \'ids\',
    );

    if (null !== $args && !empty($args))
        $args = array_merge($default_args, $args);
    else 
        $args = $default_args;

    return get_posts($args);
}

// Customize Query

$post_ids = do_query ( 
    array(
        \'post_type\' => \'products\',
        \'meta_key\' => \'price\',
        \'orderby\' => \'meta_value_num\',
        \'order\' => \'ASC\',
));

// Search for the index of the ID / prev / next

$currentID = get_the_ID(); 
$currentINX = array_search( $currentID, $post_ids);

$count = count($post_ids);

$prevINX = $currentINX - 1;
if ( $prevINX < 0 ) $prevID = FALSE;
else $prevID = $post_ids [ $prevINX ];

$nextINX = $currentINX + 1;
if ( $nextINX >= $count ) $nextINX = FALSE;
else $nextID = $post_ids [ $nextINX ];

// Output links

if ( $prevID ) {
    $link = get_permalink ( $prevID );
    echo "<a href=\\"${link}\\">previous product</a>";
}

if ( $nextID ) {
    $link = get_permalink ( $nextID );
    echo "<a href=\\"${link}\\">next product</a>";
}

SO网友:MortalViews

使用自定义查询后,需要调用wp_reset_postdata() 函数,在使用以下函数之前重置全局post数据get_the_ID()

<?php
// Setting up the loop
$args = array(
\'post_type\' => \'products\', 
\'post_status\' => \'publish\',
\'meta_key\' => \'price\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1
);
$posts_query = new WP_Query($args);

// Setting up the array in which ALL IDs of all product posts will be stored
$posts = array();

// Starting the loop
while ($posts_query->have_posts()) {
    // Including the post IDs one by one    
    $posts[] += $post->ID;
}

wp_reset_postdata(); //reset the global post data



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

if (!empty($prevID)) { ?>
<a href="www.example.com/post_type=products&post_id=<?php 
echo $prevID; ?>">previous product</a>
<?php }
if (!empty($nextID)) { ?>
<a href="www.example.com/post_type=products&post_id=<?php 
echo $nextID; ?>">next product</a>
<?php } ?>

结束

相关推荐

使用与Nginx/PHP-fpm相同组的用户在不使用FTP的情况下启用插件安装

我只是安装了一个VPS,安装了所有我需要的东西,它运行得很好。问题是,我希望它能够自动更新,并允许我安装没有ftp的插件。这是我的设置:我创建了一个名为“wordpress”的新用户和组,Nginx和PHP-FPM都使用“wordpress”作为用户和组</我在“wordpress”组中添加了自己的ssh帐户。假设我的用户名是“user”</我将文件的所有权更改为用户“user”和组“wordpress”,包括正确的权限(775用于文件夹,664用于文件,稍后我将尝试755/644)</