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

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).

Related

how to customize post meta in wordpress?

currently my theme on WordPress is Neve , and my posts all over the blog shows the following meta info :
https://i.stack.imgur.com/ynu71.jpg
where :
1- post title
2- post author
3- post date
4- post category
i want to replace these post meta with similar to this:
https://i.stack.imgur.com/Xywa9.jpg
for this purpose i have created a child theme and then installed snippet plugin to add php code easily and deactivate it once it is not working . unfortunately i could not find the code that can do the required modifications on that post meta :
https://i.stack.imgur.com/uwCrS.jpg
can any one provide a full php code to modify all these changes in one time after pasting into snippet ? or if there is another way i can do it ?
You'll have to create a child theme (already done) where you can override the current blog post template, instead of using a snippet plugin. To do this, copy the blog post template file from your theme and add it to your child theme.
WordPress will now read your child theme template instead of your theme's template, and you can easily modify the DOM from there, and shape the layout/text however way you want. (You can use the theme editor built-in in WordPress to modify the new child theme file. No plugin required.)
This is the proper way to modify a post page without plugins, and you can easily grab thing such as a post date, author, etc. via WordPress' built-in function. Example of how to get the author name of a WordPress post in PHP.
As for, 'latest edition' date, I will lend you a snippet I wrote for a client as WordPress. This will return the date at which a post has been modified as long as it is different from the publishing date (tweaks are common right after publication so it's a tad pointless to show a "last edited date" as the same as the publication date).
function current_post_last_edited_date_formatted() {
if(get_the_modified_date() !== get_the_date()) {
return '<p class="last-edited"> Last edited <span class="data">'.current_post_last_edited_date().'</span></p>';
} else {
return '';
};
}
The function you see called in the condition are WordPress core functions. =)

Custom template file for displaying the inner blog contents

I am trying to create a custom layout for my blog listing and blog content display pages.
The following url will display all the latest blogs based on the conditions that I have set.
http://fqdn/general-blog
When I click on the 'Read more' button corresponding to a button, it will display the full blog contents in another page.
Sample format : http://fqdn/general-blog/tourist-details
I am able to modify the layout of the page corresponding to blog listing. For this, I have created a custom tpl file : page--general-blog.tpl.php.
What I want is to create a new template file for displaying the contents of individual blogs.
The individual blog content display page is not entering - page--general-blog.tpl.php file. It's always using the template - page.tpl.php.
I tried creating separate tpl files like :
page--general-blog--%.tpl.php
But, the changes in this template is not getting effect in the individual blog content display page.
How should I move forward. Has someone had any similar situations?
If your content type has name general-blog,you should name single blog entry template as node--general-blog.tpl.php. See
Theming nodes by content type for details.
If you need to theme whole page template depending on node type, you should add following code in your template.php:
function YOURTHEME_preprocess_page(&$variables) {
if (!empty($variables['node']) && !empty($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
}
}
And then use page--node--general-blog.tpl.php file (see Page templates depending on node type for details).
Don't forget to clear the theme registry after creating new template file.

In WordPress how to add post to custom pages:

I am new to WordPress.
suppose i have made pages as follows
home, news, about us
now if i want to create post for news page only
news1,
news2
then posting new post how could i set this settings?
You can create page templates.
Create one for all your pages.
Then, change the template of the page of news to the news template.
In that PHP file now you can do whatever.
For example, for news.php
<?php
/*
Template Name: My Custom Page
*/
//Here comes your PHP code, where create a $WP_Query(); and loop troough on that.

Using Jetpack Portfolio Project in WordPress child theme does not call archive custom template

I have a child theme that uses the new Jetpack Portfolio Project custom post type and wish to modify archive.php to display custom results.
I'm using: WordPress v3.9.2; Theme: Child of Point, Jetpack is installed with Custom Content Types enabled, and Portfolio Projects selected in the Settings. (No other plugins that implement portfolio functionality are installed.)
According to the Codex:
Template Files
In the same way single posts and their archives can be displayed using
the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
and if you don't have this post type archive page you can pass BLOG_URL?post_type={post_type}
where {post_type} is the $post_type argument of the
register_post_type() function.
My understanding is that if you create files called single-jetpack-portfolio.php and archive-jetpack-portfolio.php within the child theme, WordPress will automatically use those files in place of single.php and archive.php respectively.
However, my child theme successfully calls single-jetpack-portfolio.php, but completely ignores archive-jetpack-portfolio.php, instead calling archive.php in the child.
I am stuck for a solution.
From the codex above, adding to the URL "?post_type=jetpack-portfolio" does cause the child theme to correctly use archive-jetpack-portfolio.php, but should I need to be manually modifying every single URL to explicitly specify this? Should WordPress not automatically be detecting this, as it does for the single-jetpack-portfolio.php file? How can I solve this?
I have tried:
Resetting the permalinks in case it was related to that (changing the option in Settings and saving and back again)
Adding an archive.php file to the child in addition to archive-jetpack-portfolio.php (I initially didn't have an archive.php in the child, so it used the parent's archive.php)
Publishing a new Jetpack portfolio project and updating an existing page (I read somewhere that publishing something might trigger Wordpress to see the changes)
Thanks in advance for any help.
I had the same problem described by the OP. When I visited mydomain.com/portfolio it would use the custom archive template. When I tried to view a project type it defaulted to the regular archive.php. I'm wondering if OP was viewing a project type page without realizing it.
My solution was to create a taxonomy template file. After playing around with it I figured out that
taxonomy.php
taxonomy-jetpack-portfolio-type.php
taxonomy-jetpack-portfolio-type-{name-of-project-type}.php
all worked correctly, depending on how specific you wanted to get.
There's more info at the wordpress codex: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display
Hope this helps someone.
I will be working on this the next days.
You should try this in the child archive.php first lines:
<?php
if( is_post_type_archive('jetpack-portfolio') )
get_template_part('content', 'jetpack-portfolio');
elseif( is_tax('jetpack-portfolio-type') || is_tax('jetpack-portfolio-tag') )
get_template_part('archive', 'jetpack-portfolio');
else continue;
?>

How do I create a custom WordPress page?

I want my WordPress blog to have a page called music. On that page I will query the DB for posts with the category music and then change around the look and feel of the posts. So I can't just put a link to /categories/music/ because I want to do custom work on the posts.
Should I put this code in a separate php file and link to it? I think I may lose access to all the nice WordPress API calls if I do that.
I was thinking about using a filter, but I am not sure which one to use. I was thinking something like the following except the_title has not been grabbed yet so I cannot check the title.
function show_music(){
if( is_page() && the_title('','',false) == 'music' ){
echo "got here";
}
}
add_filter('pre_get_posts', 'show_portfolio');
How would you go about this?
You need to put the below code in the file, and then put the file in the Theme folder. Then you can create a page using Wordpress pages and select a page template with the name you put in this comment:
/*
Template Name: Something Goes Here
*/
You need to create custom page within your theme. If you dont have idea how to create custme page or template page in WordPress theme then view my easy tutorial How to create template page in WordPress

Resources