This is my first question.
I have a custom taxonomy set up in a theme inside a functions.php file.
I've added some extra meta fields for the custom taxonomy (category), also set through the functions.php file.
I am using the update_option() function.
Here is the part that is saving the options to the DB:
<?php
// save extra category extra fields hook
add_action ( 'edited_artists', 'save_extra_category_fileds');
// save extra category extra fields callback function
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}
?>
In my template file I am calling them like this:
<?php
$terms = wp_get_post_terms( $post->ID, 'artists');
foreach ($terms as $term){
$term_id = $term->term_id;
$term_name = $term->name;
$term_taxonomy_id = $term->term_taxonomy_id;
$term_slug = $term->slug;
//do you term meta stuff here
//print_r($term);
}
?>
This is where I use them (among other things), and it is of course inside the LOOP:
<div class="single-sculpture-artist-info">
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
<img src="<?php echo $category_meta['artists_photo'] ?>" alt="<?php echo $term_name; ?>">
</a>
<h3>
<?php echo $term_name; ?>
</h3>
<p><?php echo $category_meta['artists_city_province'] ?></p>
<p><?php echo $category_meta['artists_bio_excerpt'] ?></p>
</div>
All of this code works perfectly.
I started adding the content, but then it suddenly started to fail. I think it started when I tried to use one of the category (taxonomy) names I used while developing this whole system (my guess is that it was cached somewhere or something), but then I tried using it with different name, and adding some other which were not there before, and it fails as well. My best guess is that somehow the options table is overloaded with data (limit or something).
Is that even possible? I dont have a lot, 56 working posts in that taxonomy, and 34 categories(taxonomy terms).
I tried my best to get my head around it but couldn`t find what was the problem.
When I insert like 2 or 3 more posts, it starts messing around. So, this:
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
outputs correct link, but, this:
<img src="<?php echo $category_meta['artists_photo'] ?>"
doesnt. It outputs data from some other category (from the same CPT). I can provide additional info upon request.
Not answering to your question but do you know there’s a big change happening with WP 4.2+ where they’re going to be splitting taxonomy terms, so that taxonomy terms don’t share the same term id if their slugs match.
Please take a look at the links below for some details on how this can be addressed
https://make.wordpress.org/core/2015/02/16/taxonomy-term-splitting-in-4-2-a-developer-guide/
https://developer.wordpress.org/plugins/taxonomy/working-with-split-terms-in-wp-4-2/
Through the help of my network I found the solution to the problem. The line where I am calling:
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
I switched to this:
<?php
$category_meta = get_option( "category_$term_id");
?>
This seems to have resolved the problem. However, here are some other solutions that might be more relevant in the similiar situation:
https://en.bainternet.info/tax-meta-class-faq/
also, be sure to read the things Joe Wilson posted above. Thanks again.
Related
I'm trying to add a logo to a Woocommerce product attribute (Brand Logo) and then display that brand logo on the Single Product page for each product tagged with that attribute.
I've tried the way I thought it would work however looks far too simple and it returns null.
<?php
function brandLogo(){
global $product;
?><p><?php the_field('brand_logo'); ?></p><?php
}
add_shortcode('brandLogo', 'brandLogo');
?>
I've got the ACF field to return URL of the image and have also tried using get_field as well as the_field.
My php knowledge is quite limited so I can't quite figure out how to get this one sorted.
Cheers.
I have changed the code and learned that it is important to use 'pa_' for product attribute names. So 'pa_yourattribute'. Here is the working code:
<?php
function brandLogo(){
global $product;
$terms = get_the_terms($post->ID , 'pa_brand');
if($terms){
foreach ($terms as $term){
?><img src="<?php the_field('brand_logo', 'pa_brand' . '_' . $term->term_id) ?>"/><?php
}
}
}
add_shortcode('brandLogo', 'brandLogo');
?>
I'm having a hard time even coming up with a title for this.
If I'm editing the code for a regulator old blog post (content.php) and add
<?php the_category(', ') ?>
I get a clickable list of all the categories that post belong to.
I have a custom post type called research and a custom taxonomy associated with it called topics. I'm editing content-research.php I just want the topics to show up the same way when looking at a research post.
I tried
<?php the_topic(', ') ?>
and that was a complete failure.
So I'm hoping there is a simple solutions because I have some additional taxonomies for this post type I'd like to add as well.
the_category() function only searches terms from the 'category' taxonomy as you can see in the core file https://core.trac.wordpress.org/browser/tags/4.9.2/src/wp-includes/category-template.php line 75
You could write your own function or you can use this snippet:
$terms = get_the_terms( $post->ID , 'taxonomyname' );
foreach ( $terms as $term ) {
echo ''.$term->name.' ';
}
The answer from ovidiua2003 worked just needed this tweak.
echo ''.$term->name.' ';
So the whole thing would be
<?php
$terms = get_the_terms( $post->ID , 'taxonomy' );
foreach ( $terms as $term ) {
echo ''.$term->name.', ';
}
?>
I am trying to display all the posts with the same category in WordPress however its not displaying correctly and instead is just showing everything.
Here is the php code:
<?php
$related = get_posts( array(
'category_in' => wp_get_post_categories($post->ID),
'numberposts' => 3,
'post_not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<div class="post">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<h3><?php the_title();?></h3>
</a>
</div>
<?php }
wp_reset_postdata();
?>
Its taken from here: https://wordpress.stackexchange.com/questions/41272/how-to-show-related-posts-by-category
And if it helps here is the link to the website in question where the code isnt working:http://u1f8aki.nixweb23.dandomain.dk/cat-4-post-test/
The code in question is further down the page under the red text. You can see the category at the top in the breadcrumbs.
There are quite errors in your code, and I'm sure atleast one of them are the culprit in giving you the wrong result.
I've refactored your code with some comments explaining what and why has been changed:
<?php
// For readability, save our categories in a variable for later use.
// $post->ID has been replaced with get_the_ID(), $post might not be accessible depending if you're exposing $post as a global or not.
$categories = wp_get_post_categories(get_the_ID());
/*
Instead of using get_posts(), use the recommended Wordpress loop in the form of WP_Query().
We start by defining our arguments for the loop
*/
$args = array(
'category_in' => $categories, // here we use variable for readability
'posts_per_page' => 3, //numberposts and posts_per_page has the same function, but posts_per_page is the more common of the two (IMO)
'post__not_in' => array(get_the_ID()) // you were missing a '_', ie post_not_in instead of post__not_in
);
// Start the loop
$query = new WP_Query($args);
if($query->have_posts()): while($query->have_posts()): $query->the_post();
// No need to setup or reset postdata when using this method, it does it for you!
?>
<div class="post">
<a href="<?php the_permalink(); ?>">
<?php
if( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<?php
// the_title() actually takes opening tag and closing tags as arguments in its function. So add the <h3> code like this.
the_title('<h3>', '</h3>');
?>
</a>
</div>
<?php endwhile; endif; ?>
Long story short, your code prolly isn't working because of the miss-named arguments. If you don't feel like replacing your code with my example, just change your arguments from numberposts to posts_per_page, and post_not_in to post__not_in.
If it still isn't working, check what wp_get_post_categories(get_the_ID()) is returning for each post, and make sure all posts aren't sharing some category you missed.
Edit: numberposts is actually a valid argument, changed my answer to reflect this.
I'm using keremiya theme for wordpress. I was trying to display my most viewed post in my custom post type if "most_viewed" option is on. The name of my custom post type is watch. How can i do this with my current code? I am also using a plugin called wp-post views to display the views in my sidebar. Here is my query.
<?php if(get_option('most_viewed') == 'On'): ?>
<div class="sidebar-right">
<h2><?php echo get_option('my_title'); ?></h2>
<div class="fimanaortala">
<?php $tavsayi = get_option('keremiya_tavsiyesayi'); $tavkat = get_option('keremiya_tavsiyekat');?>
<?php query_posts('showposts='.$tavsayi.'&v_orderby=desc&cat='.$tavkat.'') ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="filmana">
<div class="filmsol">
<?php keremiya_resim('80px', '70px', 'izlenen-resim'); ?>
</div>
<div class="filmsag">
<div class="filmsagbaslik">
<?php the_title(); ?>
</div>
<div class="filmsagicerik">
<?php if(function_exists('the_views')) { the_views(); echo " "; } ?>
<p><?php nezaman_yazildi(); ?></p>
</div>
<div class="filmizleme">
<img src="<?php bloginfo('template_directory'); ?>/images/filmizle.png" alt="film izle" height="21" width="61" />
</div>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
Your solution (or attempt, at least), is based on a plugin called WP-PostViews, which I have no knowledge of. So, I can't really help you there. I can, however, help you solve it without this or any other plugin. So here we go:
Wordpress has a little something called metadata. From this very own link:
The Metadata API is a simple and standarized way for retrieving and manipulating metadata of various WordPress object types. Metadata for an object is a represented by a simple key-value pair. Objects may contain multiple metadata entries that share the same key and differ only in their value.
That means you can create metadata for your custom post type that contains how many times it has been seen. To do so, we create a function:
<?php
function set_views($post_ID) {
$key = 'views';
$count = get_post_meta($post_ID, $key, true); //retrieves the count
if($count == ''){ //check if the post has ever been seen
//set count to 0
$count = 0;
//just in case
delete_post_meta($post_ID, $key);
//set number of views to zero
add_post_meta($post_ID, $key, '0');
} else{ //increment number of views
$count++;
update_post_meta($post_ID, $key, $count);
}
}
//keeps the count accurate by removing prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
?>
This will, given the post ID, increment a counter every time a post is viewed. Of course, we have to call this function somewhere in our code so it actually runs. You can do this in two ways: You can either call this function on your single template (which I believe is called single-watch.php), or you can add a simple tracker. I favor the second option, as it keeps your single post loop cleaner. You can achieve such tracking this way:
<?php
function track_custom_post_watch ($post_ID) {
//you can use is_single here, to track all your posts. Here, we're traking custom post 'watch'
if ( !is_singular( 'watch') ) return;
if ( empty ( $post_ID) ) {
//gets the global post
global $post;
//extracts the ID
$post_ID = $post->ID;
}
//calls our previously defined methos
set_views($post_ID);
}
//adds the tracker to wp_head.
add_action( 'wp_head', 'track_custom_post_watch');
?>
There you go. Now, WordPress checks if the user is visiting a page corresponding to a watch single post. If they are, it increments the counter.
The only thing left now is to query for the post with the highest number of views. This is easily achievable using WP_Query. When you create your loop, do something like this:
<?php
$query = new WP_Query( array(
'post_type' => 'watch', //your post type
'posts_per_page' => 1,
'meta_key' => 'views', //the metakey previously defined
'orderby' => 'meta_value_num',
'order' => 'DESC'
)
);
while ($query->have_posts()) {
$query->the_post();
//whatever code you want
}
?>
I kept my answer to PHP, so you can adapt to your mark-up needs. I also assumed your post-type is indeed called watch. I hope all of this helps you. If you want to query your posts in a slightly different way, I suggest you read the WP_Query docs. Cheers.
Simple Wordpress problem - get_post_meta is not retrieving custom field values. Here's the code that is pulling from the custom fields:
<img src="<?php echo FCG_PLUGIN_URL; ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, 'slider_image', true); ?>&h=250&w=400&zc=1" alt="<?php echo $post_title; ?>" />
In production, this is the HTML I get:
<img alt="Post Title" src="http://***.com/wp-content/plugins/jquery-slider-for-featured-content/scripts/timthumb.php?src=/&h=50&w=80&zc=1">
You can see the src= point in the string is empty - as if there is nothing posting from it. I have isolated and echo'd just the get_post_meta and it's a whitespace. I am 100% sure it's named correctly within the post - is there something glaring I'm missing here?
If you are calling get_post_meta inside the loop then you should call get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true)
Strange things happens when you call get_post_meta inside a loop. In some themes developers hack the $post at the beginning and get_post_meta stops working so this is one of the solution for those particular cases too.
Search for the term "slider_image" in the wp_posts and wp_postmeta tables using phpmyadmin. Then view the row that has it to see if there's anything inside.
Also try changing the name of the custom value as a test and see if that works. I use this exact code to do something similar to you and it works:
<p><img src="<? bloginfo('template_url'); ?>/img/downloadresume.png"></p>
Its because of auto save.
use these lines for preventing auto save and user privileges.
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
You could also use get_post_meta( $loop->post->ID, 'yourkey', true ); if you are using $loop = new WP_Query( $args ); or something similar.
Actually, it gave you '/', which is not nothing. I'd say that's what's saved as 'slider_image'. Check the post (or the database directly).
I've written some simple templating functions that enable you to use the meta data (custom data) in your theme. You can write a template function for any meta data key/value pair, and render it in a theme file like so:
<?php the_meta_templates($meta_data_keys) ?>
<?php the_template_for($meta_data_key) ?>
Feel free to check out the basic functions from github and give them a try. You'll need to add them to your themes functions.php file.
<?php get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true) ?>
Works for me!
could it be linked to the bug
#18210 (Update_post_meta is case insensitive on meta_key, but get_post_meta is NOT) – WordPress Trac
https://core.trac.wordpress.org/ticket/18210
It would explained the different experiences, depending on db_collation... (forgive me if it total nonsense, I am a newbie .. )
WordPress Database Charset and Collation Configuration | hakre on wordpress
http://hakre.wordpress.com/2010/12/26/wordpress-database-charset-and-collation-configuration/
<?php
// Get custum fields and values
$mykey_values = get_post_custom_values('my_key');
foreach ( $mykey_values as $key => $value ) {
echo "$key => $value ('my_key')<br />";
}
?>