Timber/Twig and Wordpress Admin Integration - php

I am a beginner programmer and I got little problem.
The problem is that I can't link Custom page (page-product-all.twig) to Wordpress admin (can't find page-product-all on Wordpress admin.)
I have created custom twig page (page-product-all.twig)
I have created custom php file (page-product-all.php)
I have created a page in Wordpress admin panel (Page Product All)
But when I change the page on Admin (Page Product All), the page (page-product-all.twig) does not change.
Thanks in advance!

Take a look at page.php from the Timber Starter Theme:
<?php
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context );
The Timber::render method on the last line loads page.twig as the default page template. However, Timber also checks for any twig files with the page- prefix followed by the name of a post (or in this case a page) with the code:
'page-' . $post->post_name . '.twig'
I really like this technique for handling custom pages because it prevents us from having to create custom page php files. This is especially helpful on sites with many pages.
If you want the page title to be "Page Product All" then your twig file will have to be page-page-product-all.twig. I sort of have a feeling you just want the page to be called "Product All" so in that case the twig file would remain page-product-all.twig and you will have to create a page called "Product All"
Now add a simple <h1>hello world</h1> to page-product-all.twig (I prefer <h1>hi mom!</h1>), preview the Product All page and voila. Custom page templates with Timber.

Related

WooCommerce: Single-product-postslug template not working?

First,I know wordpress won't allow you to use template for posts (WooCommerce's product page is built via post). So I look for the Template Hierarchy.
It says there:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post.
For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.
So I created a template and name it single-product-ccc (ccc is one of my product's slug name), but nothing happened, nothing was affected.
But by creating a template named single-product will affect all of the product pages.
Why is that happening?
I don't get it. Even a single-2313.php (2313 is one post's id) will overwrite the default single.php for that 2313 post.
Why single-product-slug is not working in the same way?
Thanks.
Let me first correct you at one point. You say: "WooCommerce's product page is built via post".
This is not correct. WooCommerce creates a new post type 'product'. You can see all custom posts types that WooCommerce creates here: https://docs.woocommerce.com/document/installed-taxonomies-post-types/
The 'product' post type uses the template single-product.php.
As you say, WordPress Template Hierarchy Docs say that you can create a template for a particular product:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
WooCommerce is a WordPress plugin and as so, it doesn't always follow WordPress rules strictly, and this is one of those cases. But you can use a filter to correct this very easily. In your theme's functions.php simply add:
add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
function custom_single_product_template_include( $template ) {
if ( is_singular('product') && (get_the_ID()==30)) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-30.php';
}
return $template;
}
where get_the_ID() is the id of your Product. Now, you can create a new template as for example single-product-30.php
Try adding this in functions.php
function your_theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'your_theme_add_woocommerce_support' );
and now onwards, you can use woocommerce/single-product.php for customisation

Woocommerce shop page custom template

As I understand by default Woocommerce shop page uses product archive template. What I am looking for, is to use a custom template for shop page.
Here is what I did:
Create template "my-shop"
Create page "My shop" -> choose template "my-shop"
Choose "My shop" as Woocommerce shop page
But none of the changes I made to "my-shop" template are present on the shop page.
What am I missing here? I would not like to change product archive itself, just the shop page.
Is there a way to disable product archive from being a default for shop page?
Thanks
I know it's too late and you may have figured it out by now. In any case, the changes that you would like to make to the WooCommerce Shop Page need to be done in the archive-product.php and it would be safer to create a child theme and do these changes. Making enhancements and customizations in a Child theme is best practice so that you can update the parent theme at any time and it won't affect your store.
I hope this helps, for more information on how you can use WooCommerce short-codes to customize your store can be found here.
To add to Silver Ringvee's answer - he used is_page but that only works on wordpress pages. For woocommerce you need to use something like is_woocommerce() . See Woocommerce conditional tags page.
My example code uses the is_shop conditional tag as that was the page you wanted to change. the code get_template_part( 'content', 'shop' ); will call the file content-shop.php in your theme root folder. This code is to be added at the top of wp-content\themes\*theme*\woocommerce\archive-product.php that you can copy from wp-content\plugins\woocommerce\templates\archive-product.php
You can add it just before get_header( 'shop' ); line 23 in my file - and the entire page will be drawn from your template. If you want to keep the header of the shop page, then put this code after the get_header code. Remember to include a footer in your file as well
if (is_shop()) {
get_template_part( 'content', 'shop' );
} else {
#normal archive-product code here
}
The solution (not perfect) that I figured to work best, until someone finds a way to actually change the template from dashboard:
Adding:
<?php
if (is_page( 'Page Title' ) ):
# Do your stuff
endif;
?>
to content-product.php in my theme's woocommerce folder.
If you prefer to go with code, you can create a redirect from the original shop page to your page via wp_redirect.
add_action('template_redirect', 'bc_010101_redirect_woo_pages');
function bc_010101_redirect_woo_pages()
{
if (is_shop())
{
wp_redirect('your_shop_url_here');
exit;
}
}
More detailed tutorial can be found here
This is not possible to create custom template for shop page , just copy and paste woocommerce Templates into you theme folder and Try to work in content-product.php template .

