Get current page url on woocommerce taxonomy pages - php

I try to use <?php echo get_permalink(); ?> on a product Archive/Taxonomy page to get the current pages permalink/display the current pages permalink text, but it gets the permalink of the first product that shows up on the archive page instead of the current page's permalink. So instead of getting something like "/product-category/fashion/" it gets the first product that shows up on the list like /product/green-shirt,
How can I get the current product taxonomy page's permalink?

Found it! I apologize for the trouble!
<?php
$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );
echo $current_url;
?>

Related

Output custom code on a defined sub category archive pages only in WooCommerce

Actually In WooCommerce, I am trying to display some custom code (a video), only in a defined product sub-category archive page.
This is the code I am using:
<?php if( has_term('viola-chan','product_cat' $tag_ID=18) ) { ?>
my video html code
<?php } ?>
But it doesn't work and it is showing up on other parent pages too.
Here is a live link
How can I output custom code on a defined sub category archive pages only in WooCommerce?
As this is related to display something on product category archives pages, you should use instead:
<?php if( is_product_category( 'viola-chan' ) ) { ?>
<div>my video html code here</div>
<?php } ?>
But "viola-chan" should be your sub-category hereā€¦
Related documentation: WooCommerce Conditional Tags: Product category page
Try third variable post id as shown below:
global $post;
if(has_term( 'viola-chan','product_cat', $post->ID )) {
//do something if post has viola-chan term
}else{
//do something else if the post don't have viola-chan term
}
Ref.- https://wordpress.stackexchange.com/questions/166576/if-product-is-in-sub-category-show-code

WooCommerce Hook into Category Info

I've been doing research for a couple of hours and going nowhere. Perhaps It's that I don't even know what I'm searching for.
I have a WooCommerce site running on Flatsome's latest version (Theme.)
I'm simply trying to get the category link for each item in the loop and make a CTA button to open the category. This is a widget on the homepage on flatsome.
function ill_category_button() {
$link = "#";
echo '<div class="add-to-cart-button">Open Collection</div>';
}
add_action('woocommerce_after_subcategory_title', 'ill_category_button');
The product category is passed to your function as a parameter, which you can then use to grab the link using get_term_link().
function ill_category_button( $category ) {
$link = get_term_link( $category->term_id);
echo '<div class="add-to-cart-button">Open Collection</div>';
}
add_action('woocommerce_after_subcategory_title', 'ill_category_button');

Wordpress Get the CURRENT Category from Single Post, Multiple Categories

I've got a Custom Post Type of Home Plan and a Custom Taxonomy of Community.
The same Home Plan appears in multiple different Communities.
Permalinks have been set, and now I'm trying to find the current Community from which I'm viewing the Home Plan.
Because the Home Plan appears in multiple Communities, the following example links all take you to the same Home Plan as intended:
domain.com/community-name-1/plan-name
domain.com/community-name-2/plan-name
domain.com/community-name-3/plan-name
In order to display the relevant information based on the Community, I'm attempting to find a way to determine the current Community.
In my single-home-plans.php file, I've printed get_query_object(); and get_the_terms(); but I haven't found anything useful.
A possible workaround would be to pass a $_GET value, but at this point that's an absolute last resort.
No such thing as current taxonomy, when you on single page. You can only check from which taxonomy user go to this page. You can do this by checking $_SERVER['HTTP_REFERER'].
Assuming your url are as simple as what you've provided, this will probably do,
// Get the Current URL path
$url = "{$_SERVER['REQUEST_URI']}";
// Convert URL path string to array
$e = explode('/', $url ); // You can dump this to verify which is the taxonomy slug
// Get all taxonomy slug assigned on this post
$taxonomy = wp_get_post_terms( get_the_ID(), 'your_taxonomy');
$tax_names = '';
foreach( $taxonomy as $tax ) {
$tax_names .= ' '. $tax->slug;
}
// check if 2nd array which is the current taxonomy exist on list of taxonomy assigned to post
if( isset( $e[1] ) && strpos($tax_names, $e[1]) !== false ) {
// Get Term Name by slug
$term = get_term_by('slug', $e[1], 'your_taxonomy');
_e( $term->name);
} else {
_e($e[1]. ' is not a valid taxonomy for this post');
}
If query_var is set to true in the taxonomy, you can detect which category is currently being viewed by using $wp_query (global),
$wp_query->query_vars['the_taxonomy_name'];
Run var_dump($wp_query->query_vars); to view the full array of all query information.
<?php global $wp_query;
$home_plan_community = $wp_query->query_vars['community'];
echo $home_plan_community; ?>

wordpress blog index is post page

I have a custom page named 'Journal', which I use as a blog index page for my wordpress website. I've run into a rather strange problem. When I enter <?php echo get_the_title(); ?> or whatever in home.php, it returns the title of a post, instead of the page title 'Journal'. Is anyone familiar with this problem?
Thanks!
This is the expected behavior for this page. When you set a page to be your "blog", you can't access the template tags for that page. Instead, the template tags are for the loop of the posts to be displayed on that page.
To get the title, you have to first get the id of that page, and then you can pass it to a function:
<?php
$page_for_blog = get_option( 'page_for_posts' );
$page_title = get_the_title( $page_for_blog );
?>
Now you can print the $page_title and you should see "Journal".
Updated with Advanced Custom Fields
Now that you have the Journal page's id ($page_for_blog), you can get your field values with:
$field_value = get_field( 'field_name', $page_for_blog );
Obviously, replace 'field_name' with whatever field you're trying to retrieve.

Advanced Custom Fields after the main content

I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );

Categories