Title: How to repeat a code
Last modified: September 19, 2021

---

# How to repeat a code

 *  [gakho209](https://wordpress.org/support/users/gakho209/)
 * (@gakho209)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-repeat-a-code/)
 * Dear pros,
 * I have a code “post by each category”. I want to use this code for more category
   but no need write all code again. Please help. Thank you so much
    Ex this code:
 *     ```
       <div class="related-post">
       	<div class="related-thumb">
       	<?php if ( has_post_thumbnail() ): ?>
       		<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'really-simple-thumb-s', [ 'class' => 'related-thumb' ] ); ?></a>
       			<?php elseif ( get_theme_mod('placeholder') != 'off' ): ?>
       		<img class="related-thumb" src="<?php echo esc_url( get_template_directory_uri() ); ?>/img/thumb-medium.png" alt="<?php the_title_attribute(); ?>" />
       	<?php endif; ?>	
       	</div>
       		<div class="related-category">
             <?php 
               if ( has_category() ) { the_category( ', ' ); }
             ?>
           </div>
       	<div class="related-inner">
       		<div class="related-title">	
       		<a href="<?php the_permalink();?>"><?php echo wp_trim_words( get_the_title() , 15 ) ?></a>
       		</div>
       		<div class="related-excerpt"><?php echo wp_trim_words( get_the_excerpt() , 50 ) ?></div>
       	</div>
   
       </div>
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Davood Denavi](https://wordpress.org/support/users/binarywc/)
 * (@binarywc)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-repeat-a-code/#post-14885920)
 * You probably need to write some custom php ir javascript within a child theme
   to be sure your customizing sticks for the specific category.
 * More on child themes: [https://developer.wordpress.org/themes/advanced-topics/child-themes/](https://developer.wordpress.org/themes/advanced-topics/child-themes/)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-repeat-a-code/#post-14886842)
 * The code you posted simply outputs the current post within a loop. It’s likely
   within code that actually drives the loop. Which posts are looped through depend
   upon the query made. You may query for posts in multiple categories, but the 
   list will not be sorted by category, that’s not a possible option with the usual
   WP_Query class. Ordering posts by category would require a custom query or custom
   PHP code to re-sort the posts. Pagination is difficult and inefficient with the
   sort with PHP option if there are a lot of posts in the query results.
 * Another possibility would be to make individual queries for posts in each category,
   making a separate loop for each. The same output code could be reused by nesting
   loops. If you have a lot of categories to query for this will not be a very efficient
   option. Preventing duplicate listing of posts which have multiple terms might
   be something you’d want to address as well. This approach can also be difficult.
 * The easiest solution is to simply list category terms, each term linking to an
   archive page for that specific category term.
 * Like Davood said, any of these can be implemented through a child theme.
 *  Thread Starter [gakho209](https://wordpress.org/support/users/gakho209/)
 * (@gakho209)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-repeat-a-code/#post-14896158)
 * Hi pros,
    That’s a example code only. I just want to define this code by a function
   or script…, then I can call it when show a list of any category. Because I don’t
   want to write the page with very long code but same. It’s possible? How to do
   it?
 * Thank you.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-repeat-a-code/#post-14898241)
 * I think I understand now, sorry for my misunderstanding earlier.
 * One approach would be to simply wrap it in a function declaration. For example
 *     ```
       <?php
       function do_my_thing() { ?>
   
       <!-- your code goes here -->
   
       <?php } ?>
       ```
   
 * Where ever you want the code to execute, call the function:
    `<?php do_my_thing();?
   >`
 * Function declarations are normally done on a theme’s functions.php or a plugin
   file. You do have to mind variable scope within functions. Any variables are 
   local to the function. As long as the other functions called within this function
   are working off the global $post object, as they appear to be, all will be fine.
 * As this appears to be template code, there’s a more “WordPressy” option. You 
   could place your code in its own template file. Then call this partial template
   with [get_template_part()](https://developer.wordpress.org/reference/functions/get_template_part/).
   The parameters for this are a little odd. Let’s say your template file is in 
   the file templates/page-category.php, relative to the main theme folder. On another
   template file, you’d include this one with:
    `get_template_part('templates/page','
   category');` The template part code always concatenates the joining `'-'` and
   terminal `'.php'`, so do not include these in your parameters.
 * A reminder: when dealing with custom templates and code, it’s best to create 
   a child theme to contain your work. `get_template_part()` only works for themes(
   including child themes), don’t try to use it to load plugin code that might be
   in template form.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to repeat a code’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-repeat-a-code/#post-14898241)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
