Wordpress template for specific nested page - wordpress

In Wordpress I have a page with the slug logged-user with a parent page ecadmin, making it accessible under the URL /ecadmin/logged-user/.
Since it has a parent page, I (or any other user) can create a page with the same slug logged-user as long as it isn't nested under the same parent page.
The problem now is that I cannot create a single page template page-logged-user.php in theme's folder, as this template can be potentially applied to any other page named logged user no matter where it belongs hierarchly.
Is there a way to name the template file in such way that is referencing to its parent page(s) as well? E.g. page-ecadmin-logged-user.php

The feature you are hoping for doesn't exist, but there is a quite simple way to accomplish what you want to do:
You can create a custom page template [whatever-you-want].php and place this comment in the header
/*
Template Name: Logged In User
*/
That template will be available in the Page Attributes meta box for any "Page" in Wordpress, but will only be applied to a given page if selected in that meta box.
Note:
All content creators have access to the page attributes meta box by default.
If you are worried about users applying that template inappropriately you could hide the Page Attributes meta box from everyone who isn't an admin:
function remove_post_custom_fields() {
if (!current_user_can( 'create_users' )){
remove_meta_box( 'pageparentdiv' , 'post' , 'normal' );
}
}
add_action( 'admin_menu' , 'remove_post_custom_fields' );
http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/

You could try using using page-$id.php which would be unique to that page.

Related

Woocommerce product archive displays nothing when called from custom Page

I have a woocommerce shop page which shows me all of the products perfectly. This page is also set as the "Shop" page in the settings. Now I want a second page called Home, which should use the same template as the shop page. (basically a second shop page without categories and some news)
home-template.php
<?php /* Template Name: Homepage */
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
?>
I'm using the standard archive-product template from woocommerce's github.
I can see the template being loaded, like the header, but certain woocommerce functions don't return the expected value. woocommerce_before_shop_loop doesn't return anything, even though it should display the orderby dropdown. woocommerce_product_loop() returns true, but wc_get_loop_prop('total') returns zero.
I've also tried renaming the file to front-page.php but that didn't help. The home page is set correctly in the reading settings.
Am i missing a call to a query, or is it something else? Thank you in advance for your help!
When WooCommerce creates the builtin shop page it saves the id of the created page in the wp_options database table with option_name 'woocommerce_shop_page_id'. When WordPress loads a page WooCommerce checks if the page id of the page being loaded is equal to the option_value of option_name 'woocommerce_shop_page_id' and then executes code to generate the HTML of the shop page. If you have looked at the shop page in the page editor you will notice that the content is empty. The magic is being done with hard coded routines that execute for this special id.
Since, your custom page has a different page id none of the custom code for generating the shop page will be execute. So, you need to execute this magic code for your custom page. It can be done but you need a good understanding of WooCommerce.
I suggest you reconsider your design and instead of a new page just customize the existing shop page using actions and filters.

Plugin Development: Dynamically change post title (the_title()) from a plugin, without changing database title

I am developing a custom plugin that needs to change the post title dynamically (NOT the "title" tag for the head) without using a page template.
I want the end user of the plugin to be able to use their own theme templates, which will already output the title, usually in an h1 tag, but I don't know where and how for sure, because it is not my theme.
I will need to just change the output on the frontend, not in the database. I want to change it based on whether a url's query string matches certain values, or whether the frontend user is logged in or not. I know how to do this from a custom template, but I want to do this from a template the user already has selected.
I noticed that if I filter the_title(), it changes it everywhere in the admin dashboard and in the frontend navigation menus, which is what I don't want, I just want to change where it is outputted in the user's page template (usually in an h1 tag).... does anyone have any suggestions on how to go about this?
You can use the_title filter, which would have influence only on front-end, this doesn't change record in Database. Your code would be something like this:
add_filter( 'the_title', 'add_text_to_post_title' );
function add_text_to_page_title( $title ) {
if( is_post() )
$title = 'Your text: '. $title;
return $title;
}

Custom post type: creating a page template with the same slug

