Call Wordpress Archive page with multiple category IDs - php

I'm using custom fields on my template page and I want create a hyperlink to multiple category IDs.
This snippet gets my custom post field and returns category IDs 23 and 19, or whatever series I put in the custom field, ie: 23,9,17:
<?php $featuredpost_cat = get_field('featured_category_id'); ?>
I want to do something like this (I don't know the proper calls and syntax):
<a href="<?php wp link to $featuredpost_cat ?>"My Link Title </a>
I hope this is clear enough.

To category link by category id:
<?php
$featuredpost_cat = get_field('featured_category_id');
$category_link = get_category_link( $featuredpost_cat );
?>
My Link Title
You can get more details about this in the wordpress function reference site.
http://codex.wordpress.org/Function_Reference/get_category_link

Try this:
My Link Title
Im just giving an general solution how you can link to the id hope this helps.

Related

Display custom field from parent page

On a child page x, i want to output a custom field from the parent page
<?php the_field('imagecategory'); ?>
The child page can also have this field (but i dont want to grab that)
so what i want is:
On the child page, grab the parent page id. And for that specific page show the output of the code
<?php the_field('imagecategory'); ?>
I asume i need to grab the parent id with $post->post_parent;, but i got no idea how to tell the code to grab that custom field for that specific page :(
Aparently you are using Advanced Custom Fields (ACF) plugin right? If yes, you must to get your custom page where you set your custom field before to show it on a different page, something like this:
<?php $mypage = get_page_by_title('my page'); ?>
<p><?php the_field('custom_field', $mypage); ?></p>
I hope it helps.

Button that links to same product in a different category wordpress woocommerce

I'm looking for someone who can help me out with a problem I'm running into at the moment.
I am trying to add a link onto my product page which redirects the user to the same product but under another category:
We have 2 different Bannersets for sale:
productname: Testproduct 1
productcategory: Static bannerset
info: A bannerset that exists of 7 sizes with a JPG extension
productname: Testproduct 1
productcategory: Interactive bannerset
info: A bannerset that exists of 7 formats in .jpg + HTML5 extension
now, on each individual productpage I want a link (basic html href link or button doesn't really matter) that redirects the customer to the same product but in the other category. for instance:
On the static bannerset product page I want a link that says:
I would like this product in the interactive version.
now if you click on this link, I want the customer to be redirected FROM the current url: http://examplesite.com/shop/static-banners/testproduct-1 TO: http://examplesite.com/shop/interactive-banners/testproduct-1
Is there any way to do this?
I already tried fiddling around with some php codes but I only managed to get the permalink and not change the url ...
I hope my explanation was clear enough to understand.
Thanks in advance,
Joost
<a href="<?php echo home_url(); ?>/shop/interactive-banners/<?php echo $post->post_name; ?>">
-add this inside the loop in your product template
What you need to do is check, under which category the product is currently display. i.e. Static bannerset or Interactive bannerset. And based on that information you can format the target link and/or the button text as well.
I assume you are okay with using the respective category slugs directly.
Following is the code for your reference.
//get current category object
$current_category = get_queried_object();
//get current category id and slug
$current_cat_id = $current_category->term_id;
$current_cat_slug = $current_category->slug;
//identify target category based on current category's slug.
$target_cat = ($current_cat_slug == 'static-banners') ? 'interactive-banners' : 'static-banners';
//for display text (if required)
$display_text = ($current_cat_slug == 'static-banners') ? 'I would like this product in the Interactive version.' : 'I would like this product in the Static version.'
//get target category link
$target_cat_link = get_term_link($current_category->term_id);
//display link as anchor tag
<?php echo($display_text); ?>
Hope this helps.

Add a custom field in a woocommerce category

Hi I have created with CTP custom fields plugin a custom field to appear only in taxonomy -product category- this field, appear in the back end when I edit the category.
Field is intended to be there to upload images and output the URL of the image like this:
<div class="page-heading" style="background-image:url('<?php the_field('field_name'); ?>') !important;">
This kind of code works perfectly OUTSIDE woocommerce (in the normal WP pages and posts) but for some reason, doesn't show anything, even that in the back end I can see the image attached to the category.
In the front end it shows like a empty field...
Im not sure what i'm doing wrong.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
the_field( 'field_name', $post->ID );
Thanks, your tips made me search the right thing and found the answer:
<?php
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
the_field('the_name_of_the_field', $post_id);
?>

Get Wordpress Category from Single Post

I'm finishing up a WP theme, and I'm on the single.php template. I'm having some issues because I need to access the parent category that a post is in in order to display certain images and XML content.
Here is an example of what I'm talking about. The following is the end url of a single post:
/andrew/leaf-art-2/
/andrew/ is the category, and leaf-art-2 is the single post. When I am on the single post, I am having trouble getting single_cat_title(); to return the category that the current post is in. I am using single_cat_title(); instead of the_category(); because it displays the string value of the category which I then use to place a picture of the artist (whose category this is) on their posts. I don't have any use for the url, I just need the string with the category name.
Any good ways of doing this? I have been searching the Wordpress Codex and lots of forums, and haven't found any answers yet.
The following was my original post.
I have set up a category called "artists" which when I run single_cat_title("", false); I can get the string value of the category and then use it to search for the appropriate artist image using XML.
This works fine on the category.php template page.
The problem is that when I'm actually inside of a single post that has the "artists" category, single_cat_title(); doesn't output any information to the page, thereby keeping me from accessing the XML data.
I need to, while in the "artists" > "sample" post, be able to get from WP the category.
P.S. the above category is one of many that is using this setup, which is why I can't hardcode it.
How about get_the_category?
You can then do
$category = get_the_category();
$firstCategory = $category[0]->cat_name;
For the lazy and the learning, to put it into your theme, Rfvgyhn's full code
<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name; echo $firstCategory;?>
<div class="post_category">
<?php $category = get_the_category();
$allcategory = get_the_category();
foreach ($allcategory as $category) {
?>
<a class="btn"><?php echo $category->cat_name;; ?></a>
<?php
}
?>
</div>
Get category names with permalink in single post without loop
the_category(',', '', get_the_ID())
If you want only the first one:
get_the_category(get_the_ID())[0]

Any idea why Wordpress's inCategorgy tag is not working?

So I am trying to add a "-none" to a class for a post if it is in a specific category in Wordpress. So like lets say if I am viewing a post that has a category id of 7, i want a certain class titled "example" change to "example-none".
Here's my code:
<div class="example<?= is_category('events') ?'-none':'' ?><?= in_category('7') ?'-none':'' ?>">
The weird thing with the code is, it works in a page when I am viewing all the posts in a specific category. But when I go to an interior post that is in a specific category, the code does not work.
I am using the in_category('7') tag to achieve this on a wordpress sidebar.
Any idea on what I am doing wrong?
I would remove the quotes around the id of the category:
in_category(7)
This should be a number, not a string.
Thanks. i got it working using this code:
<div class="example<? wp_reset_query(); ?><?= in_category(7) ?'-none':'' ?>">

Categories