1. Home
  2. Docs
  3. WordPress Basic
  4. 커스텀 쿼리 루프 쇼트코드

커스텀 쿼리 루프 쇼트코드

function custom_query_shortcode($atts) {

   // EXAMPLE USAGE:
   // [loop the_query="showposts=100&post_type=page&post_parent=453"]

   // Defaults
   extract(shortcode_atts(array(
      "the_query" => ''
   ), $atts));

   // de-funkify query
   $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
   $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);

   // query is made               
   query_posts($the_query);

   // Reset and setup variables
   $output = '';
   $temp_title = '';
   $temp_link = '';

   // the loop
   if (have_posts()) : while (have_posts()) : the_post();

      $temp_title = get_the_title($post->ID);
      $temp_link = get_permalink($post->ID);

      // output all findings - CUSTOMIZE TO YOUR LIKING
      $output .= "<li><a href='$temp_link'>$temp_title</a></li>";

   endwhile; else:

      $output .= "nothing found.";

   endif;

   wp_reset_query();
   return $output;

}
add_shortcode("loop", "custom_query_shortcode");

 

비주얼 컴포저 빌더 구조를 간단히 연결하기 위한 쇼트코드 (응용)

function custom_query_shortcode($atts) {

    // EXAMPLE USAGE:
    // [linked_content the_query="page_id=1111&post_type=page"]

    // Defaults
    extract(shortcode_atts(array(
        "the_query" => ''
    ), $atts));

    $content_block_query = new WP_Query( $the_query );
    if ( $content_block_query->have_posts() ) {
        while ( $content_block_query->have_posts() ) { $content_block_query->the_post();
            the_content();   // 빌더 구성 그대로 (단, 배경 이미지 및 색은 style로 따로 지정해야 함) 
        }
    } 
    // Restore original Post Data    
    wp_reset_postdata();   

}
add_shortcode("linked_content", "custom_query_shortcode");

https://codex.wordpress.org/Shortcode_API
https://developer.wordpress.org/reference/functions/add_shortcode/