我已经为Wordpress偏移量完成了php代码,请参见下文。 这段代码可以正常工作,但是我想知道这样做的正确方法吗?
您可以在下面使用这部分代码。
我尝试过,并且效果很好。我希望这段代码符合Wordpress标准。
function hc_get_post_type_items($post_type, $cat = "", $pag_items = 10, $latest_items = "", $isSearch = false, $isPagination = true) {
global $paged;
global $hc_wp_query;
if (!isset($paged)) $paged = (get_query_var('page')) ? get_query_var('page') : 1;
if (!$isPagination) $paged = 0;
$arr = array();
$args = array();
if ($isSearch) {
$args = array('s' => get_search_query(),'paged' => $paged);
} else {
if (is_tag()) {
$args = array('post_type' => $post_type,'tag'=>$cat, 'posts_per_page' => $pag_items, 'offset' => $latest_items, 'paged' => $paged);
} else {
if ($post_type != 'post') {
$args = array('post_type' => $post_type, $post_type. '-cat'=>$cat, 'posts_per_page' => $pag_items, 'offset' => $latest_items, 'paged' => $paged);
} else {
$args = array('post_type' => $post_type,'category_name'=>$cat, 'posts_per_page' => $pag_items, 'offset' => $latest_items, 'paged' => $paged);
}
}
}
$arr = hc_get_post_type_items($Y_NOW["post_type_slug"],$cat,(($Y_NOW["pagination_type"] == "pagination_wp" || $Y_NOW["pagination_type"] == "") ? $Y_NOW["pag_items"] : "9999"),$Y_NOW["latest_items"]);
$HC_PAGE_ARR = json_decode(stripslashes($post->post_content));
$HC_SOURCE_PAGE_ARR;
if ($HC_PAGE_ARR === NULL & $HC_POST_TYPE != "page" && $HC_POST_TYPE != "post") {
$args = array( 'post_type' => 'y-post-types', 'posts_per_page' => 999 );
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
$content = json_decode(get_the_content(), true);
if (!($content === NULL)) {
if ($content['post_type_setting']['settings']['slug'] == $HC_POST_TYPE) {
$HC_SOURCE_PAGE_ARR = $content;
break;
};
}
}
}
}