最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress shortcode for a section Template

programmeradmin2浏览0评论

I have made a Section Template called section-bachelorscholarships.php in the Folder frontpage in the Theme Folder and I have put this Code

    <?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>

then I put this code into the Function.php

         function my_master_shortcode( $attr ) {
         ob_start();
         get_template_part('frontpage/section', 'bachelorscholarships');
         return ob_get_clean();
           }
         add_shortcode( 'mastergrants', 'my_master_shortcode' );

My problem is when I load the Front page to see the Update is still loading without End and getting stuck with it, Any Help

I have made a Section Template called section-bachelorscholarships.php in the Folder frontpage in the Theme Folder and I have put this Code

    <?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>

then I put this code into the Function.php

         function my_master_shortcode( $attr ) {
         ob_start();
         get_template_part('frontpage/section', 'bachelorscholarships');
         return ob_get_clean();
           }
         add_shortcode( 'mastergrants', 'my_master_shortcode' );

My problem is when I load the Front page to see the Update is still loading without End and getting stuck with it, Any Help

Share Improve this question edited Mar 29, 2020 at 22:59 Ahmad Al Hariri asked Mar 29, 2020 at 21:18 Ahmad Al HaririAhmad Al Hariri 215 bronze badges 11
  • Why is your posts_per_page set to negative 4? I fixed the code block formatting in your question so it's viewable, but can you indent your code correctly? It's extremely difficult to read – Tom J Nowell Commented Mar 29, 2020 at 21:30
  • Sorry, This is my first time I write a quistion here and I really do not know how to deal with stack Exchange Forms. I just need to display four Posts in this part of the Template that Why I made it -4 as I know, Should I turn it off to jus 4 ? – Ahmad Al Hariri Commented Mar 29, 2020 at 21:38
  • Yes, should just be 4. – mikerojas Commented Mar 29, 2020 at 21:41
  • The only case I have seen using a negative number for posts_per_page is using -1 which will return all posts that match the query. – mikerojas Commented Mar 29, 2020 at 21:42
  • I have tried to put it 'posts_per_page'=> 4, but is still have the Problem which I have – Ahmad Al Hariri Commented Mar 29, 2020 at 21:48
 |  Show 6 more comments

1 Answer 1

Reset to default 0

I have found the Solution with another Code but still not know what was the Problem with the First Code . It is solved . the Code which I will use will be :

               <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/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' );
发布评论

评论列表(0)

  1. 暂无评论