I'm currently working on a custom post type and want to be able to edit the archive page from Wordpress with a page template. So I created the CPT called 'cars' and created a page template with template name: 'Cars overview'. Next i create a page inside WordPress and choose the template page 'Cars overview' and gave it the URL: mywebsite.com/cars/
Now the problem is that the slug 'mywebsite.com/cars/' is already in use by the custom post type itself causing the page to load the custom post type loop instead of the page template loop. So I can't edit the title, content etc inside WordPress. I could change the url of the page, but i want to be able to control the overview page in WordPress.
Long story short: How can I create a page template that is using the same URL as the custom post type archive page?
Thanks in advance!
One simple solution, simply disable the archive where you create your custom post type:
register_post_type("cars", array("has_archive" => false));
Another approach rather then disabling the archiving and adding another page to show the cars. Changing the archive template used by your theme might be a better option.
First step is to find the template currently in use by your theme, copy it to your plugin file and you can change the template file to whatever you like. You can find more information about it here.
The only thing you need to do is point WordPress to the right direction:
add_filter("archive_template", "archive_template");
function archive_template($archive_template) {
global $post;
if ($post->post_type == "cars")
{
$archive_template = "path/to/your/template.php";
}
return $archive_template;
}
Disabling the archive and creating one manually seems a bit strange to me. And I always replace the archive page and sometimes single page from our theme (usually the7).

Multiple thumbnails in WordPress based on page template?

Is there a way to add multiple thumbnails in a page based on page template or page ID
instead of post type (post, page)?
You can add a conditional statement to test for a specific page id:
if(is_page('<your page id>')){
// Do your thing
}
Similarly, you can test for a specific page template like so:
if(is_page_template('<page template file name>']){
// Go on wit' you bad self
}
Make sure that when you use is_page_template() that you feed it the file name of the template, e.g., is_page_template('two-columns.php'), as opposed to the name of the template, e.g., is_page_template('Two Columns').

Can WordPress post content be external data (not from the database)

I am trying to display a page based on some data returned from an external API (Amazon). This data is formatted then, has to be displayed on a page, created on the fly, based on URL querys. I can already do this with shortcodes but this has to be from the query.
I see all kinds of info in the codex on returning custom query_posts into the loop from the database. However, I cannot find info getting external data to appear on a page.
Is this possible in WordPress? (anything is possible, right?) Just point me to some functions, filters or tutorials please.
If I understand you correctly, you want to retrieve data dynamically and display it in a WordPress page?
There are many ways to do this, but here's one option:
Create a Page Template
Create a WordPress page and use the Page Template created in step 1.
Edit the Page Template to call the external API and display the data
I'm guessing you've been trying to find a way to do this from the "content" of a page or post, but the easiest way is to put the code in a custom Page Template.
UPDATE: If you want to programmatically create a page, this might work for you: http://wordpress.org/support/topic/how-to-create-pages-programmatically?replies=5#post-1230619
http://www.prelovac.com/vladimir/wordpress-shortcode-snippet-to-display-external-files
This is a snippet to display external data in a post.
Would this help? If the dynamic page was a HTML page and THEM displayed in WP.
Yep, Thats possible.
I Did by using bridges.
You can do it by add "add_meta_boxes" action.
Inside the meta box function you can add call and get the external page contents, or can give your own forms .etc
My Code :
/*
* Add Meta Product Type Field to POST
*/
add_action('add_meta_boxes', 'meta_box_product_type_add');
My Meta Box
/*
* Product Type Meta Box Init
*/
function meta_box_product_type_add()
{
add_meta_box('ptype_testing', 'Product Type', 'add_ptype', 'testing', 'normal', 'high');
}
/*
* Product Type Field
*/
function add_ptype()
{ ?>
<label>Type of Product : </label>
<select name="ptype" id="ptypes">
<option>----Select----</option>
<option>Physical</option>
<option>Virtual</option>
</select>
<label>Unit :</label>
<select name="punit" id="units">
<option>----Select----</option>
<option>KG</option>
<option>Mtr</option>
<option>Ltr</option>
</select>
<?php
}
Try It....

Resources