自定义字段上的自定义发布类型导航

时间:2013-04-21 作者:NotoriousWebmaster

我正在处理按自定义字段排序的自定义帖子类型。当我列出帖子时没有问题。但是,当我转到其中一篇文章的单篇文章页面时,我希望上一篇和下一篇导航链接按照自定义字段指定的排序顺序将我们带到上一篇/下一篇文章。默认情况下,WP按发布日期对其进行排序。

排序列表代码:

    // shortcodes
    // lor_list of letters: Lists all letters of reference in a table format
    add_shortcode(\'lor_list\', \'aaaConsultant_shortcode_lor_list\');

    function aaaConsultant_shortcode_lor_list() {
        global $wp_query;

        $oWP = new wp_query(array(
            \'post_type\' => \'letters_of_ref\'
            , \'orderby\' => \'meta_value\'
            , \'meta_key\' => \'aaaConsultant_dateofletter\'
        ));

        $out = \'<div id="lor_list_container">\' . "\\n";
        if (!$oWP->found_posts) {
            $out .= \'<p>d\\\'Oh! We don\\\'t have any Letters of Reference yet.</p>\' . "\\n";
        } else {
            while ($oWP->have_posts()) {
                $oWP->the_post();
                $out .= \'<a href="\' . get_permalink() . \'">\' . get_the_title( $oWP->post->ID ) . \'</a>\' . "<br>\\n";
            }
        }
        $out .= \'</div> <!-- END #lor_list_container -->\' . "\\n";

        wp_reset_postdata();
        return $out;
    }
我该怎么做?

谢谢你的帮助。

1 个回复
最合适的回答,由SO网友:Hameedullah Khan 整理而成

要使上一篇和下一篇文章的链接起作用,您需要连接到get_*_post_* 过滤并修改查询,以使用自定义字段对帖子进行实际排序。我希望下面的代码将为您工作。

For Previous post link

add_filter(\'get_previous_post_join\', \'wpse96670_get_previous_post_join_custom_sort\');
add_filter(\'get_previous_post_where\', \'wpse96670_get_previous_post_where_custom_sort\');
add_filter(\'get_previous_post_sort\', \'wpse96670_get_previous_post_sort_custom_sort\');
function wpse96670_get_previous_post_join_custom_sort($join) {
    global $wpdb;
    return "INNER JOIN {$wpdb->prefix}postmeta ON (p.ID = {$wpdb->prefix}postmeta.post_id)";
}
function wpse96670_get_previous_post_where_custom_sort($where) {
    global $wpdb, $post;
    $meta_value = get_post_meta($post->ID, \'aaaConsultant_dateofletter\', true);
    return "WHERE 1=1  AND p.post_type = \'letters_of_ref\' AND p.post_status = \'publish\' AND {$wpdb->prefix}postmeta.meta_key = \'aaaConsultant_dateofletter\' AND {$wpdb->prefix}postmeta.meta_value < \'{$meta_value}\'";
}
function wpse96670_get_previous_post_sort_custom_sort($sort) {
    global $wpdb;
    return "ORDER BY {$wpdb->prefix}postmeta.meta_value DESC LIMIT 1";
}

For next post link

add_filter(\'get_next_post_join\', \'wpse96670_get_next_post_join_custom_sort\');
add_filter(\'get_next_post_where\', \'wpse96670_get_next_post_where_custom_sort\');
add_filter(\'get_next_post_sort\', \'wpse96670_get_next_post_sort_custom_sort\');
function wpse96670_get_next_post_join_custom_sort($join) {
    global $wpdb;
    return "INNER JOIN {$wpdb->prefix}postmeta ON (p.ID = {$wpdb->prefix}postmeta.post_id)";
}
function wpse96670_get_next_post_where_custom_sort($where) {
    global $wpdb, $post;
    $meta_value = get_post_meta($post->ID, \'aaaConsultant_dateofletter\', true);
    return "WHERE 1=1  AND p.post_type = \'letters_of_ref\' AND p.post_status = \'publish\' AND {$wpdb->prefix}postmeta.meta_key = \'aaaConsultant_dateofletter\' AND {$wpdb->prefix}postmeta.meta_value > \'{$meta_value}\'";
}
function wpse96670_get_next_post_sort_custom_sort($sort) {
    global $wpdb;
    return "ORDER BY {$wpdb->prefix}postmeta.meta_value ASC LIMIT 1";
}
然后,您需要在模板中输入一些代码:

    <?php 
        $oNext = get_adjacent_post(false, \'\', true);
        $oPrev = get_adjacent_post(false, \'\', false);

        if ($oPrev || $oNext) {
            echo \'<div id="lor_nav">\' . "\\n";
        }
        if ($oPrev) {
            echo \'<div class="prevlink"><a href="\' . get_permalink($oPrev->ID) . \'"> &laquo; \' . $oPrev->post_title . \'</a></div>\' . "\\n";
        }
        if ($oNext) {
            echo \'<div class="nextlink"><a href="\' . get_permalink($oNext->ID) . \'">\' . $oNext->post_title . \' &raquo;</a></div>\' . "\\n";
        }
        if ($oPrev || $oNext) {
            echo \'</div><div class="clear"></div>\' . "\\n";
        }
?>
如果您需要任何澄清,请告诉我。

结束

相关推荐

Thesis -style Navigation

我正在研究一个主题,我希望用户能够像论文一样选择要在主题选项页面中显示的页面。我已经在谷歌上搜索了几个小时的逆向工程论文,但还没有找到一个很好的解决方案。我想知道是否有人做过这件事或看过教程。