I wanted to display the header of a particular theme. I have done by writing this code in the template like below :
<?php
/*
Template Name:CLIENT LIST
*/
get_header("header1");
?>
Is this correct ? I have named the file as header1.php. I have already header.php.I wanted to make another header file so i named it as header1.php.
The documentation shows you the correct approach:
<?php get_header( $name ); ?>
$name
(string) (optional) Calls for header-name.php.
Default: None
So, name your file header-something.php and call it with get_header( 'something' );
Related
I'm building WordPress site.
I'm trying to do this
get_template_part( 'templates/blocks/section-header-page.php' );
My folder structure looks like this:
-mainFOlder
--assets
--templates
---blocks {
----section-header-page.php
----section-contact.php
}
How do I access the files in that folder? With get template part.
get_template_part() takes two parameters the first is the $slug or location of the template and slug. The second the name of the actual template.
get_template_part( string $slug, string $name = null );
So in your case this should work.
get_template_part( 'templates/blocks/section' , 'header-page' );
get_template_part( 'templates/blocks/section', 'contact');
Can't confirm, I have not tested it.
All you need to do is get rid of the ".php" on your template string.
Wordpress automatically assumes it's a php file so no extension is needed.
get_template_part( 'templates/blocks/section-header-page' );
I'd like to be able to edit my WordPress theme's CSS by using a Page in the backend instead of the default style.css file. I've seen it done before as a page template but I can't seem to figure it out myself.
I'm using WordPress version 4.7.2 on a Multisite. I want the CSS to generate on theme activation, hence using a Page.
I apologize in advance if this is an easy fix and open to other ways to accomplish this. Thanks!
I highly recommend using a plugin, such as https://wordpress.org/plugins/wp-add-custom-css/ or use the WordPress Customizer "Additional CSS" field.
However, to answer your question, this was easy since it's a modified way that I make some pages I load with ajax. You will need to create a page template and that requires FTP and a code editor to create the page and then push it your theme folder (hopefully a child theme or this template will go away when you upgrade).
I named my CSS page template: css-page.php
It requires only the opening php tag.
CODE for template css-page.php
<?php
/**
*
* Template Name: CSS page
* Not a recommended idea.
*
*/
header("Content-type: text/css; charset: UTF-8");
ob_start("compress");
//minify CSS
function compress( $minify ) {
/* remove comments */
$minify = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minify );
/* remove tabs, spaces, newlines, etc. */
$minify = str_replace( array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $minify );
return $minify;
}
//get the content
if (have_posts()) :
while ( have_posts() ) : the_post();
$content = get_the_content();
$content = wp_filter_nohtml_kses( $content );
echo $content;
endwhile;
endif;
ob_end_flush();
Then in your functions.php file in your child theme (or an include of that file or a custom plugin) paste in the following BUT change it where noted:
Change the value p='3134' to your page id where you're using this template. Change the id="something" to something else or remove it. Useful to have an id or a class for js manipulation.
For functions.php
//Get CSS page contents
function myprefix_page_css() {
echo '<link rel="stylesheet" id="something" href="'. site_url() .'/?p=3134" type="text/css" media="screen">';
}
add_action( 'wp_head', 'myprefix_page_css', 99 );
Trying to create multiple, single custom templates, for a custom post type, based on the custom port type's custom taxonomy.
For example: when a post has a certain category, it will use a different template, rather than the default single-custom-post-type-name.php
This is what I have tried so far. But it does not seem to be working. And I assuming it is because I am trying this on a CPT.
add_filter('single_template', create_function(
'$the_template',
'foreach( (array) get_the_category() as $cat ) {
if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
return TEMPLATEPATH . "/single-{$cat->slug}.php"; }
return $the_template;' )
);
exaple loop in the myposttype-single.php (slug of your posttype) file:
while (have_posts()) : the_post();
$category = get_the_category();
$firstCategory = $category[0]->cat_name;
if ( $firstCategory === 'yourcategoryname' ){
get_template_part( 'partials/myposttype', 'specialcat' );
} else {
get_template_part( 'partials/myposttype', 'default' );
}
endwhile;
than create a folder called "partials" in your theme folder.
in this folder you create two files called "myposttype-specialcat.php" and "myposttype-default.php". Add the same code into this file like in a normal template.. header, loop, footer etc..
if the first categoryname of that post will be "yourcategoryname" it will take the special template and not the default one.
I am creating my own little widget sitemap to put in my footer. Here's my code:
function widget($args, $instance)
{
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if ( $title ) { echo $before_title . $title . $after_title; }
// WIDGET CODE GOES HERE
// ?> THIS IS A TEST! <?php
?><ul><?php
wp_list_pages('title_li=<h2>MAP OF THE SITE</h2>&sort_column=menu_order&depth=0&include=57,55,59,61,63,65,192');
?></ul><?php
echo $after_widget;
}
but looks like wp_list_pages doesn't return anything. Not even the title. Indeed if I uncomment that "this is a test" then it's shown.
The strange thing is the same wp_list_page is implemented also in header.php to obtain menus.
First rule when creating Wordpress custom functions is to check if function with the same name already exists.
<?php if (!function_exists("function_name")) {...} ?>
Second rule is to name your function with really unique name, in order to avoid conflicts. In your case this function already exists, that's why you cannot extract the data as you expect...
One more thing, I assume you will put this whole function code in your theme's functions.php file.
Is there a way to pass some $variables to block & node from function like template_page_preproceess on drupal 6?
$vars should already be available to node.tpl.php (if $vars doesn't work for you, use $variables). To add another variable to the $vars, add the following to template.php:
function yourtheme_preprocess_node(&$vars, $hook) {
$vars['yourvariable'] = "your variable content";
}
And then in node.tpl.php you can output the contents of the variable where you want by adding this:
<?php if ($yourvariable): ?>
<?php print $yourvariable ?>
<?php endif; ?>
Don't forget to flush the theme cache if you're not seeing your new item.