PHP Archives - Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane | https://10webtest.10web.me/category/php/ Wordpress developer servicing Melbourne, Brisbane, Sydney, Perth, Adelaide, Canberra Mon, 18 Jul 2022 05:00:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://www.jane-james.com.au/wp-content/uploads/2021/07/cropped-1901_logo-01-1-32x32.jpg PHP Archives - Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane | https://10webtest.10web.me/category/php/ 32 32 Custom post type pagination in WordPress – redirect canonical https://www.jane-james.com.au/custom-post-type-pagination-in-wordpress-redirect-canonical/ Wed, 27 Apr 2022 06:24:38 +0000 https://alphaomegadigital.com.au/?p=13071 If you have been working with WordPress for a while, no doubt you have been stumped on how to handle custom post type pagination. “WordPress offers built-in functionality for navigating through posts. Theme developers can use simple links or numbered pagination to indicate the previous page or the next page in a given sequence.” Codex […]

The post Custom post type pagination in WordPress – redirect canonical appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>
If you have been working with WordPress for a while, no doubt you have been stumped on how to handle custom post type pagination.

“WordPress offers built-in functionality for navigating through posts. Theme developers can use simple links or numbered pagination to indicate the previous page or the next page in a given sequence.”

Codex

The problem is, this pagination built into WordPress does not work on custom post types, it only works using standard posts.

I’m going to be the first to admit that I was really stuck on this one. I went through various tutorials online including paginate links, wpza and even the wpsmith article to build a plugin in composer using custom rewrite URLs. None of them worked.

After much trial and error, I am going to share the code with you for how I got CPT pagination running successfully on my project.

  1. Go to your functions file and using the template redirect hook, and unhook redirect_canonical.

2. Next we are going to declare our pagination function and use the paged parameter within our wp_query loop. We are also going to remove our redirect canonical and template redirect filters. The gist is below if you’d like to copy:

3. Next, we need to alter the custom query in our custom post type archive page. For practical purposes I have called this template archive-template but if your custom post type category is moves you would call this archive-movies.php.

4. after your loop/endif statement, call in the pagination function using a div class of pagination

5. Finally, we need to jazz it up with some CSS. Feel free to change the hex colours and styles as required

And that is is my friends! Now when you click on page 2, instead of showing the URL of /page/2 and refreshing the first page, your custom post type template will calculate how many pages you have based on your max posts per page, and paginate through them.

I really hope this saves someone some time, and they don’t get stuck on this as I did.

Alpha Omega Digital is a WordPress agency based in Melbourne, Australia but also services clients from Sydney, Brisbane, Newcastle, Perth, Adelaide, Darwin and Hobart. Have a project in mind? Contact me here.

The post Custom post type pagination in WordPress – redirect canonical appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>
Can you have multiple page templates for custom post types in WordPress? https://www.jane-james.com.au/can-you-have-multiple-page-templates-for-custom-post-types-in-wordpress/ Tue, 14 Dec 2021 04:51:00 +0000 https://alphaomegadigital.com.au/?p=12952 I recently came across an issue on a client project, and that was: the requirement to create multiple templates for a CPT or custom post type. This client runs events, and each event should have a specific CPT with it’s own branding, style guide etc. But first: What is a custom post type? Custom post […]

The post Can you have multiple page templates for custom post types in WordPress? appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>

I recently came across an issue on a client project, and that was: the requirement to create multiple templates for a CPT or custom post type. This client runs events, and each event should have a specific CPT with it’s own branding, style guide etc.

But first: What is a custom post type?

Custom post types are specific, custom code-added posts that allow you to keep your site flexible. For example: You might want a section for members of the team and add them in different ways on their own page or list inside of WordPress itself with no need go through complicated editing processes every time someone joins/leaves- just edit one place!

Custom post types are a great way for marketers and site managers alike to add new functionality without having to edit template files, which should strictly be done by a WordPress developer.

First, let’s define what Custom Post Types are, how taxonomies are used, and how they integrate with ACF:

Custom Posts

Custom Post types allow you to create a new admin interface for managing content, media, functionality, and you can also create a custom filter or use a plugin to allow you to search by taxonomies. The main benefit a business gets by having custom post types is using different page templates, for each CPT. For example a book store may have custom post types, sorted by category of fiction, non-fiction, children’s and cooking. This means the user of the site can search and filter by these categories. It also means a custom template file can be created for each category- meaning you have a base design and configuration for each category without having to design a page from scratch.

Categories

The categories are a great way to organise your posts, depending on what you have activated in the theme. Categories can be used for organising your menu into category pages and displaying them by listing all of their content by for example a category of cooking. Once you have multiple categories setup as CPT, you can click on the category eg: cooking, and see every post with that category without having to trawl through the website or use WordPress default search function by keyword.

