admin管理员组文章数量:1125307
In my function.php I need to run something to check post type or page id.
how can i check current post type loaded in functions.php
function.php
<?php
// the ajax function
add_action('wp_ajax_filterPosts', 'filterPosts');
add_action('wp_ajax_nopriv_filterPosts', 'filterPosts');
function filterPosts()
{
global $post;
if( is_singular() ) {
if( 'post_type_b' == $post->post_type ) {
$certain_post='post_b';
} elseif ( 'post' == $post->post_type ) {
$certain_post='post';
}
}
$response = [
'posts' => "",
];
// New args for an All Posts Query
$all_args = array(
'posts_per_page' => -1, // returns all posts
'post_status' => 'publish',
'post_type' => $certain_post
);
$filter_args = array(
'post_status' => 'publish',
'post_type' => $certain_post
);
if ($_POST['limit']) {
$filter_args['posts_per_page'] = $_POST['limit'];
}
if ($_POST['category']) {
$filter_args['cat'] = $_POST['category'];
// Get the total number of posts for the category
$all_args['cat'] = $_POST['category'];
}
$filter_query = new WP_Query($filter_args);
// New All Posts Query
$all_query = new WP_Query($all_args);
if ($filter_query->have_posts()) :
while ($filter_query->have_posts()) : $filter_query->the_post();
$response['posts'] .= load_template_part('/template-parts/posts-filter/single-post');
endwhile;
wp_reset_postdata();
endif;
echo json_encode($response);
die();
}
本文标签: Get current post type within functionsphp
版权声明:本文标题:Get current post type within functions.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736656490a1946268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论