If URL x, load template, else single.php (on Wordpress) - php

I have custom posts that I want to be shown in a different template.
Right now I have custom posts on:
www.mywebsite.com/travel/post-name
All other (default) posts are on:
www.mywebsite.com/post-name
Basically, I'm trying to find a function.php code, that identifies URL and applies another template.
So, if path www.mywebsite.com/travel/post, then load post in traveltemplate.php
else, single.php
Any help would be appreciated.
P.S. this is not categories, so in_category category ID and category won't work.
I'm looking for a rough url identifier.

WordPress has a pretty sophisticated system to pick the “correct” template for specific pieces of content already, based on certain file naming patterns.
Check https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/, it explains how that looks for custom post types.
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php

Related

How to place featured image below post title

I found this solution to getting the featured image to default below the post title.
wordpress add featured image below post title
Where do I place this php?
Will it work for all posts?
Please have a look at the wordpress template hierarchy in themes:
https://developer.wordpress.org/themes/basics/template-hierarchy/
You can edit the look of single post views inside the single.php, if using a custom post type single-{post-type}.php. If you are using a theme that is not made by yourself and might get updated, make sure to use a child theme, so your changes will not be overwritten when the parent theme is getting updated.
This is a nice tutorial for creating child themes in wordpress:
https://www.smashingmagazine.com/2016/01/create-customize-wordpress-child-theme/
In the template file you get the featured image with get_the_post_thumbnail() function and the post title with get_the_title() function.
Search for the lines in the code, where you can find the functions and change the location to your desired place. Or if there is no featured image, you can add the code you already found.
Where do I place this in php?
Inside of the single page template of your theme.
Will it work for all posts?
If you use the single.php file, yes. All posts use this template. But you can only apply the changes to specific post types as described above - if you like to. You can also make it work for some specific category terms with category-{term}.php or maybe you want to change the post listing in your archive.php. Please check the link above, Wordpress Template Hierarchy will make this clear, very easy to understand.
Hope this helps!

Use a single wordpress category.php file for every category registered

As the title suggests, I'm trying to use one category.php file for every category registered by a user in the back-end. I've always created category-[specific category].php files in the past but this feels very messy as the design usually remains the same.
Without being specific in the query I return all posts in all categories however, I want to be able to show the custom posts for only the category selected from the nav. The problem is, I can't seem to find a way to pass the category the user selected into the query in order to only return posts attributed to it.
Thanks in advance for any help.
You don't need to do anything specific to show a list of posts from a specific category. Take a look at this article on the WordPress Codex. Here is an excerpt from that page:
In the case of categories, the hierarchy is fairly simple. For
instance, suppose the slug of the Category in question is news and the
Category ID is 6. The Template Hierarchy specifies that WordPress will
use the first Template file it finds in your current Theme's directory
from the following list:
category-slug.php
category-ID.php
category.php
archive.php
index.php
So all you need to do is create category.php in your theme and then view any category and it'll use that template. You can then use is_category() to check which category is currently being used if you want to do custom things depending on the category.

Allow page text to be edited from inside the wordpress menu without hardcoding it

