Custom Post Type - Single-{slug}.php to override Single-{$posttype}.php in WordPress - wordpress

I am looking to add a custom template for a single post inside of a Custom Post Type. The Custom Post Type is working as it should and all of the posts are correctly using single-{$posttype}.php. However, for the page with the slug "our-wedding", I am trying to override single-{$posttype}.php and have it use single-our-wedding.php. However, the page is still using single-{$posttype}.php.
Any ideas?

You can either use a solution that will let you assign custom templates to one particular post of that post type (there are plugins).
Or you can edit the single-{$posttype}.php to include a
if( is_single('our-wedding') ){
get_template_part( 'my-template');
} else {
// The usual code for this single-posttype
}
And then create a file called "my-template.php" inside your theme folder.
(Edited based on user feedback.)

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

WordPress, how to use dynamic urls for the same page?

I don't know if it's possible, but I want to configure a WordPress site to render the same page for all URLs that match the format https://example.com/things/<any_word>, the URL must keep its value.
For example,
The following URLs must show the same content:
https://example.com/things/pencil
https://example.com/things/book
https://example.com/things/tv
Your help would be greatly appreciated
There are several options:
1) Register your custom post type and create defalut template (the best option)
2) Add some custom code to page template (single.php or page.php ...) where your call a specific template for a specific category:
<?php
if (get_the_category(get_the_id())=='your-category')
{
get_template_part( 'TEMPLATE_FOLDER/content', 'something' ); // "content-something.php"
} ?>
3) Create a template and choose it for needed pages
https://developer.wordpress.org/themes/template-files-section/page-template-files/
https://developer.wordpress.org/reference/functions/register_post_type/

How to use a file url from Advanced Custom Fields to overwrite post title link?

I am developing a custom post type for the client to be able to upload a pdf using the advanced custom field file type. The current theme is Avada and I am wondering how I can change the title link to be the file URL?
I have thought about creating a custom page template and disregarding the archive altogether. I am open to suggestions/ideas.
The ideal outcome is using the native archive layout but having the title link to the file URL (pdf) instead of to the post.
You may use the filter to modify the title. The below code is untested just giving you the idea. Also, Make sure to check if the custom field exist.
add_filter('the_title', 'my_custom_title_with_link');
function my_custom_title_with_link($title){
global $post;
//please do a check for the post type otherwise all the post title will be changed
$title = ".$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).

WordPress - Custom Post Type Entry Won't Load Into A Page

I created a very basic custom post type in my WP site to hold events. Basically, a user can input an event title, some information about the event and the date.
Currently, the only time the events are being displayed are in my sidebar where I just pull the title and date of the upcoming events.
However, I want to be able to allow the user to also click on the title of the link to lead to the event's individual page. I tried previewing my custom post type event and it kept leading me to my 404.php page. The events don't seem to load into a page at all.
I'm hoping this is a small property that I didn't set when I registered my post type. Any help?
Wordpress has incorporated custom post types into the template hierarchy and you have access to them at single-$posttype.php and archive-$posttype.php.
If you create these templates and your posts don't show up in the loop, you need to reset your permalinks/flush your rewrite rules.
As well, it would help to see how you are registering your post types, as in order for your archive-$posttype.php to be available you need to call has_archive => true in your arguments.
What i've done when i needed it:
Add this code in the same function that create your post type:
add_action("template_redirect", array(&$this, 'template_redirect'));
function template_redirect() {
global $wp;
if ($wp->query_vars["post_type"] == "events")
{
include(TEMPLATEPATH . "/events.php");
die();
}
}
Then, just create events.php with the_loop (like single.php)!
Hope it helps
[]'s

Resources