Php 如何组成html genesis循环(Wordpress)?
我想要编辑默认存档模板。我创建了archive.php并添加了以下代码:Php 如何组成html genesis循环(Wordpress)?,php,wordpress,loops,genesis,Php,Wordpress,Loops,Genesis,我想要编辑默认存档模板。我创建了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_o
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 定制genesis不一定需要创建php文件 您可以直接在custom 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;
}
//将修改挂接到wp\U头上
添加操作('wp_头',函数(){
//检查我们是否在存档页面上,如果不在,请返回
如果(!is_archive())返回;
//删除默认循环
移除动作(“genesis_循环”、“genesis_do_循环”);
//添加自定义循环
添加_操作('genesis_loop','your_custom_loop_archive');
添加过滤器(“主体类”、“mpp主体类”);
//强制内容侧栏布局设置
添加过滤器(“genesis\u pre\u get\u option\u site\u layout”,“\u genesis\u return\u full\u width\u content”);
});
函数mpp_body_class($classes){
$classes[]='mpp home';
返回$classes;
}
函数您的\u自定义\u循环\u存档(){
//需要获取日期存档的查询日期
全局$wp_查询;
//获取当前存档详细信息,此操作将返回用户的对象用户数据
//对于日期存档返回NULL,这就是为什么我们还必须对日期使用$wp_查询对象
$obj=get_queryed_object();
//生成查询参数以获取此存档的所有帖子
$args=[
'orderby'=>'menu_order',//覆盖默认orderby
'order'=>'ASC',//覆盖默认医嘱B
'posts_per_page'=>'12',//覆盖每个页面的默认帖子
];
//为作者存档添加作者参数
如果(is_author())$args['author']=$obj->ID;
//为日期存档添加年份和月份参数
如果(is_date())$args=array_merge($args,$wp\u query->query);
//为分类法存档添加tax_查询参数,包括类别和标记
//is_tax()将为帖子类别和标签返回false
//我们需要包括is_category()和is_tag()
//看https://core.trac.wordpress.org/ticket/18636
如果(is_tax()| is_category()| | is_tag()){
$args['tax_query']=[[
“分类法”=>$obj->taxonomy,
“字段”=>“id”,
“术语”=>$obj->term\u id,
]];
}
//生成新查询
$loop=新的WP_查询($args);
//杜氏环
如果($loop->have_posts()):
while($loop->have_posts()):$loop->the_post();global$post;
//在这里构建html模板
回显“”,打印($post,1),“”;//吐出每个post对象数据
结束时;
endif;
}