You can also add tags to your content which are searchable, as you normally would with WordPress posts. Categories and tags are known as taxonomies in WordPress.

Advanced custom fields

Custom fields are a great way to add additional information you want that can be used later in your theme. In our book store example, custom fields allow you to add more information about your book post such as its author or qualifications, which could also add to your search and filter function.

Now that we have cleared that up, let’s go back to the headline of this article which is how to have multiple custom post type templates.

How to use multiple custom post type templates in WordPress

When creating a new CPT, WordPress will search for the CPT template file that it will use for each category, and this is declared in your template files like so:

For this example, I am using a custom post type category of events. WordPress will search for a template file with the template post type of ‘event’, and use that template to display your event page.

When you have only one template file, your CPT backend looks like this:

You can see on the right hand side there is no option to select a different template file, like you usually have when you are creating a new page. This is because we only have one template file in use for the CPT with category of event.

So, we need to create another template file for the category of event, and we will have two template files to choose from.

My reason for doing this is the current event template is built in CSS Grid, which is not user friendly for the marketing manager who wants to use it and doesn’t know CSS. She wants a standard WordPress template, so I have copied the content of my page.php file from my parent theme, and into a new template file called vendor-event.php, which means we have a second event template for her to use with the usual WordPress page layout. Here is the code I used:

After you have created your second CPT template file, you can go to your CPT category in your dashboard – in this case event, click on ‘add new’ and now you can see there are two template files to choose from before you build your new page- one in CSS Grid and one in Flexbox.

Alpha Omega Digital is a WordPress agency based in Melbourne, Australia but also services clients from Sydney, Brisbane, Newcastle, Perth, Adelaide, Darwin and Hobart. Have a project in mind? Contact me here.

The post Can you have multiple page templates for custom post types in WordPress? appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>
WordPress: add a like button to your posts using PHP and Javascript https://www.jane-james.com.au/add-a-like-button-to-your-posts-in-wordpress-using-php-and-javascript/ Mon, 04 Oct 2021 05:15:54 +0000 https://www.jane-james.com.au/?p=10198 This week I had a client request a modification to an existing WordPress page template which already had two post anchor identifiers to retrieve get_the_author_meta and get_the_date. It was a custom template that displays a CPT by category to a page. You can see the example below of the get requests: So I found this […]

The post WordPress: add a like button to your posts using PHP and Javascript appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>
This week I had a client request a modification to an existing WordPress page template which already had two post anchor identifiers to retrieve get_the_author_meta and get_the_date. It was a custom template that displays a CPT by category to a page.

You can see the example below of the get requests:

So I found this repository on Github by Jon Masters and decided to implement it, but modifying the custom post type template instead of the standard blog page template.

Jon gives walkthrough instructions, but since I modified it for a custom post type template I will do my own walkthrough, in case you are looking to modify a CPT template as well.

1. Run Query Monitor to find the template file you are after

If you install the Query Monitor plugin, you can click on ‘templates’ to see which WordPress template file is being called in. It’s not perfect though. In this case, it showed me a template hierarchy of 5 template files, none of which contained the section I wished to modify. If this happens, then you can just dig around through the template files and if the naming convention is obvious you shouldn’t have too much trouble. In this case I was editing the header-insight.php

2. open your functions file and add the post-like.php file code

Ok, this will make your functions file enormously long, but since Jon has shared the code I am going with what he as built for us already. Of course you can refactor that code into a post-like.php template file and call it into your functions file, but let’s keep it simple and add the post-like.php into functions.php like so:

3. Change theme name

In the pile of code we just added to functions.php go hit control F, and replace “YourThemeTextDomain” with your theme name. This isn’t necessary but I think it makes your code clearer to read.

4. Add the javascript file to your library

Add this javascript to your theme/lib/js/simple-likes-public.js path and save. Here’s the code to save you time:

5. replace the svg with font awesome

I found the SVG file came out massive, and of course its easy to modify it with CSS, but it’s also easy to add font awesome icons in, and Jon has already commented out the instructions on how to do this. In case you would like to see it anyway:

6. add the likes function within the loop of your template

Now we are going to go into our template file, in my case it’s header-insights.php and find the section where the GET parameters that are already calling in the author and the date, and we are going to echo in our function here:

7. Check your page template and make sure your like button is working

Now we’re going to go to our post template file, refresh the page and see if our like button is working. If you click and un-click the button, the heart will turn on and off which is correct. The way the function is setup, it only allows one user to like a post by calling your IP address into the wp_postmeta table in SQL, so for example you cannot publish a post and click like a hundred times to look really cool, LOL. You can see the finished product below:

Jane James is a WordPress web developer based in Melbourne, Australia but also services clients from Sydney, Brisbane, Newcastle, Perth, Adelaide, Darwin and Hobart. Have a project in mind? Contact me here.

The post WordPress: add a like button to your posts using PHP and Javascript appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>