Limit number of pages that use a specific template?

You can approach this by first using a database query to count the number of pages that are already using the template: $query = “SELECT COUNT(*) as total FROM prefix_posts as p JOIN prefix_postmeta as m ON p.ID = m.post_id WHERE p.’post_type’ = ‘page’ AND p.’post_status’ = ‘publish’ AND m.’meta_key’ = ‘_wp_page_template’ AND m.’meta_value’ = … Read more

How to quickly switch custom post type singular template?

you can do it like this: //add movies_view to query vars add_filter(‘query_vars’, ‘my_query_vars’); function my_query_vars($vars) { // add movies_view to the valid list of variables $new_vars = array(‘movies_view’); $vars = $new_vars + $vars; return $vars; } then add a template redirect based on that query_var: add_action(“template_redirect”, ‘my_template_redirect’); // Template selection function my_template_redirect() { global $wp; … Read more

Multiple areas of dynamic content in a page

The example you gave has a listing of three pages and four posts (which are dated). The same can be achieved by one of the following: two different Custom Post Types, one per block, e. g. post_type=”products” and post_type=”projects”; children of two another pages, e. g. children of “Products” in one block and children of … Read more

Add Archive Page Template via Plugin

WordPress has a template_include filter which you can use. // Add a filter to ‘template_include’ hook add_filter( ‘template_include’, ‘wpse_force_template’ ); function wpse_force_template( $template ) { // If the current url is an archive of any kind if( is_archive() ) { // Set this to the template file inside your plugin folder $template = WP_PLUGIN_DIR .”https://wordpress.stackexchange.com/”. … Read more

Loading page content into a variable in template

You can always use the output buffering to store the printing contents in a variable. function return_get_template_part($slug, $name=null) { ob_start(); get_template_part($slug, $name); $content = ob_get_contents(); ob_end_clean(); return $content; } $content = return_get_template_part(‘content’, ‘page’); This would be most preferable to keep using the get_template_part() right now. An alternative would be to use locate_template() function but it … Read more

How to add custom template in plugin?

This plugin looks for page_contact.php from active theme’s folder and uses plugin’s templates/page_contact.php as a fallback. <?php /** * Plugin Name: Test Plugin */ add_filter( ‘template_include’, ‘contact_page_template’, 99 ); function contact_page_template( $template ) { $file_name=”page_contact.php”; if ( is_page( ‘contact’ ) ) { if ( locate_template( $file_name ) ) { $template = locate_template( $file_name ); } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)