cboakes
Forum Replies Created
-
Forum: Hacks
In reply to: WP_Query tax_query in pluginThanks for taking the time to reply bcworkz.
I’m trying to write a plugin that returns JSON encoded results. A bit like JSON API but with the ability to query ACF fields and multiple taxonomy terms.
I’ve constructed my query (basic example in the question) which, if I leave out the tax_query part works fine. However with the tax_query as an argument it doesn’t return anything.
I wasn’t sure if WP_Query was the best way to approach this kind of query or whether I need to go down the $wpdb route.
Forum: Fixing WordPress
In reply to: ‘If statement’ to determine how many tags a post hasFor anyone who is interested:
Forum: Fixing WordPress
In reply to: Display Post Titles Under Tag Name in SidebarHi,
Thanks for the reply but I’ve just figured it out. Here’s how I ended up doing it. I realise that this may not the most perfect method but it works. If you have any suggestions post them up.
To clarify: the following code looks to see if a post has more than one tag, if it does it displays that tag as a header and displays the other tags with that tag below. If the post only has one tag it displays it as a link.
To demonstrate.
Post 1 has tags of ‘Music’ and ‘Rock’
Post 2 has tags of ‘Music’ and ‘Jazz’
Post 3 has a tag of ‘My Band’It would look like this:
-Music
–Rock
–Jazz
-My Band<?php //Set up an array to put all of the tags that have been used in drop-dwon $used = array(); //Get all the tags $tags = get_tags(array('orderby' => '', 'order' => 'ASC') ); //For each tag foreach ($tags as $tag) { $post_tag = get_term($tag, 'post_tag'); //Get posts limited only to the current tag $t_posts=get_posts(array('orderby' => 'date', 'tag__in' => array($tag->term_id))); //If the post has more than one tag if ($post_tag->count > 1) { //Display the drop down title (e.g if two posts were tagged with 'Music' this would diaply that 'Music' title echo '<h4 id="' . $tag->term_id .'">' . $tag->name . '</h4> <br />'; //Declaring the title as a variable for later use $taghead = $tag->name; //For each post with the current tag foreach($t_posts as $t_post) { setup_postdata($t_post); //Get all of the posts under the current title $the_tags = get_the_tags($t_post->ID); foreach($the_tags as $the_tag) { //Don't diaply the current tags title if ($the_tag->name == $taghead) continue; //Display the tag under the title echo '<p class="tag-' . $the_tag->term_id . '"><a href="' . get_tag_link( $the_tag->term_id ) . '">' . $the_tag->name . '</a></p>'; //Push the current tag into the used array array_push($used, $the_tag->term_id); } //End foreach tag } //End foreach //If the post only has one tag then link to it as an archive } else { //If the tag has already been displayed above don't show it here if (in_array($tag->term_id, $used)) continue; //Display the rest of the tags echo '<h4 class="tag-' . $tag->term_id . '"><a href="'. get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a></h4><br />'; } // End else ?>Forum: Fixing WordPress
In reply to: Display Post Titles Under Tag Name in SidebarI’ve tried my best to do this but it isn’t working for me – I know it’s a lot to ask but is there a way you could give me an example please?
Forum: Fixing WordPress
In reply to: ‘If statement’ to determine how many tags a post hasDoes anyone know how I might even start to do this – I’m really stuck?
Thanks
Forum: Fixing WordPress
In reply to: Display Post Titles Under Tag Name in SidebarI figured it out, here’s how I did it for anybody interested:
<?php $tags = get_tags( array('orderby' => 'name', 'order' => 'ASC') ); foreach ( (array) $tags as $tag ) { $args=array('orderby' => 'date', 'showposts' => -1, 'tag__in' => array($tag->term_id), 'caller_get_posts'=>1); $posts=get_posts($args); echo '<h2>' . $tag->name . '</h2> <br />'; foreach($posts as $post) { setup_postdata($post); ?> <p><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p> <?php } } ?>I found this topic on how to do the same thing with categories extremely useful 🙂
Thanks for your help!
Forum: Fixing WordPress
In reply to: Display Post Titles Under Tag Name in SidebarThanks for the response Michael.
I think I see what you mean, so, for instance if I was going to display the posts ID under the tags title I would do as follows:
<?php $posthead = get_posts(array('orderby' => 'name', 'order' => 'ASC', 'category' => '3,5,6,7,8')); foreach($posthead as $post) : setup_postdata($post); ?> <?php $posttags = get_the_tags($post->ID); foreach($posttags as $tag) { echo '<h2>' . $tag->name . '</h2>'; echo '<p>' . $post->ID . '</p>'; } ?> <?php endforeach; ?>However my main problem is that it will display the tag title and then the post title underneath for each post. So for example if I had two posts tagged with ‘music’, then it would look like this:
Music
band_1
Music
band_2Thanks
Forum: Fixing WordPress
In reply to: Exclude post in sidebar based on selectionFigured it out, thanks to this article:
So the variable needed to be declared as:
$post->IDForum: Fixing WordPress
In reply to: Exclude post in sidebar based on selectionI’ve figured out what I think is the best way to approach it but I still need help with its completion:
I use:
<?php query_posts(array('category_name' => 'news', 'post__not_in' => array(31))); ?>
This would exclude, in this instance, the post with the ID of 31.So now I need to make it dynamic. My knowledge of php isn’t great, but I thought declaring a variable of the current post would work, so something like:
<?php $excludeid = 31; query_posts(array('category_name' => 'news', 'post__not_in' => array($excludeid))); ?>This worked fine. So to make it dynamic my logic was to go with:
$excludeid = the_ID();But this just doesn’t work, would anybody help me to try and make this dynamic?
Thanks
Forum: Fixing WordPress
In reply to: Separating wp_list_pages with a /I did notice that – it is an issue that I’m going to try and solve.
What do you think would be the best way to approach it?
Thanks
Forum: Fixing WordPress
In reply to: Separating wp_list_pages with a /Thanks esmi,
I managed to figure out by simply looking up the ‘wp_list_pages’ in the codex, there I was able to find that there is in fact a parameter that can be passed called ‘link_after’, which does what I want. So to display a / character after each link I would simply just pass the parameter like this:
wp_list_pages(‘title_li=0&sort_column=menu_order&link_after= /’);
Chris