Wordpress Timber (TWIGG) - How do I apply a template to a bunch of pages if parent is the same

Basically I want to apply a template to the child pages of a specified page in Timber, Wordpress.
I have been currently using page-(name of the page).twig to apply templates to specific pages but this only changes one page at a time.
Dont know if this answers your question but you can make a php file that calls a twig file that has the layout you would like to use for the pages. You then add "Template Name: Your Template Name" in doc blocks in that php file. You can then choose to use that template in the wordpress admin panel when creating a new page.
I've figured it out
If I create a page called contact, I will have to create a twig called page-contact.twig otherwise it will default to base.twig
$templates = array('base.twig');
if ($post){
array_unshift($templates, 'page-' . $post->post_name . '.twig');
}
If homepage, switch to front-page.twig
if (is_home()){
array_unshift($templates, 'front-page.twig');
}
If 404, switch to 404.twig
if (is_404()){
array_unshift($templates, '404.twig');
}
If I create a page called contact and a subpage called location then the following code will get the name of the parent page and put child-page- before it so it doesn't clash with the other template. Any page below contact will use the template child-page-contact.twig
if ($post->post_parent !== 0) {
array_unshift($templates, 'child-page-' . $post_data->post_name . '.twig');
}

wordpress link home page to different page

I have created html pages and trying to convert into wordpress theme,how to link html one page to other page in wordpress menu bar
sample code :
Features
this code is not working,it 's showing page not found.how to make this link in wordpress using php code.
Firstly Create Page "Features" from wp-admin.
Create template for this page.
http://codex.wordpress.org/Stepping_into_Templates
To set this page in menu Go in "Menu" section in WordPress.
http://codex.wordpress.org/Appearance_Menus_Screen
To view this menu in fronted use wp_nav_menu()
Take example template from your theme.
This is file in theme ending with -page.
Change template name in head of a file.
Then remember about the_loop all should be inside a loop to work correctly with many pages.
Put html there, also in header.php attach css to this html.
Page structure is like, header in the up, then page and then footer.
Remember to preserve good html structure - divs beginning and ending.
Then you create a page with content ( which is presented in the_loop ) , which has its own url address.
You can set url naming of pages in settings -> permalinks, you may need to write to .htaccess file.
Then you have direct url to page. You can use it in code like this:
echo bloginfo('url'). 'nameofpage';
All to do is create a template and assign it to page ( on page edit page template option ).
You can use pages or posts for this, i prefer pages.
Create new pages or posts and get their ID.
For linking its:
Get link with this:
get_permalink( $yourPostOrPageID ); // only get; not echo
Otherwise
Wordpress homepage link:
get_bloginfo('home');
Category or custom taxonomy term link:
get_term_link( $term, $taxonomy );

WordPress : How to add custom template in genesis framework

I am creating a custom members area for my client's employees. Basically, what I've done so far is I created a new role=consultants and I gave that role a read only access.
Then I uploaded the Peter's Login Redirect Plugin so that the consultants (employees) land in a page called CONSULTANTS PORTAL. From there, they will be able to access their individual page which it will load as long as the name of the page matches the username given to them. That way they can only see their own page.
To see this process, you can visit this link in the wordpress.org forums EASY CLIENT PORTAL
So I've managed a lot of it, except...I am supposed to duplicate the page.php and then add the script that will make the individual page show up. But, the Genesis Framework is pretty complicated. The page.php has an empty skeleton and the actual meat of the page is in a li/structure root folder (That's what I think anyway) .
As it is right now, I have the following in my default template page consultants-portal.php
<?php
/**
* Template Name: Consultants Portal
*/
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
genesis();
?>
This code gets me this. You can see the content (my page) loading before the page loads. Which tells me there is something else I need to add to this so that the content loads in the actual white area of the page.
The instructions in the link I mentioned says to add the dynamic script right above the is_page or have_posts, but I as I said, Genesis doesn't have this in page.php. instead it is all broken in pieces and spread through the root.
Sorry if I made this too long to read, I wanted you to have all the info I have.
has anyone done this before?
Try out the following code:
<?php
/**
* Template Name: Consultants Portal
*/
// remove Genesis default loop
remove_action( ‘genesis_loop’, ‘genesis_do_loop’ );
// add a custom loop
add_action( ‘genesis_loop’, ‘my_custom_loop’ );
function my_custom_loop () {
// add your queries or custom structure here
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
}
genesis(); ?>
Instead of writing the code directly, write it inside the loop function as above.

Categories