Community Forums › Forums › General Genesis Framework Discussions › Custom search.php template for Genesis Framework
- This topic has 0 replies, 1 voice, and was last updated 1 year, 3 months ago by
annahernandez99.
-
AuthorPosts
-
March 11, 2021 at 8:49 am #503420
annahernandez99
ParticipantI have setup a custom search.php template within the Genesis Framework and I've created an array variable for specific post types:
$post_types = array('treatment','post', 'team_member' );
However, I need to edit which fields are pulled in for each of the arrays as two out of three are custom post types built with ACF Pro (one is the default WP posts).
Please find the most up to date code below. Ideally, I want to set the number of posts that appear for the three arrays ('treatment','post', 'team_member' ) and what fields are pulled in.
<?php
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );/* Remove the default loop */
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'hs_do_search_loop' );
add_action( 'genesis_before_loop', 'genesis_do_search_title' );/**
* Echo the title with the search term.
*
* @since 1.9.0
*/
function genesis_do_search_title() {$title = sprintf( '%s %s', apply_filters( 'genesis_search_title_text', __( 'Search Results for:', 'genesis' ) ), get_search_query() );
echo apply_filters( 'genesis_search_title_output', $title ) . "\n";
}
/**
* Outputs a custom loop
*
* @global mixed $paged current page number if paginated
* @return void
*/
function hs_do_search_loop() {
// create an array variable with specific post types in your desired order
$post_types = array('treatment','post', 'team_member' ); // other custom post type can be added.
echo '<div class="search-content">';
foreach ( $post_types as $post_type ) {
// get the search term entered by user
$s = isset( $_GET["s"] ) ? $_GET["s"] : "";
// accepts any wp_query args
$args = (array(
's' => $s,
'post_type' => $post_type,
'posts_per_page' => 4,
'order' => 'ASC'
));
switch ( $post_type ) {
case 'treatment':
$post_type_heading = 'Treatments';
break;case 'post':
$post_type_heading = 'Latest News';
break;case 'team_member':
$post_type_heading = 'Consultants';
break;
}
echo '<div class="post-type '. $post_type .'"><div class="post-type-heading">'. $post_type_heading . '</div>';
// force content limit.
add_filter( 'genesis_pre_get_option_content_archive_limit', 'sk_content_limit' );
// modify the Content Limit read more link.
add_filter( 'get_the_content_more_link', 'sp_read_more_link' );
// remove archive pagination.
remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' );
// custom genesis loop with the above query parameters and hooks.
genesis_custom_loop( $args );
echo '</div>';
}
}
echo '</div>'; // .search-content
function sk_content_limit() {
return '150'; // number of characters.
}
function sp_read_more_link() {
return '... Read more';
}
function sk_show_excerpts() {
return 'excerpts';
}
genesis(); -
AuthorPosts
- You must be logged in to reply to this topic.