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

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/

Related

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

Custom Post Type - Single-{slug}.php to override Single-{$posttype}.php in 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.)

Custom 404 Wordpress with custom fields

I'm running into the following problem. I have a 404.php but I need to use some custom fields on that page. So I preferably need to make a page and use that. But I can't seem to override the default behaviour.
Is there any way to fix this?
Try this one:
Remove all the code from the 404.php
put a single line code of your custom template:
<?php get_template_part( 'new-template-name' ); //don't put extension(.php) ?>
Here you have to create a new template with the same above mention name
Thanks.
Here is how I managed to accomplish it:
Create an empty page template in theme directory named 404-content.php with the following lines of code:
<?php
/* Template Name: 404 Content */
Create ACF field group with the desired custom fields for the 404 page (ex. title & contents). The group's post template should be set to "404 Content".
Create a 404 page in WP admin. Select the 404 Content as its template, and fill the custom fields with the desired content. Note the post-id of the page created.
Update 404.php in your theme directory to display your custom fields. For example (replace xxxx with the post-id of your 404 page):
echo get_field('404-page-title', xxxx);
...
echo get_field('404-page-content', xxxx...)
Hope that helps someone :)

How to change header in Wordpress Blog page and all Post pages only

How to change header in Wordpress Blog page and all Post pages only.
I want a custom header in Blog page and all single posts page.
Thanks
You can use conditional tags within Wordpress:
http://codex.wordpress.org/Conditional_Tags
Basically, you will need to create the content you want only for the blog and post pages and wrap this within the conditional tags:
P.S Don't forget to include the category, and archive pages - as well as the post pages.
is_single(), is_archive(), is_category()
<?php if (is_single()) { ?>
// This is a post page
<?php } else { ?>
//This is not a post page
<?php } ?>
Hope this helps.
To include different header for specific pages change get_header($name) function call inside templates and add $name attribute. Wordpress will load header-{name}.php if file exists, if not, default header header.php will be loaded. Check out template hierarchy for templates where you need to change get_header() function call ( single.php, page.php ).
Wordpress documentation:
get_header
template hierarchy

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