分区模板的WordPress快捷代码

时间:2020-03-29 作者:Ahmad Al Hariri

我制作了一个名为Section bachelorscholarships的节模板。php在文件夹frontpage中的主题文件夹中,我已经把这段代码

    <?php while ( have_posts() ) : the_post(); ?>
    <?php  $args = array(
        \'post_type\'=> \'grants \' ,
        \'scholarship_degree\'=> \'bachelor\',
        \'status\'=>\'publish\',
        \'posts_per_page\' => 4,
        \'tax_query\' => array(
                      array(
                            \'taxonomy\' => \'expired\',
                            \'field\'    => \'slug\',
                            \'terms\'    => array( \'expired\' ),
                            \'operator\' => \'NOT IN\',)),);
    $q    = new WP_Query( $args );
    ?>
    <div class="row">
      <?php while ( $q->have_posts() ) : $q->the_post(); ?>
        <div class="col-sm-3" style="padding: 0px;">  
           <a href="<?php the_permalink(); ?>"  >
                <div class="card">
                <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID),\'thumbnail\' ); ?>
                <img  class="card-img-top" src="<?php echo $url ?>" />    
                <div class="card-body">
                     <p class="card-title"><?php the_title();?> </p>
                        <p class="card-text">
                              <div class="text-muted">
                                 <p><?php $terms = wp_get_post_terms($post->ID, \'funding-scholarship\');
                                      if ($terms) {$out = array( \'name\'=>\' \' );              
                                            foreach ($terms as $term) {
                                              $out[] = \'<a class="text-muted\' .$term->slug .\'" href="\' .get_term_link( 
                                              $term->slug, \'funding-scholarship\') .\'">\' .$term->name .\'</a>\';}
                                              echo join( \', \', $out ); }?></small></p> 
                                        </p>
                                      </div>  
                                    </div>  
                                  </div>
                                </div> 
                          <?php endwhile; ?>
                          </div>
                          <?php wp_reset_postdata(); ?>
                          <?php endwhile; ?> 
                      </div>
然后我将此代码放入函数中。php

         function my_master_shortcode( $attr ) {
         ob_start();
         get_template_part(\'frontpage/section\', \'bachelorscholarships\');
         return ob_get_clean();
           }
         add_shortcode( \'mastergrants\', \'my_master_shortcode\' );
我的问题是,当我加载首页时,看到更新仍在无休止地加载并被卡住,有什么帮助吗

1 个回复
SO网友:Ahmad Al Hariri

我已经用另一个代码找到了解决方案,但仍然不知道第一个代码的问题是什么。问题解决了。我将使用的代码是:

               <div class="card-deck">
               <?php 
               // the query  
               $wpb_all_query = new WP_Query(array(
                                  \'post_type\'=>\'grants\',
                                  \'scholarship_degree\'=> \'bachelor\',
                                  \'post_status\'=>\'publish\',
                                  \'posts_per_page\'=>  4,
                                  \'tax_query\' => array(array(
                                    \'taxonomy\' => \'expired\',
                                     \'field\'    => \'slug\',
                                     \'terms\'    => array( \'expired\' ),
                                     \'operator\' => \'NOT IN\',
                                    )),
                                )); 
            ?> 
            <?php if ( $wpb_all_query->have_posts() ) : ?>
                  <!-- the loop -->
               <div class="row">           
                    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
                       <div class="col-sm-3" > 
                           <a href="<?php the_permalink(); ?>">
                             <div class="card">
                              <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \' \' ); ?>
                              <img  class="card-img-top" src="<?php echo $url ?>" /> 
                                  <div class="card-body">
                                    <p class="card-title"><?php the_title();?> </p>
                                         </a>
                                          <p class="card-text">
                                        <div class="text-muted">
                                    <p><?php $terms = wp_get_post_terms($post->ID, \'funding-scholarship\');
                                              if ($terms) {
                                              $out = array(
                                              \'name\'=>\' 
                                                  <svg class="bi bi-bookmark" width="1em" height="1em" viewBox="0 0 16 16"                                                                          fill="black"xmlns="http://www.w3.org/2000/svg">
                                                   <path fill-rule="evenodd" d="M8 12l5 3V3a2 2 0 00-2-2H5a2 2 0 00-2 2v12l5-3zm-4 1.234l4-2.4 4 2.4V3a1 1 0 00-1-1H5a1 1 0 00-1 1v10.234z" clip-rule="evenodd"/>
                                                  </svg> تمويل المنحة     \');
                                  foreach ($terms as $term) {
                                  $out[] = \'<a class="text-muted\' .$term->slug .\'" href="\' .get_term_link( $term->slug, \'funding-scholarship\') .\'">\' .$term->name .\'</a>\';}
                                   echo join( \', \', $out ); }?></small></p> 
                          </div>  
                      </div>  
                  </div>
               </div>  
                  <?php endwhile; ?>
                  <!-- end of the loop -->
                  <?php wp_reset_postdata(); ?>
                  <?php else : ?>
                  <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p> 
                  <?php endif; ?>  
                  </div>  
                  </div> 


          AND THE SHORTCODE WHICH I WILL USE :
          function my_bachelor_shortcode( $attr ) {
                       ob_start();
                       get_template_part(\'frontpage/section\', \'bachelorscholarships\');
                      return ob_get_clean();
          }
          add_shortcode( \'bachelorgrantslist\', \'my_bachelor_shortcode\' );

相关推荐

Custom Post type shortcodes

我使用高级自定义字段在我的主题中创建自定义帖子类型(功能)。我想知道如何创建自定义帖子类型的短代码。因此,我只使用任何页面的自定义帖子类型的短代码来显示我在自定义帖子类型(功能)中添加的信息。