I'm having an irritating problem with my Wordpress site I was hoping you might be able to help me with.
Basically, I have a page - Case Studies, which was called 'case-studies' in the nav structure. It was later removed and re-added, forcing it to 'case-studies-2'. Is there any way of editing this? I tried changing the permalink within the page itself but it reverts to 'case-studies-2' after being changed.
My second issue is with the custom post type 'Case Study' which ties into this page. I can get the page to pull the custom posts in a list, but their permalink goes to a 404 and resetting the permalink structure didn't fix it!
Lastly, my attempts to resolve this problem have led to my Case Studies page bouncing back to the homepage no matter what I do with the template file.
Thanks in advance folks,
Graham
The first page of Case Study with nav-structure case-studies is still available in trash of post so firstly delete that page from trash & then edit your new case study page and in that edit permalink and remove 2 just like case-studies-2 to case-studies
For second issue you have to register your custom post type to theme's function.php file
For this add this code to function.php file
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'album', 'movies','music_review') );
return $query;
}
after this reset your permalink
Issue resolved - the old file was in the bin, not properly removed. Deleting it from there allowed me to update the permalink directly on the page :)
Thanks for your help, peeps!
Related
EDIT: Found my old code, that worked, and got it fixed that way. Also gonna use a plugin to add custom functions, to avoid it dissaperaing again with next theme update (Thanks Heba). And the code:
function foo_modify_query_order( $query ) {
if (get_the_ID()==80) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } } add_action( 'pre_get_posts', 'foo_modify_query_order' );
So, it should be a simple task, but I have no idea why it's not working by now. I have tried everything, every Google link is purple, so now I hope someone can tell me what it is, I'm doing wrong.
I should start by mentioning that it's a Divi theme, but I did get it to work a few months ago, but forgot how, and I can't recreate it now.
I'm just trying to sort the main query alphabetically (A-Z), but only on a page called recipes. The site have all the recipes as posts, together with their blog posts, but the blog posts are sorted by date and showed on a separate page, which works fine.
But weird stuff are happening, when I try and alter it. If I use this code, it actually works, but the blog page also get sorted alphabetically, which it shouldn't.
function foo_modify_query_order( $query ) {
if (is_archive()) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'foo_modify_query_order' );
Which doesn't make sense to me, since it's posts? Out of all the ways I've tried, it only sorts if I use is_archive().
If I try to make it check for a specific page first, like && is_page('recipes') I get an error.
If I just add if (is_page('recipes')){ echo 'Recipes'; } it works, but if I try to add the sorting inside, I get nothing.
So partially working, but I can't combine the two. Is it a Divi issue or am I doing it wrong? Hope anyone can help, I've tried so many different things now. Cheers :)
I would recommend creating a child theme where you will create a new page template to be used in the 'recipes' page.
First, use Child Theme Configurator plugin to set a new child theme, don't forget to select Copy Menus, Widgets and other Customizer Settings from the Parent Theme to the Child Theme option to keep your settings:
After that, from files tab in the child theme configurator, copy the page template used for blog (or the page template used in 'recipes' page).
After that, change the file name, and the name written in the top of the page template file after Template Name, to recipes ordered alphabetically or any name you would prefer like next:
/*
* Template Name: recipes ordered alphabetically
*/
You can add these args to the query:
$args['orderby'] = 'title';
$args['order'] = 'ASC';
And change 'recipes' page template to the new created one.
For more detailed answer, Please insert the code used in the page template.
i'm developing a web portal using wordpress.
I need to access draft posts from frontend.
If i login with administrator account i can view draft posts(admins posts) in single pages. But other users can't access their own drafts from frontend and gets 404 error.
The links on frontpage like http://website.com/?p=486
I'm not sure whats the problem about. Maybe access restrictions or permalink settings or 404 settings or something else. Any ideas?
I found the solution. The problem is single page only shows published posts.
With using wordpress pre_get_posts hook i changed the query and add also draft posts.
function ceo_single_page_published_and_draft_posts( $query ) {
if( is_single() ) {
$query->set('post_status', 'publish,draft');
}
}
add_action('pre_get_posts', 'ceo_single_page_published_and_draft_posts');
The solution that works for me is to query for published and draft:
query_posts(array(
'post_status' => array( 'publish', 'draft')
));
As I understand by default Woocommerce shop page uses product archive template. What I am looking for, is to use a custom template for shop page.
Here is what I did:
Create template "my-shop"
Create page "My shop" -> choose template "my-shop"
Choose "My shop" as Woocommerce shop page
But none of the changes I made to "my-shop" template are present on the shop page.
What am I missing here? I would not like to change product archive itself, just the shop page.
Is there a way to disable product archive from being a default for shop page?
Thanks
I know it's too late and you may have figured it out by now. In any case, the changes that you would like to make to the WooCommerce Shop Page need to be done in the archive-product.php and it would be safer to create a child theme and do these changes. Making enhancements and customizations in a Child theme is best practice so that you can update the parent theme at any time and it won't affect your store.
I hope this helps, for more information on how you can use WooCommerce short-codes to customize your store can be found here.
To add to Silver Ringvee's answer - he used is_page but that only works on wordpress pages. For woocommerce you need to use something like is_woocommerce() . See Woocommerce conditional tags page.
My example code uses the is_shop conditional tag as that was the page you wanted to change. the code get_template_part( 'content', 'shop' ); will call the file content-shop.php in your theme root folder. This code is to be added at the top of wp-content\themes\*theme*\woocommerce\archive-product.php that you can copy from wp-content\plugins\woocommerce\templates\archive-product.php
You can add it just before get_header( 'shop' ); line 23 in my file - and the entire page will be drawn from your template. If you want to keep the header of the shop page, then put this code after the get_header code. Remember to include a footer in your file as well
if (is_shop()) {
get_template_part( 'content', 'shop' );
} else {
#normal archive-product code here
}
The solution (not perfect) that I figured to work best, until someone finds a way to actually change the template from dashboard:
Adding:
<?php
if (is_page( 'Page Title' ) ):
# Do your stuff
endif;
?>
to content-product.php in my theme's woocommerce folder.
If you prefer to go with code, you can create a redirect from the original shop page to your page via wp_redirect.
add_action('template_redirect', 'bc_010101_redirect_woo_pages');
function bc_010101_redirect_woo_pages()
{
if (is_shop())
{
wp_redirect('your_shop_url_here');
exit;
}
}
More detailed tutorial can be found here
This is not possible to create custom template for shop page , just copy and paste woocommerce Templates into you theme folder and Try to work in content-product.php template .
This question is based on an unanswered question in Wordpress Development which has not gotten a solid answer.
I have a wordpress website which lists hotels. the url for a single hotel looks like:
/hotels/the-marriot-hotel
I also have a custom taxonomy for Locations, which allows me to browse the hotels in various locations, which works fine, the urls are like:
/Locations/Liverpool
For URL's like /hotels/* I would like to use a custom template, which I have done already and works fine.
The Problem
I also want to be able to drilldown the Locations taxonomy creating a breadcrumb type URL and also use a different template for the hotel page.
For Example, if a user is browsing /Locations/Liverpool and clicks the Marriot Hotel I would like it to click through to /Locations/Liverpool/the-marriot-hotel instead of /hotels/the-marriot-hotel and also use a slightly different template, which can also load a different sidebar and recommend other hotels in the area specific to the location slug in the URL
So basically I want two routes to a single post and a different template used based on the route used.
How would I go about implementing this?
What have I tried?
I've tried adding a new page and using a rewrite rule to point to it to be the locations hotel page.
I've tried adding a slug on the end of the /Locations/{location-slug} url and reading this in the page template and loading the hotel post instead of the list it doesn't seem to be working but also feels like a terrible hack anyway
An idea that I've had is to add a rewrite to the hotels/{slug} page and using code to detect the URL used and switch templates dynamically but I'm not sure this is the best approach
I have managed to get this working using the second method mentioned above (adding a rewrite to the locations landing page and checking for a query_var).
I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.
I used this online post for reference.
Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.
function prefix_locations_rewrite_rule() {
add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}
function prefix_register_query_var( $vars ) {
$vars[] = 'hotel';
return $vars;
}
function prefix_url_rewrite_templates() {
if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/taxonomy-locations.php';
});
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );
In my template file for the hotels landing page:
$hotelSlug = get_query_var( 'hotel', false);
if ( have_posts() ) {
while (have_posts()) : the_post();
if ($post->post_name == $hotelSlug) {
break;
}
endwhile;
}
This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.
We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.
I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue
The title might not be completely clear, but I didn't know how to ask this in another way.
I want to build a system in Wordpress where the user can put some projects together where it would be on an url like http://mywordpress.com/projectbuilder/ or something like that.
Normally I would create an page in the admin menu and set it to a certain template and in the content I would put some text like: "Do not delete this page, this content is not shown".
But I think there must be a better way to add a custom page to a certain URL without adding it in the backend as a page with "useless content" since the content would not be changeable from the backend in this case.
I hope this makes sense. How could I go about that?
I think I could achieve this with a custom plugin but I can't seem to find any code how to go about that. I have found a way to add administration pages in the settings menu on the right. But I want to add a page to the website on the front end.
Sorry i didn't got your question properly. but some what say to create Custom post or taxonomy :
Please check below link
Custom Post and Taxonomies
In your functions.php file add this anywhere:
function themeslug_projects() {
$args = array(
'public' => true,
'label' => 'Projects',
'rewrite' => array( 'slug' => 'projects' ),
);
register_post_type( 'projects', $args );
}
add_action( 'init', 'themeslug_projects' );
Do save your permalink settings again after doing this, this will work surely then..
basically you can do this by creating a rewrite rule and then point to a file.
add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
// add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
//basically tell wordress to add a query var if sidebar is added to url. change sidebar to what you want your link to be.
// i set up a custom post type to make this work called custompostype..it does nothing but just to give post_type a value.
add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=customposttype','top');
}
// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
array_push($vars, 'is_sidebar_page');
return $vars;
}
// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
if(get_query_var('is_sidebar_page')){
$new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
if(file_exists($new_template))
$template = $new_template;
}
return $template;
}
after adding any rewrite rule, the changes wont take place until you go into settings->permalinks and hit the "save" button.