New to WordPress theme design, and my google searching is not returning what I am after.
I have a theme I have created and its fine, but the text is all hardcoded into the theme.
I want there to be options in the theme appearance settings? (right place?) where a user can e.g. enter their 'about us' text.
But I have no idea what I should be searching or how to pull in that information into my theme.
Im no a beginner to PHP, but just the way WordPress works itself.
A link, search term or quick start is all I need.
I think you might find the (Free) Advanced Custom Fields plugin useful: http://www.advancedcustomfields.com/
You can add unlimited custom fields and create user interfaces for non-technical people to add content to your theme without them touching any code.
If you set up a field named 'about_text', calling it in your theme would look like this:
<?php the_field('about_text'); ?>
First, you should learn how wordpress is working to display the content. In back office, under Settings > Reading you'll find an option that let you tell how you want Wordpress to deal with the front page : is it a listing of the posts, or a static page?
If you choose the first options, it will use the index.php template from your theme folder. If it's a static page, you'll have to select which page to use for displaying the home (a page you created under the Menu page). The template that will be use then is front-page.php.
For every post / page (page is a post_type, just a variation of post) there will be a title field, a wysiwyg content and a featured image which will be displayed in a template. That's all you can manage by default. To display the title you can use the_title, the content the_content and for the featured image you will need the_post_thumbnail - note that those functions will need to be used inside the loop.
In order to display some more fun, you have many tools at your disposal:
Widgets : widgets are displayed in a sidebar - don't take it literally, it's just a zone of your template. You can register a sidebar with register_sidebar (use this inside your functions.php file, within an init hook). Then in your template you can display the sidebar with dynamic_sidebar.
In WP back office, go then under Appearence > Widgets. You will find your brand new sidebar where you can put any kind of widget you want. If you need a wysiwyg widget, I recommend you to install the Black Studio TinyMCE widget.
Custom fields : any post_type (a post type is an entry in wp_posts) in Wordpress have some associated metas store in wp_postmeta. A meta is defined by a key and a value - it's like any post have an associated array that you can customize.
There is two ways to work with custom fields. First, you can use the default Wordpress feature: when editing a page, click on the screen options button on the top right and enable "custom fields". You will now have a new area to work with on the bottom of the page : you can add fields by name (by default it will list the existing fields names but you can add your own) and value. It's good enough to simple text fields. Then in your template, you can get that value with the get_post_meta function.
The other way is to use a custom fields plugin, which allow you to have wysiwyg fields, loops, media uploader, datepicker... Two popular plugins are Advanced Custom Fields and Custom Field Suite. They both allow you to easily create set of fields for any page / post_type from the back office, and provide their own functions to manipulate fields in templates (but you can still use the WP functions if you want). For example, to get a custom field with Custom Field Suite you do: CFS()->get('my_custom_field').
Custom Post Types : sometimes, custom fields and widgets are not really convenient when you deal with a lot of data with possibly associated pages. In that cases, you can create your own post types just like posts or pages, but you can define which capabilities they have. Use register_post_type to define your new post type (still in init hook), that will make a new section available in your back office, just like posts. Then in your template you can query them with get_posts, the WP function that you will probably the most use when you'll start to play with Wordpress. I suggest you to read the WP_Query documentation in order to learn more about it.
And, at last but not least, you can create your own template that you can associate with any pages. For that, create a php file named whatever you want in your theme folder, paste into it the content from page.php and add this PHP comment on top :
/*
Template Name: My Template
*/
Then choose this template in the dropdown when you edit a page (on the right sidebar).
I think you will have enough to play with, but if you want learn more I recommend you to read about Wordpress hooks and the Widget API to create your own widgets.
Have fun with Wordpress!

Use Custom Post Archive Page for a specific Custom Post

I'm stuck on this issue with my Wordpress setup. My current setup is as follows: I have a custom post type "degrees" that are categorized by undergraduate, graduate, online, and abroad. The initial page is a specific taxonomy template I have set up for the user to specify which degrees to look through. Once a category is selected, the user will come to a landing page with all graduate programs, etc. This page is handled in the backend as a archive-degrees.php. Then, upon selecting a program, the template used is single-degrees.php. My problem is that I have one program (currently using the single-degress.php) that I would like to use the archive page as it needs to list out sub-programs. I hope that makes sense. Basically, I need this one specific Custom Post to use the archive page and then the single page for its sub-children. I am not sure if there is a way to do this. Any help will be much appreciated.
You're referring to specialized page templates. Your best bet is to create a new template for this page specifically.
From the Codex:
Create a template for one Page: For more extensive changes, intended for just one specific Page, you can create a specialized template file, named with that Page's slug or ID:
page-{slug}.php
page-{ID}.php
For example: Your About page has a slug of 'about' and an ID of 6. If your active Theme's folder has a file named page-about.php or page-6.php, then WordPress will automatically find and use that file to render the About page

How to display a wordpress custom post?

Okay, so I am completely lost on how to display a custom post. I've looked everywhere and can't seem to wrap my mind around it.
So, I downloaded the plugin Custom Post Type UI to create a custom post. Here are the parameters of my custom post type:
Name: discount
Label: Discount
Supports:
title,
excerpt,
thumbnail,
author
Then, I downloaded Advanced Custom Fields to give my custom post types some custom fields for users to edit.
I didn't have any problems with that. My problem is that I have no idea how to implement this into my site. I would like it to act exactly like a blog post and also implement a masonry for it - just like my blog posts (I'm using the dante theme (I can't post any more links)).
If anybody could help me out with this, or send me in the right direction, I would be so greatful. Thanks!
Your custom post type's theme template would be called "single-[post type name].php".
So for your example, you would want to create single-discount.php if your theme's root directory. Then you can start pulling your data from the posts through that.
For Advanced Custom Fields, you'll want to get familiar with get_field() and the_field(). ACF has great documentation on their site for how to use the various field types (http://www.advancedcustomfields.com/resources/)
To answer your questions in a general way, you could start by:
Theme Development. To understand how a WP theme works
Template Hierarchy. To understand what are the template files and how they are managed by WP
The Loop. To understand how WP shows the post
WP Query Class. To understand how to manage the posts, perhaps in the case of custom post type
For the custom fields, #Joe has pointed you in the right direction.
Hope it helps!

Categories