dream0614 2018-01-07 03:03
浏览 47
已采纳

如何构建html创世循环(Wordpress)?

I want edit default archive template. I created archive.php and add this code:

    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();

How to call post loop and makeup html for it?

  • 写回答

1条回答 默认 最新

  • dongyuan2388 2018-01-07 09:25
    关注

    You don't necessarily need to create a php file for customizing genesis,

    you can simply build a custom loop directly on custom functions.php or any plugin

    // 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突