I am trying to alter the url structure in the standard sitemap page produced by YOAST SEO plugin in Wordpress. I am able to alter Custom Post type sitemap using the below code, but I am not able to do it for a custom taxonomy page I have, In the if statement I used is_tax( 'city' ) but this did not work. Hotel is a custom post type and city is a custom taxonomy page that list hotels in a specific city.
function sitemap_post_url( $url, $post ) {
if ( $post->post_type === 'hotel' ) {
return \str_replace( 'hotel', 'test', $url );
}
return $url;
}
add_filter( 'wpseo_xml_sitemap_post_url', 'sitemap_post_url', 10, 2 );
I really appreciate if someone can help with the code I can use in the if condition to alter custom taxonomy archive links pages in YOAST xml sitemap
Related
I'm trying to display images attached to custom taxonomies on the individual product pages. I've used the Custom Taxonomy plugin (https://wordpress.org/plugins/taxonomy-images/) and added the images using Advanced Custom Fields. I have 2 custom taxonomies called 'range_logo' and 'features', each with images assigned to them. I need to output the images to the single product page using the hook 'woocommerce_before_add_to_cart_form'.
As you can see below, I have tried outputting a line of text to test if the hook is working correctly, and it is, but the taxonomy image isn't shown when I add the code which is supplied with the Taxonomy Images plugin.
add_action(
"woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
echo 'Taxonomy image here';
print apply_filters( 'taxonomy-images-queried-term-image', '' );
}
Expected results are that the appropriate taxonomy image for the product would be displayed. Instead I got this error:
Notice: term_taxonomy_id is not a property of the current queried object. This usually happens when the taxonomy-images-queried-term-image-id filter is used in an unsupported template file. This filter has been designed to work in taxonomy archives which are traditionally served by one of the following template files: category.php, tag.php or taxonomy.php. Learn more about template hierarchy. in /var/sites/a/mysite.com/public_html/wpnew/wp-content/plugins/taxonomy-images/legacy/includes/public-filters.php on line 398
I'm using the Avada theme - maybe the layout of the theme's template files could be the reason for the error? I think I may need to use archive.php instead, but I don't have much experience with PHP.
I've tried a few possible solutions and have looked elsewhere for help, but I've hit a wall with getting this to work and I'm not sure what I should be trying next.
welcome to the community. First off, I would like to mention that you have written a bit confusing detail about the taxonomy images as you have mentioned using the plugin Taxonomy Images as well as Advanced custom fields
If you are using the former plugin, you can do something as shown below:
add_action("woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'product_cat',
) );
}
The above code will give you the images from the category associated with that product. If it is another custom taxonomy that you created, please change the value in taxonomy parameter.
If you are using ACF you will have to get product terms first. Let me know if that's the case and I will share that code with you.
I've created a pretty basic plugin which adds a custom post type with a couple taxonomies and a few custom fields. I would rather use a plugin than simply add it to the theme because I think I might be useful in the future. The problem is that I want to customize the archive page for the particular post type. I could obviously just create a new file, in this case archive-research.php but having it in the theme directory would defeat the purpose of using a plugin. Is there a method whereby I could have the custom archive file as part of the plugin itself?
Use archive_template filter, from WordPress codex: https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
<?php
function get_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'my_post_type' ) ) {
$archive_template = dirname( __FILE__ ) . '/post-type-template.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
?>
I've created a custom archive page for a custom post type I've also created. I would like to style only this Custom Archive page. I've tried to create a function inside functions.php but it doesn't work.
My question is : how do I use a custom stylesheet for this archive page and not on the other ones ?
Thanks
function interviewlist_style() {
if ( is_page_template('archive-interview.php') )
wp_enqueue_style('Archive Interviews', get_stylesheet_directory_uri() . '/css/archive_interviews.css');
}
add_action( 'wp_enqueue_scripts', 'interviewlist_style', 1);
If I've understood the question correctly, you've created a template for your custom post type archive.
The conditional you're using would apply if you were viewing a page that had been assigned a custom template with the file name 'archive-interview.php'. As you're dealing with an archive, not a page, you need to use a different function.
Replace:
if ( is_page_template('archive-interview.php') )
With:
if ( is_post_type_archive( 'interview' ) )
This will evaluate to true if you're viewing the archive for a custom post type named 'interview'.
https://codex.wordpress.org/Function_Reference/is_post_type_archive
Using a WordPress multisite network, I need to access custom post types from our main site from within a shortcode.
For example, our main site (ID 1) is where the custom post types (Case Studies) are stored. In functions.php I have the following:
//[casestudy]
add_shortcode( 'casestudy', 'casestudy_shortcode' );
function casestudy_shortcode( $atts ) {
$a = shortcode_atts( array(
'id' => ''
), $atts );
switch_to_blog(1);
//Get fields from custom post type with Advanced Custom Fields Pro
//and return HTML output with them
restore_current_blog();
}
Then call the shortcode with [casestudy id="123"] where the ID is the post ID of the case study.
The problem is, doing this returns the Case Study HTML fine but breaks some features of the page and also populates the 'recent posts' widget with blog posts of the main site.
Any ideas on what's going wrong? Thanks.
Adding my comment as an answer:
Reading the OP code comments, it looks like restore_current_blog() is never called, because the HTML is returned before calling it.
Make sure that the return part is the last line in there.
I am building a site using WP and the Prosperent API. I have built out product category pages where the user can see all the "Backpacks" and even sub categories. I now want to build product detail pages. Because the data comes from an API, and not stored in my database I don't have an easy want to have pretty links.
On the category pages, I am linking each product like this:
$pageLink = esc_url( add_query_arg( array (
'gearID' => $value['catalogId'],
'gearTitle' => $value['keyword']
),
'http://www.example.com/products/g/' ) );
Which looks like this:
http://www.example.com/products/g/?gearID=65198586171ee0ea97f3e39935468f4d&gearTitle=The%20North%20Face%20Inferno%20Sleeping%20Bag:%20-20%20Degree%20Down%20Asphalt%20Grey/Caution%20Orange,%20Long
I AM Successfully capturing the "gearID" & gearTitle query variables, This allows me to display the product on the product detail page template. This is working great. But the URL has my query strings in it.
I have tried several different variations, but this is my code for the rewrite:
add_filter( 'query_vars', 'add_query_vars_filter' )
function custom_rewrite_basic() {
$gearID = get_query_var( 'gearID', false );
add_rewrite_rule('^products/g/([0-9]+)/?', 'index.php/products/g/?gearID=$matches[1]', 'top');
flush_rewrite_rules();
}
add_action('init', 'custom_rewrite_basic');
You can see in the code I have a get_query_var for the gearID, I attempted to insert where $match[1] is placed and didn't work. Perhaps I need to store data in DB so WP knows what to rewrite? Can't do it on the fly?
Ultimately, I want it to look like this:
http://www.cascadegear.com/products/g/The%20North%20Face%20Inferno%20Sleeping%20Bag:%20-20%20Degree%20Down%20Asphalt%20Grey/Caution%20Orange,%20Lon