how to retrieve custom fields on taxonomy page? - wordpress

I am using the "advanced custom fields" plugin and need to have it so that a custom field is pulled in for the category pages. I can get these to come in on pages, but the category pages are giving me a lot of trouble... 'video' is the name of the custom field I want to pull in.
This is the code I am currently using:
<?php echo get_field('video', 'clear-creek'.$wp_query->queried_object->term-4); ?>
or just a standard version like this which works on the regualar pages...
<?php the_field('video'); ?>
but it's not working... can someone please help steer me in the right direction?
Thanks!

If you are on a category's archive page, you would use this:
<?php echo get_field('video', 'category_'.get_query_var('cat')) ?>
If you are on a custom taxonomy instead, you would use this:
<?php $queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
echo get_field('video', $taxonomy . '_' . $term_id); ?>
This will dynamically get the taxonomy's slug and ID, and build out your get_field based on that information.

Related

How to Show taxonomies terms custom field value using ACF plugin

I have used Advance Custom Field (ACF) wordpress plugin to add custom filed on Woocommerce -> Product -> Categories form.
Right now i can't able to print that field value on categories page.
Please see this screen shot to know field slug name and other details -> http://nimb.ws/BsSiJO and help me to show that field value on categories page.
Thanks,
Ketan.
Display a field
<p><?php the_field('field_name', $term); ?></p>
Retrieve a field
<?php
$variable = get_field('field_name', 'product-cat_23');
// do something with $variable
?>
Finding the term related to the current post
<?php
// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'category');
// we will use the first term to load ACF data from
if( !empty($terms) ) {
$term = array_pop($terms);
$custom_field = get_field('category_image', $term );
// do something with $custom_field
}
?>

Can't retrieve custom taxonomy within multisite

I'm currently working on a Wordpress multisite. While I can retrieve custom post types from within child blogs by using switch_to_blog(1) I cannot retrieve any custom taxonomies this way.
For instance, this page on the main blog lists posts from the "employment" post type and shows the custom "location" category associated with it.
http://209.59.177.85/employment-opportunities/
This one is within a child blog, and the template code is after a call to switch_to_blog(1):
http://209.59.177.85/waterloo/careers/
Same code, but the custom post type shows, and the custom taxonomy does not. The page does not generate any errors. I can't even get a basic term list printed on the page for the taxonomy.
Has anyone done this before?
Thanks!
Here's the solution, in case anyone is looking. The terms can, of course, be accessed directly from the db:
<span class="date">Location: <?php
$jobid = get_the_ID();
$queryterms = "
SELECT *
FROM ".$table_prefix."terms terms, ".$table_prefix."term_taxonomy term_taxonomy, ".$table_prefix."term_relationships term_relationships
WHERE (terms.term_id = term_taxonomy.term_id AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id)
AND term_relationships.object_id = ".get_the_ID()."
";
$terms = $wpdb->get_results($queryterms, OBJECT);
if ( $terms != null ) {
foreach( $terms as $term ) {
echo "<a href='/location/";
print $term -> slug ;
echo "/'>";
print $term -> name ;
echo "</a>";
unset($term);
} } ?> | Posted on: <?php the_time('F j, Y'); ?></span>

get value of image custom field at frontent of wordpress?

Please help me out.I am battling since three days.
How to fetch value of a custom field to display it in post content area?
You can get custom field to display using below code:
get_post_meta($post_id, $key, $single);
Custom field data can be fetched inside template (inside loop) using this functions:
Fetches all fields
<?php the_meta(); ?>
OR for specific value
get_post_meta($post_id, $key, $single);
You can learn more + examples in official wordpress documentation about custom fields.
You can use this through below code:
<?php echo get_post_meta($post_id, $key, true); ?>
Here your arguments must same as you have mention in function.php, while creating your custom field like as in:
<?php add_post_meta($post_id, $key, $single,true); ?>
Thanks.

Using wp_query to pull content from a specific post using either title or id

I am trying to pull excerpts and custom fields from specific posts, I have tried using post titles and post id but I am only succeeding in pulling every single post using this query. For example I have this code trying to pull just the title for the post with id 182
<?php $map = new WP_Query();
$map->query('post_id=182'); ?>
<?php while ($map->have_posts()) : $map->the_post(); ?
<?php the_title(); ?>
<?php endwhile; ?>
It pulls the title of every single post using this method and I can not figure out how I am going to have multiple loops like this each pulling content from just one specific post. Can someone please explain where I went wrong?
I've had luck with WP_query('p=182').
If you know the post ID then you can use get_post($post_id); like so
$post_id = 182;
$my_post = get_post($post_id);
$title = $my_post->post_title;
echo $title;
echo $my_post->post_content;
ckeckout codex

Wordpress Category Link get_category_link(id)

I need to link to a category in my wordpress site. The following code works, somewhat:
<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'People' );
// Get the URL of this category
$category_link = get_category_link( $category_id );
?>
My problem is that it includes /category/ in the url, which isn't how my permalink structure is designed. Does anyone know a way around including /category/ in the url it outputs?
I don't understand what you want to do. Look here Template Tags/wp list categories « WordPress Codex for the template tag for category menus that will include whatever category base you have set. If you want to output the link to a category on the category page itself, then use:
<a href="<?php bloginfo('url'); ?>/<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>" title="<?php echo $category[0]->category_nicename; ?>">
<?php $category = get_the_category(); echo $category[0]->category_description; ?></a>
I found a plugin that does work with 2.9:
http://wordpress.org/extend/plugins/wp-no-category-base/
I'm going to leave the question open, though, for those who may know how to solve the problem without a plugin.

Resources