How to list, custom page templates usage? - wordpress

I created a custom page template. And i assign a page its name called like 'Authors', (but it can be change for my customer).
/**
* Template Name: Authors
* Authors page template.
* #package News V2.
* #version v12096.01-stable
*/
The point,
Could we list which page use my custom page template ? If we how ? Thanks.

function getAresAuthorsPage(){
global $post;
$args =array(
'post_type' => 'page',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_wp_page_template',
'meta_value' => 'template-authors.php'
);
$Crumbpage = get_posts( $args );
return 'ALL AUTHORS';
}
we need to use, meta_query for listing that page template usage ;)
Have a nice one.
Note. Sorry about my really bad English.

Related

Woocommerce show individual variations on a page

My woocommerce store sells books for which there are often several variations - ebook, audiobook, paperback...
I have these set up as variable products. But I would like to add pages where people can see just a single variation - so, all the audiobooks or all the ebooks. This would be linked from a menu.
Is this possible? Or should I just give up and do each one as a simple product? I know there are paid plugins out there, but my turnover is super small and I don't want to make a loss on my store.
I've picked up some understanding of code, but programming is not my thing. If you are kind enough to post something, I will need a teensy bit of hand-holding. So saying that I need to target the shop loop (or something) will leave me floundering. But saying, here is some code, just change X for your variation name and add the css or post in functions.php will be set me on the right path.
Thank you in advance.
Create new template in your active theme. In my case i named it template-audiobook-products.php - Read more about templates here - https://developer.wordpress.org/themes/template-files-section/page-template-files/
Inside the template add the follwoing code
/*
Template Name: Audio books
*/
get_header('shop');
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'variable',
),
array(
'taxonomy' => 'pa_color', //Change to proper attribute
'field' => 'slug',
'terms' => 'blue', //Change to proper value
),
),
'fields' => 'ids'
);
$get_all_variable_products = get_posts($args);
$product_ids_list = array();
foreach($get_all_variable_products as $parent_variation_id):
$_product = wc_get_product($parent_variation_id);
$variable_product_ids = $_product->get_children();
foreach($variable_product_ids as $variable_product_id):
$_variable_product = wc_get_product($variable_product_id);
$_variable_product_attributes = $_variable_product->get_attributes();
if($_variable_product_attributes['pa_color'] == 'blue'):
$product_ids_list[] = array( 'parent_id'=> $parent_variation_id, 'variation_id' => $variable_product_id);
endif;
endforeach;
endforeach;
woocommerce_product_loop_start();
foreach($product_ids_list as $_product_id):
//Since $product is obj we can pass it and use our default content-product template.
$product = wc_get_product($_product_id['variation_id']);
//Keep in mind this template by default doesnt support ajax (use css to hide button or use woo hook/filters)
wc_get_template_part( 'content', 'product');
//In case we want custom design or ajax button uncomment following.
// echo 'Add to cart';
endforeach;
woocommerce_product_loop_end();
get_footer('shop');
Create new page and select this template
To work properly the standart woocommerce template we need to include the classes and other html tags that we may need in our template. Add this function in your active theme functions.php file.
function my_custom_body_class($classes) {
if(is_page_template('template-audiobook-products.php')) {
$classes[] = 'woocommerce';
}
return $classes;
}
add_filter('body_class','my_custom_body_class');

Single custom post type template (WordPress using Timber/Twig) not working

I've been unable to get the single twig templates for a custom post type to work.
My developments.twig page correctly lists the posts in custom post type developments.
However, the single template defaults to single.twig, rather than single-development.twig.
In single-development.php I have:
$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;
Timber::render('single-development.twig', $context);
Like some others suggested I resaved the permalinks but that didn't help.
In timber-functions.php I have:
public function register_post_types() {
// Use categories and tags with attachments
register_taxonomy_for_object_type( 'category', 'attachment' );
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
register_post_type('developments', array(
'labels' =>
array(
'name' => __( 'Developments' ),
'singular_name' => __( 'development' )
),
'public' => true,
'supports' => array( 'title', 'author', 'excerpt', 'custom-fields', 'revisions', 'page-attributes' ),
'show_in_menu' => true,
'taxonomies' => array( 'category' ),
));
}
How is this structure supposed to work.. have I missed something?
Your post_type called developmentS (plural), so your single file should be named single-developments.php
Register a new custom post type has to be inside functions.php
Besides the way you wrote the script does not seem the right way to register a post type. There are no function named as register_post_types(), it's register_post_type();
Here is the developer handbook where you can get the details of custom post type.
https://developer.wordpress.org/reference/functions/register_post_type/
You can find an example at the bottom of the page. You can create a single for custom post type as single-developments.php where you simply can use wp_query to get the data for single post.
Reference: https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/
I misunderstood the naming conventions around plural and singular.
The template in fact needed to be single-developments.twig, and was not using the 'singular_name' for single template as I had thought.
I also did not need the extra single-development/s.php.
This code in single.php works as expected:
else {
Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', 'single.twig' ), $context );
}

WordPress: Taxonomy archives based on Custom Post Type

I've generated three different custom post types (e.g. books, movies, games).
And I've a custom taxonomy for all of them (e.g. genre).
What I need are archives for the taxanomy based on the post types.
For example: "books-genre", "movies-genre"...
Is there any solution to do that? Now I've only the taxonomy archive for "genre".
The way I like to approach custom post archives is to create a custom archive template with WP_Query sections where I need them. You'd create the blank file in the root of your theme at archive-cptnamehere.php.
You might have some template partials to add in but the core of the page looks like this:
<?php
// 1- Get ID of the page's path by URL (reliable)
$pagePath = $_SERVER['REQUEST_URI'];
$pageObject = get_page_by_path($pagePath);
$pagePathID = $pageObject->ID;
// 2- Print page title
$teamPageTitle = get_the_title($pagePathID);
echo $teamPageTitle;
// 3 - Do a query that gets the data you need
// Args: -1 shows all locations, orders locations alphabetically by title, orders locations a-z, only query against team items
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'team',
'meta_query' => array(
array(
'key' => 'team_page_categorization',
'value' => $team_page_categorization_options_array_item,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
// 4- Setup query while loop
if($the_query->have_posts()) {
while($the_query->have_posts()) {
$the_query->the_post();
// 5- Do what you need to inside the loop
// 6- Close it all up, don't forget to reset_postdata so you can do additional queries if necessary!
}
wp_reset_postdata();
}
?>

Move woo commerce reviews from tabs

I have been trying to move woocommerce reviews. I was able to remove the reviews and replace them outside of the tab, but I am having trouble getting the form for the review to show up.
This is the code I have so far:
function woocommerce_review_list() {
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
}
add_action('woocommerce_after_single_product_summary', 'woocommerce_review_list', 10);
You could try loading WooCommerce's entire comments template:
wc_get_template('single-product-reviews.php');
Or failing that you could simply copy out the relevant parts from that template.

how to add new pages using WP core function

how can I add a page using core functions of wordpress.I can find function for Post etc as well but couldn't find any for Pages.Any one who can help me
Thanks
You use the same function to add posts or pages, just add the post_type parameter like this :
$args = array( 'post_type' => 'page',
'post_content' => 'You content...',
'post_parent' => $parent_id; // ID of the page this one should be a child of
... // etc.
...
'post_title' => 'Title for your page');
wp_insert_post ($args);

Resources