将 wordpress 过滤变成函数

我有一个按自定义分类过滤的 wordpress 页面列表。如何将其转换为 wp 函数以与简码一起使用?

这是工作过滤:


<?

$custom_terms = get_terms('csgroup');


foreach($custom_terms as $custom_term) {

    wp_reset_query();

    $args = array('post_type' => 'page', 'csgroup' => 'digital-expert-group',

    // $args = array('post_type' => 'page', 'csgroup' => 'management-consultants-group',

        'tax_query' => array(

            array(

                'taxonomy' => 'csgroup',

                'field' => 'slug',

                'terms' => $custom_term->slug,

            ),

        ),

     );


     $loop = new WP_Query($args);

     if($loop->have_posts()) {

        echo '<h2>'.$custom_term->name.'</h2>';


        while($loop->have_posts()) : $loop->the_post();

            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';

        endwhile;

     }

}


wp_reset_postdata();


?>

我想得到这个:


function l1category_list_func(){


==my php code with list of pages==


}


add_shortcode( 'l1category_list', 'l1category_list_func' );


一只甜甜圈
浏览 88回答 1
1回答

哔哔one

如果我正确理解你的问题,你想使用 return 语句返回你之前回显的输出的串联字符串。function l1category_list_func(){&nbsp; &nbsp; $output = '';&nbsp; &nbsp; $custom_terms = get_terms('csgroup');&nbsp; &nbsp; foreach($custom_terms as $custom_term) {&nbsp; &nbsp; &nbsp; &nbsp; wp_reset_query();&nbsp; &nbsp; &nbsp; &nbsp; $args = array('post_type' => 'page', 'csgroup' => 'digital-expert-group',&nbsp; &nbsp; &nbsp; &nbsp; // $args = array('post_type' => 'page', 'csgroup' => 'management-consultants-group',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'tax_query' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy' => 'csgroup',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'field' => 'slug',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'terms' => $custom_term->slug,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$loop = new WP_Query($args);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($loop->have_posts()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<h2>'.$custom_term->name.'</h2>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($loop->have_posts()) : $loop->the_post();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; wp_reset_postdata();&nbsp; &nbsp; return $output;}
打开App,查看更多内容
随时随地看视频慕课网APP