Is it safe/recommended to use wp_enqueue_script function outside the functions.php file?

I created multiple post formats, and when i use an audio post format, i want to include some additional javascript for the audio player. So, i include the content-audio.php like this: <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( ‘blog/content’, get_post_format() ); ?> <?php endwhile; ?> This loads the blog/content-audio.php as expected: <article … Read more

get_template_part() does not work if you call it when you are in a subfolder

Say for example you have a directory like this: theme – subfolder – template.php content-job-listing.php If I try and call get_template_part like so get_template_part(‘content’, ‘job-listing’) from the file template.php (note this is just a generic name not that actual name I’m using) it returns NULL. Similarly, if I use get_template_part(‘../content’, ‘job-listing’) this also fails to … Read more

Combining shortcode and get_template_part

I manage a wordpress site and have created a template part that I usually paste below the content. <div class=”posts-group”> <h2>Featured store products</h2> <?php $catquery = new WP_Query( ‘post_type=product&posts_per_page=4&orderby=rand’ ); while($catquery->have_posts()) : $catquery->the_post(); ?> <a href=”https://wordpress.stackexchange.com/questions/213705/<?php echo get_post_meta($post->ID,”PRODUCT-url’, true);?>”> <article class=”post home-post homeproduct”> <div class=”post-thumbnail-img”> <?php the_post_thumbnail(‘small-thumbnail’); ?> </div> <h2><?php the_title(); ?></h2> <p></p> </article> </a> … Read more

get_template_part from plugin

This is default get_template_part function inside WordPress: function get_template_part( $slug, $name = null ) { do_action( “get_template_part_{$slug}”, $slug, $name ); $templates = array(); if ( isset($name) ) $templates[] = “{$slug}-{$name}.php”; $templates[] = “{$slug}.php”; locate_template($templates, true, false); } I am trying to use that action for locating custom post type loop file from plugin: add_action( “get_template_part_templates/loop”, … Read more

How to add custom content template part for a custom post type on main query using a plugin

I have a custom post type paper defined in my WordPress plugin that I show along with standard posts in the main query via: function add_custom_post_types_to_query( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( ‘post_type’, array( ‘post’, ‘paper’ ) ); // As was pointed out by Ihor Vorotnov this return is not necessary. … Read more

Check if partial file is called from within header.php or footer.php

I am including a partial file both in header.php and in footer.php with get_template_part(‘content-form’); Is there an if clause I can use to check from where the file is being called? If it is called from within footer.php, then I would like to add a class name. <div class=”default-class <?php if (called_from_footer) echo ‘footer-class’; ?>”> … Read more

When is get_template_part() preferable to simply using the template.php files?

In this post on wordpress.stackexchange.com I asked whether using get_template_part(), as demonstrated in the TwentyTen theme, is a recommended best practice. The general consensus I got was that it was not necessarily the best practice in all situations. This related question then is: can you provide me with an example where using get_template_part() would be … Read more

How to make get_template_part always check child theme first?

How can I force WP to always check the child theme folder first when running get_template_part? Example: child theme loads get_template_part(‘content’, ‘inventory’) in single.php. Because all child themes (and there are a lot) share the same common inventory template base, the file content-inventory.php is in the parent theme. So far so good. I would like … Read more