2016-10-18 2 views
-1

저는 WordPress 개발자입니다. 고급 맞춤 입력란을 사용하여 첫 번째 맞춤 템플릿 페이지를 만들고 루프를 관리했습니다.내 WP_Query 루프의 단축 코드를 만드는 방법은 무엇입니까?

 <?php 
      $args = array(
       'post_type' => 'art', 
       'orderby' => 'title', 
       'order' => 'ASC' 
      ); 
      $the_query = new WP_Query($args);   
     ?> 
     <?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 

     <?php get_template_part('content', 'art'); ?> 

     <?php endwhile; endif; ?> 

그러나 템플릿 페이지 내부뿐만 아니라 원하는 곳에서도 사용하고 싶습니다. 따라서 짧은 코드를 만들어야합니다.

예 :

function foobar_func($atts){ 
    return "foo and bar"; 
} 
add_shortcode('foobar', 'foobar_func'); 

내 질문은 다음과 같습니다 어떻게 내가 내 단축 코드 내부의 루프를 넣을 수 있습니다?

답변

0
add_shortcode('foobar', 'foobar_func'); 
function foobar_func($atts) { 
global $post; 
$output = ''; 
$args = array(
      'post_type' => 'art', 
      'orderby' => 'title', 
      'order' => 'ASC', 
      'posts_per_page' => 10, 

); 
$fe_query= new WP_Query($args); 
if ($fe_query->have_posts()) { 
    $output .= '<ul class="fe-query-results-shortcode-output">'; 
    while ($fe_query->have_posts()) { 
     $fe_query->the_post(); 

     $title = get_the_title(); 
     $link = get_the_permalink(); 

     $output .= "<li><a href=\"{$link}\">{$title}</a></li>"; 
     } 
     $output .= '</ul>'; 
     } else { 
    $output .= '<div class="fe-query-results-shortcode-output-none">No  results were found</div>'; 
    } 

wp_reset_postdata(); 
return $output; 
} 
+0

감사하지만 아쉽게도 제대로 동작하지 않습니다. = / –

0
<?php 
function loop_art() { 
     ob_start(); 
     get_template_part('loop_art'); 
     return ob_get_clean(); 
    } 
    add_shortcode('loop_art', 'loop_art'); 
?> 

<?php 
       $args = array(
        'post_type' => 'art', 
        'orderby' => 'title', 
        'order' => 'ASC' 
       ); 
       $the_query = new WP_Query($args);   

      if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post() 
        ?> 

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
    <header class="entry-header"> 

     <h1 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> 

     <div class="entry-meta"> 
      <p>Price: $<?php the_field('price'); ?></p> 
     </div><!-- .entry-meta --> 

    </header><!-- .entry-header --> 

    <div class="entry-content"> 

     <p><img src="<?php the_field('image'); ?>" alt="Example image of <?php the_title(); ?>"></p> 


    </div><!-- .entry-content --> 

</article> 

       <?php endwhile; endif; ?> 
관련 문제