Search
Close this search box.

wordpress 获取post_type 所有分类以及每个分类下五篇文章

<?php
/**
 * Plugin Name: join us pages
 * Author: SHOPEO
 * Version: 1.0
 * Description: YZ
 */ 
function register_widget_scripts() {
    wp_register_script( 'widget-script-1', plugins_url( '/assets/js/jquery.min.js', __FILE__ ) );
	wp_register_script( 'avalon', plugins_url( '/assets/js/avalon.js', __FILE__ ), array(), '2.0.0', true );
	wp_register_script( 'script-jquery', plugins_url( '/assets/js/code.js', __FILE__ ), array(), '2.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'register_widget_scripts' );
function register_widget_styles() {
	wp_enqueue_style('styles', plugins_url( '/assets/css/style.css', __FILE__ ));
}
add_action( 'wp_enqueue_scripts', 'register_widget_styles' );



add_action("wp_ajax_vote_new_lists", "vote_new_lists");   
add_action("wp_ajax_nopriv_vote_new_lists", "vote_new_lists");

function vote_new_lists(){
    $termes = get_terms('join-category',array(
        'orderby'    => 'date',
        'hide_empty' => 0
    ));
    foreach($termes as $key){
        $arg = array(
            'post_type' => 'join-us',
            'tax_query' => array(
                array(
                    'taxonomy' => 'join-category',
                    'field'    => 'term_id',
                    'terms'    => array($key->term_id),
                    'operator' => 'IN'
                )
            )
        );
        $quer_post = new WP_Query($arg);
        $key->posts = $quer_post->posts;
    }
    wp_send_json(array('lists'=>$termes));
}



add_shortcode('pageJoinus','pageJoinusFun');
function pageJoinusFun(){
    ob_start();
    ?>  
        <script src="/wp-content/plugins/se-join-us/assets/js/avalon.js?ver=2.0.0"></script>
        <script type="text/javascript">var ajax_url = '<?php echo admin_url(); ?>admin-ajax.php';</script>
        <style>
            .ms-controller{visibility: hidden;}
            .pagejoinbox{ display: grid;grid-template-columns: 285px auto; grid-template-rows: minmax(500px,auto);}
            .pagelefts{background-color: #191E26;border-left: 1px solid #9ea8b2;border-top: 1px solid #9ea8b2;border-right: 1px solid #9ea8b2; position: sticky; top: 0;}
            .pageright{background-color: #2d2e34;padding-bottom: 150px; box-sizing: border-box;}
            .faqs{width: 100%; overflow: hidden; display: flex;flex-direction: column;justify-content: center;align-items: center;}
            .pagejoinbox .faqsubs:not(:first-child) .submenus{display: none;}
            .pagejoinbox .faqsubs{display: flex;flex-direction: column;justify-content: center;align-items: center;width: 100%;}
            .pagejoinbox .faqsubs > a{display: block;width: 100%;height: 55px; line-height:55px; text-align: center;font-size: 21.33px;color: #fff; background-color: #236382;border-bottom: 1px solid #9ea8b2;}
            .pagejoinbox .faqsubs  .submenus{display: flex;flex-direction: column; width: 100%; background-color: #374254; padding: 30px;}
            .pagejoinbox .faqsubs  .submenus > a{color: #fff; text-align: center; margin-bottom: 10px; position: relative; display: inline-block; width: 100%; cursor: pointer; }
            .pagejoinbox .faqsubs  .submenus .actives::after{content: ''; display: inline-block;; width: 15px; height: 15px; background-color: #236382; border-radius: 15px;position: absolute; left: 40px; top: 50%; margin-top: -5.5px;}
            .rightbos{ display: flex;justify-content: center;align-items: center;flex-direction: column; padding: 80px; box-sizing: border-box;} 
            .rightbos .righttitle{ margin-bottom: 50px; font-size: 32px; color: #fff; font-family: "微软雅黑";}
            .rightcontent{line-height: auto; color: #fff; font-size: 21px;font-weight: 300; font-family: "微软雅黑";word-break:break-all;}
            @media screen and (max-width:850px){
                .pagejoinbox{display:flex;flex-direction: row;}
                .rightbos{padding: 15px!important; font-size: 14px; }
                .righttitle{ margin-bottom: 10px;}
                .pagelefts{position: sticky; top: 0; width: 35%; display: block;}
                .pagejoinbox .faqsubs  .submenus > a{font-size: 12px;}
                .pagejoinbox .faqsubs  .submenus .actives::after{content: ''; display: none; width: 15px; height: 15px; background-color: #236382; border-radius: 15px;position: absolute; left: 40px; top: 50%; margin-top: -5.5px;}
            }
        </style>
        <div class="pagejoinbox" ms-controller="new_list" class="ms-controller">
             <div class="pagelefts">
                 <div class="faqs">
                     <div class="faqsubs" ms-repeat="list">
                        <a href="javascript:void(0)" ms-click="slideToggle">{{el.name}}</a>
                        <div class="submenus">
                            <a ms-repeat-pl="el.posts" ms-click="changeContent(pl)" ms-class="{{pl.ID == curid?'actives':''}}">{{pl.post_title}}</a> 
                        </div>
                    </div>
                 </div>
             </div>
             <div class="pageright">
                 <div class="rightbos">
                     <div class="righttitle">{{contents.post_title || ''}}</div>
                     <div class="rightcontent" ms-html="contents.post_content"></div>
                 </div>
             </div>
        </div>
        <script>
            var vm = avalon.define({
                $id:'new_list',
                list:[],
                contents:{},
                curid:'',
                a:function(){
                    $.ajax({
                        url:ajax_url,
                        type:'post',
                        data:{
                            'action':'vote_new_lists'
                        },
                        success:function(respond){
                          vm.list =respond.lists;
                          vm.contents=vm.list[0].posts[0];
                          vm.curid = vm.contents.ID
                        }
                    })
                },
                slideToggle:function(){
                    $(this).next().slideToggle();
                },
                changeContent:function(pl){
                    vm.contents = pl;
                    vm.curid = pl.ID;
                }
            })
            vm.a();
        </script>
    <?php
    return ob_get_clean();
}
?>

发表评论

Optimized by WPJAM Basic