我为我所有的CPT帖子创建了一个归档和分类模板。基于此,我创建了搜索。php文件,如下所示:
<div class="center">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php include \'product.php\' ?>
<?php endwhile ?>
<nav class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages,
\'prev_text\' => __(\' « \'),
\'next_text\' => __(\' » \'),
) );
?>
</nav>
<?php else : ?>
<p>
<?php esc_html_e( \'Sorry, no posts matched your criteria.\' ); ?>
</p>
<?php endif; ?>
</div>
在函数中。php I add pre\\u get\\u帖子:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set(\'post_type\', array( \'drzewa_formowane\', \'pre_bonsai\' ) );
}
}
}
add_action(\'pre_get_posts\',\'search_filter\');
以及我在函数中的CPT示例。php:
/**
register custom post type
**/
//http://codex.wordpress.org/Function_Reference/register_post_type
add_action( \'init\', \'drzewa_formowane\' );
function drzewa_formowane() {
$labels = array(
\'name\' => _x( \'Drzewa formowane\', \'post type general name\', \'your-plugin-textdomain\' ),
\'singular_name\' => _x( \'drzewa formowane\', \'post type singular name\', \'your-plugin-textdomain\' ),
\'menu_name\' => _x( \'Drzewa\', \'admin menu\', \'your-plugin-textdomain\' ),
\'name_admin_bar\' => _x( \'Drzewa formowane\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
\'add_new\' => _x( \'Dodaj nowe\', \'book\', \'your-plugin-textdomain\' ),
\'add_new_item\' => __( \'Dodaj nowe\', \'your-plugin-textdomain\' ),
\'new_item\' => __( \'Nowe\', \'your-plugin-textdomain\' ),
\'edit_item\' => __( \'Edytuj\', \'your-plugin-textdomain\' ),
\'view_item\' => __( \'Zobacz\', \'your-plugin-textdomain\' ),
\'all_items\' => __( \'Wszystkie\', \'your-plugin-textdomain\' ),
\'search_items\' => __( \'Szukaj\', \'your-plugin-textdomain\' ),
\'parent_item_colon\' => __( \'Nadrzędne drzewo:\', \'your-plugin-textdomain\' ),
\'not_found\' => __( \'Nie znaleziono.\', \'your-plugin-textdomain\' ),
\'not_found_in_trash\' => __( \'Nie znaleziono nic w koszu.\', \'your-plugin-textdomain\' )
);
$args = array(
\'labels\' => $labels,
\'description\' => __( \'Drzewa formowane bonsai\', \'your-plugin-textdomain\' ),
\'public\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_icon\' => \'dashicons-palmtree\', // dodaje ikonkę z Dashicons https://developer.wordpress.org/resource/dashicons
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
);
register_post_type( \'drzewa_formowane\', $args );
}
/**
register custom hierarchical taxonomy, like categories
**/
// https://codex.wordpress.org/Function_Reference/register_taxonomy
add_action( \'init\', \'drzewa_formowane_categories\', 0 );
function drzewa_formowane_categories() {
$labels = array(
\'name\' => _x( \'Kategorie\', \'taxonomy general name\', \'textdomain\' ),
\'singular_name\' => _x( \'Kategorie\', \'taxonomy singular name\', \'textdomain\' ),
\'search_items\' => __( \'Przeszukaj kategorie\', \'textdomain\' ),
\'all_items\' => __( \'Wszystkie kategorie\', \'textdomain\' ),
\'parent_item\' => __( \'Nadrzędna kategoria\', \'textdomain\' ),
\'parent_item_colon\' => __( \'Nadrzędna kategoria:\', \'textdomain\' ),
\'edit_item\' => __( \'Edytuj kategorię\', \'textdomain\' ),
\'update_item\' => __( \'Uaktualnij kategorię\', \'textdomain\' ),
\'add_new_item\' => __( \'Dodaj kategorię\', \'textdomain\' ),
\'new_item_name\' => __( \'Nowa nazwa kategorie\', \'textdomain\' ),
\'menu_name\' => __( \'Kategorie\', \'textdomain\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
);
register_taxonomy( \'drzewa_formowane_categories\', array( \'drzewa_formowane\' ), $args );
}
但是,当我添加$query->设置为“post\\u type”时,我无法接收到CPT帖子的任何结果。。。那么,我还能做些什么呢?