Using get_tmplate_part() function in Wordpress - wordpress

I am making a wordpress theme, in which I have different skins and templates, Is it a good idea to use get_template_part() function to load different chunks/templates instead of having one single file. I find it useful however my only concern is, will this function put load on server or effect the performance in any way ? I am using it like below:
/* Post Tags Template */
get_template_part( 'templates/post_tags' );
/* Post Meta Template */
get_template_part( 'templates/post_meta' );

Related

Removing wordpress theme from particular pages

I have a custom Wordpress theme that includes a header, footer, and a few other elements. It's great, but there are a few pages that I want to style completely differently. One of them is, for example, a landing page, where having the typical header makes no sense. How can I tell Wordpress not to use any themes on this page? Is this even possible?
You can use Page Templates
For example, if you would like to use custom header or footer for your landing page you can do following in a separate .php file and declare it as a Page Template.
I'm assuming your header and footer file name header-landing.php and footer-landing.php
<?php
/**
* Template Name: Landing Page
*/
get_header( 'landing' ); ?>
// Landing Body
<?php get_footer( 'landing' ); ?>
If you wish no header or footer then, you can just omit get_header() or get_footer() function call.
You can use conditionals to decide when to load certain stylesheets onto certain pages.
For example, in your functions.php, you can use wp_enqueue_style to load in different stylesheets. In your php, you would say something like this:
function custom_page_style_sheet() {
if (is_page() ):
wp_enqueue_style( 'custom-page-styling', get_stylesheet_directory_uri() . '/custom-page.css' );
}}
add_action('wp_enqueue_scripts', 'custom_page_style_sheet');
Try this tutorial, which is where I grabbed the example code from:
http://wpsites.net/wordpress-themes/second-style-sheet-theme/

What is the right place for reusable render functions in WordPress theme?

Let's say I have a reusable peace of HTML used on different pages in my theme.
In order to not duplicate myself I want to put this peace of HTML into some place and reuse it later. And because it has some parameters (needed for rendering) - it should be a function.
It seems to me that I cannot use get_template_part function because I want to pass some parameters into the render function.
functions.php feels more like a place for low level theme functionality.
What is the right place for render functions in WordPress?
UPD I've just found that in _s theme that I am using as a base code for my theme they have inc/template-tags.php. It feels like a good place. Is it the right way?
check codex set_query_var and get_query_var
In main template
set_query_var('customer_name', $customer_name);
get_template_part( $slug, $name );
template part retrieve it as
$customer_name= get_query_var( 'customer_name', 1 );
echo $customer_name;
Ok so in _s theme they suggest:
Custom template tags in inc/template-tags.php that keep your templates clean and neat and prevent code duplication.
Seems like a good place was found :)

Blog page with theme completely different from the main theme?

I have been trying to make my website on Wordpress. I am using Brave Zeenat as my primary and Grido as my blog theme.
I have read many tutorials in places, which discuss how to apply a customized flavour of the main theme on a static page, e.g. Blog. However, the main theme I am using does not appeal to me as a blog theme at all, so I wanted to do something entirely different, so I have tried two methods.
First, I tried to just create a page named Blog and force it to take a theme of my choice using the Page Theme plugin. That worked instantly, but the blog page is empty and would not accept articles of certain categories by default like this.
Second, I tried to not use any plugin at all, and use a custom PHP file instead, which sets some loops and calls a theme. This file blog.php had to be in the main theme directory, otherwise it would not be applicable as a template from the page settings in Wordpress dash.
So I put it with my main theme, but call to load the other theme, like this:
<?php
/*
Template Name: Blog
*/
$paged = get_query_var('paged');
query_posts('cat=0&paged='.$paged);
global $more;
$more = 0;
load_template(WP_CONTENT_DIR . '/themes/grido_v1.0.1/index.php');
?>
Eventually I only want to see category no.9, but for now, I left it as 0, which should display all categories. But when I run this with Page Theme plugin disabled, I get this error: Fatal error: Call to undefined function themify_get() in /var/sites/v/visualdeceptions.info/public_html/wp-content/themes/grido_v1.0.1/index.php on line 10.
Now, although this is a themify error, I am sure if I try to use other premium themes as well, I will encounter very similar errors, because I have only set a custom php file, and no style, header, footer, etc. But I am not sure how to do it.
Try to add getheader() and getfooter() in the code
<?php
/*
Template Name: Blog
*/
get_header(); //HERE
$paged = get_query_var('paged');
query_posts('cat=0&paged='.$paged);
global $more;
$more = 0;
load_template(WP_CONTENT_DIR . '/themes/grido_v1.0.1/index.php');
get_footer(); //HERE
?>

Replacing WordPress core functionality with a plugin

I'm creating a WordPress plugin for a custom menu layout. I am well aware that I could just as easily implement this menu directly into the theme and I've read up quite thoroughly on the features and limitations of wp_nav_menu(), plus I have already tried and tested every plugin already created for replacing the default WordPress menu.
I wish to use a plugin since my client will be implementing this on several different WordPress sites, many of which run on different themes - and most of those are themes which I did not create and I do not wish to re-write their code in case they update the theme in the future.
When I've looked into a way to implement the menu into the theme I found that there are only two good methods since there is no hook or filter called at menu display time. The first is to change the theme to look for the plugin (this is similar to the method used by PixoPoint and many other menu plugins):
header.php:
if(function_exists('pixopoint_menu')){
pixopoint_menu();
} else {
wp_nav_menu();
}
The second method is a walker class:
plugin.php:
class my_walker_class Extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args) {
/*
* Etc. Etc.
*/
}
}
header.php:
wp_nav_menu( Array( 'walker' => 'my_walker_class' ) );
However as you'll note both of these methods require a modification to the standard header.php file.
Ideally I would like to simply replace the wp_nav_menu() function if my plugin is loaded, as this would give my plugin support for the majority of themes without having to edit any of the theme files. Is there a good way to do this? Or is there a better way to write a menu plugin which I am not seeing?
You can use the wp_nav_menu_args filter to modify the arguments passed to wp_nav_menu, so you can specify your custom walker class.
add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args_filter');
function my_wp_nav_menu_args_filter($args = array()) {
$args['walker'] = new my_walker_class();
return $args;
}

Wordpress: how to create a template that is still available when you switch theme?

I have a custom template for my portfolio page. However, after switching theme, it's gone because it is in my previous theme's folder.
Technically, I can copy that file into my new theme folder. However, I plan to change theme every two weeks and this becomes non-trivial. Is there a way to always have a bunch of common template files available to me no matter when and how often I switch theme? (In other words, I want to create template files that are not dependent on themes.)
Thanks!
There is, using template_redirect, which you would put in the functions.php file.
function uniquename_default_template() {
global $wpdb;
if(get_post_type() == 'posttype') : /* You could use is_single() instead of get_post_type() == '' or any type of conditional tag) */
include(TEMPLATEDIR . 'path/to/theme/file.php'); /* You could use TEMPLATEDIR to get a file from a template folder, or PLUGINDIR to get a file from the plugins directory - doesn't support HTTP requests */
exit; endif;
}
add_action('template_redirect', 'uniquename_default_template');
Hope it helps.

Resources