如何构造 html genesis loop (Wordpress)?
How to makeup html genesis loop (Wordpress)?
我想编辑默认存档模板。我创建了 archive.php 并添加了这段代码:
function mpp_body_class( $classes ) {
$classes[] = 'mpp-home';
return $classes;
}
add_filter( 'body_class', 'mpp_body_class' );
// Force content-sidebar layout setting
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
genesis();
如何为它调用post循环和化妆html?
您不一定需要创建 php 文件来自定义 genesis,
您可以直接在自定义 functions.php 或任何插件上简单地构建自定义循环
// hooking the modification on wp_head
add_action('wp_head', function() {
// check if we are on archive pages, if not just return back
if ( !is_archive() ) return;
//remove genesis default loop
remove_action( 'genesis_loop', 'genesis_do_loop' );
//add your custom loop
add_action( 'genesis_loop', 'your_custom_loop_archive' );
add_filter( 'body_class', 'mpp_body_class' );
// Force content-sidebar layout setting
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
});
function mpp_body_class( $classes ) {
$classes[] = 'mpp-home';
return $classes;
}
function your_custom_loop_archive() {
// needed to get the queried date for date archive
global $wp_query;
// get current archive details, this return object user data for user
//returns NULL for date archive, that's why we have to also use $wp_query object for date
$obj = get_queried_object();
//build query paramter to get all the post of this archive
$args = [
'orderby' => 'menu_order', // overide default orderby
'order' => 'ASC', // overide default orderb
'posts_per_page'=> '12', // overrides default posts per
];
//add author paramater for author archive
if ( is_author() ) $args['author'] = $obj->ID;
//add year & monthnum paramater for date archive
if ( is_date() ) $args = array_merge( $args, $wp_query->query );
//add tax_query paramater for taxonomy archive, includes categories & tags
//is_tax() will return false for post category & tag
//we need to include is_category() and is_tag()
// see https://core.trac.wordpress.org/ticket/18636
if ( is_tax() || is_category() || is_tag() ) {
$args['tax_query'] = [[
'taxonomy' => $obj->taxonomy,
'field' => 'id',
'terms' => $obj->term_id,
]];
}
//build new query
$loop = new WP_Query( $args );
//duh Loop
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post(); global $post;
// build your html template here
echo '<pre>', print_r( $post, 1), '</pre>'; // spit each post object data
endwhile;
endif;
}
我想编辑默认存档模板。我创建了 archive.php 并添加了这段代码:
function mpp_body_class( $classes ) {
$classes[] = 'mpp-home';
return $classes;
}
add_filter( 'body_class', 'mpp_body_class' );
// Force content-sidebar layout setting
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
genesis();
如何为它调用post循环和化妆html?
您不一定需要创建 php 文件来自定义 genesis,
您可以直接在自定义 functions.php 或任何插件上简单地构建自定义循环
// hooking the modification on wp_head
add_action('wp_head', function() {
// check if we are on archive pages, if not just return back
if ( !is_archive() ) return;
//remove genesis default loop
remove_action( 'genesis_loop', 'genesis_do_loop' );
//add your custom loop
add_action( 'genesis_loop', 'your_custom_loop_archive' );
add_filter( 'body_class', 'mpp_body_class' );
// Force content-sidebar layout setting
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
});
function mpp_body_class( $classes ) {
$classes[] = 'mpp-home';
return $classes;
}
function your_custom_loop_archive() {
// needed to get the queried date for date archive
global $wp_query;
// get current archive details, this return object user data for user
//returns NULL for date archive, that's why we have to also use $wp_query object for date
$obj = get_queried_object();
//build query paramter to get all the post of this archive
$args = [
'orderby' => 'menu_order', // overide default orderby
'order' => 'ASC', // overide default orderb
'posts_per_page'=> '12', // overrides default posts per
];
//add author paramater for author archive
if ( is_author() ) $args['author'] = $obj->ID;
//add year & monthnum paramater for date archive
if ( is_date() ) $args = array_merge( $args, $wp_query->query );
//add tax_query paramater for taxonomy archive, includes categories & tags
//is_tax() will return false for post category & tag
//we need to include is_category() and is_tag()
// see https://core.trac.wordpress.org/ticket/18636
if ( is_tax() || is_category() || is_tag() ) {
$args['tax_query'] = [[
'taxonomy' => $obj->taxonomy,
'field' => 'id',
'terms' => $obj->term_id,
]];
}
//build new query
$loop = new WP_Query( $args );
//duh Loop
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post(); global $post;
// build your html template here
echo '<pre>', print_r( $post, 1), '</pre>'; // spit each post object data
endwhile;
endif;
}