link to other posts in same category
-
On the single post template, I would like to add links to my other individual posts, by title, that are in that same category. So, if the post category is “apples,” I’d like to add a list of my other posts on “apples.”
I want to place this in the sidebar — I believe I understand how to “re-start” the Loop in the sidebar for post-associated information.
So far with my meager programming skills I cannot figure a way to do this — can you help? I’ll post the answer including the coding, if it requires coding, here.
Thank you!
-Sara
-
See the Conditional_Tags – that should get you started.
Thank you, Moshu. Can you tell me, is there some sort of
is_category('this')
sort of equivalent? It is obvious to me how to create something for ONE specific category, but I want a list that is generated based on what category/ies the current post being viewed, is in.“Other posts in this category” might be how I’d phrase it, and the “this” is the condition I’m interested in finding.
The “Conditional_Tags” page seems not to cover that contingency — or am I missing something?
Thanks again, please let me know.
Well, with a post in more categories it will be way more complicated, I guess.
Having a post in one category (say cat ID# 5) – you just need to build an if statement with in_category (that’s for single posts, NOT is_category!):if in_category(‘5’)
show this…
elseif
show something else…I am not a coder, so see the working examples there; the above is just the “logic”.
Yeah, logical but not extensible. There should be some way to say “tell me what category/ies this post is in, and then show me other posts in the same category/ies”. So, that’s what I’m looking for.
Thanks for helping me parse this out — hopefully someone will step in and help us both get the right answer.
-Sara
Moshu, thanks again — should I open a new discussion with my topic re-worded to get someone else to look, do you think?
I have done some searching and testing, and found that I can put this code in the sidebar:
<?php $postlist = get_posts('category=3&numberposts=50'); foreach ($postlist as $post) : ?> <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> <?php endforeach; ?>
That will list all my posts from category “3”.
What I want to do is list all my posts from the current category. Is there a query I can do to get the current category of the current post, and then put a variable in the place of the category number, so it works for each category? Thanks.
Ah well, I was too lazy to make the code and intended to find a plugin. I bumbped into this code and thought of having a look at it. Well, you’re almost there.
The bold items..the_category will call up the category name for the current post. The number 5 can be changed to the number of posts you wish to display.
Reanime, let me see if I understand correctly. You are saying that if my code read:
<?php $postlist = get_posts('category=the_category&numberposts=50'); foreach ($postlist as $post) : ?> <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> <?php endforeach; ?>
That it would work? I’ll test it, but let me know if that’s what you mean.
Thanks.
Here’s some updated code that worked for me.
<?php $postlist = get_posts('category='. get_the_category()); foreach ($postlist as $post) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>
This is exatly what I am looking for. Excellent! Thank you. I do have a question though.
I am posting recipies and I needed the child/sub cats to appear under the post. (I am running WP 2.5) The above works to a point.
Here is an example of what I am attempting.
When viewing the “Cookie Recipes” descriptive parent cat post I have the links to each cookie recipe beneath the welcoming post.
Cookies
Chocolate
Raisin
etc…
But I do NOT want the parent “Cookies” to appear.Also, when I am viewing the individual recipes I want the others listed BUT NOT the current child/sub cat being displayed. (or the parent)
So when I am in the Chocolate Cookie recipe I have the link to the other children of Cookies underneath that recipe post BUT NOT the current Chocolate Cookie permalink.
I am used to PERL a bit and using unless but, this one has got me stuck.
in the foreach loop unless (currentcat == oneintheforeach ) { <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> }
I am using this in each applicable template file. (post & archives so far)
Can anyone guide me in the right direction?
Thanks in advance….
I think I am on to something here but still having difficulty. Too old I think.
Using some kind of exclude? I have also seen filters used in plugins.
<ul> <?php wp_list_categories('exclude=4,7&title_li='); ?> </ul>
(needless to say the wp_list_categories is not relevant)
Combined along with the great post from jaiasmit?
I am constantly re-reading the documentation but my old brain is just not seeing a solution.
So…
<?php $postlist = get_posts('category='. get_the_category()); foreach ($postlist as $post) : ?> how do I 'exclude=currentpost) in here??? <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endunless; ?>
I am stuck with my limited ability. I could write up a stand alone PERL script that queries the db and stick it in an iframe but, excluding the current post and the parent cat from the permalink list would require those vars to be made available to said script. I know there is a better way. I have been reading for two days now. There is prob a one liner which will make me feel like more of an idiot.
Man I feel really bad about nearly flaming this thread but, here is what I found.
https://www.ads-software.com/extend/plugins/wordpress-23-related-posts-plugin/
This did what I needed and then some however, if I am to feel comfortable manging our wordpress site, I need to figure out how to resolve my basic coding issue above. Help would still be appreciated.
I would like to give much kudos to Denis,PaoPao
I will follow up monetarilly when possible. (Got five teenagers in the house!)
I’m struggling with this one too. I’m working on a theme that has a section for “posts in the same category as the current post, with the same tag, excluding the current post”. But I can’t figure out that last little bit! I’ve tried adding “
'p=-.'$id
” in the query, where $id is the id of the current post, but no such luck. Seems like it would make sense to work that way, but I guess not.I’ve been wanting this same feature and also couldn’t find it as a plugin. I’m no genius with PHP, but I hacked together the following code with some help from this thread. It works for me and leaves out the current post. I have it at the very top of a sidebar that appears only on single.php. It also accounts for a post belonging to multiple categories by showing the posts related to each on separately.
Let’s try that again:
I’ve been wanting this same feature and also couldn’t find it as a plugin. I’m no genius with PHP, but I hacked together the following code with some help from this thread. It works for me and leaves out the current post. I have it at the very top of a sidebar that appears only on single.php. It also accounts for a post belonging to multiple categories by showing the posts related to each on separately.
<?php $postid = $post->ID; foreach((get_the_category()) as $category) { echo "<h3>Related Posts in ".$category->cat_name." </h3>"; $postlist = get_posts('category='.$category->cat_name); foreach ($postlist as $post) : $catpostid = $post->ID; if (in_category($category->cat_name) && ($catpostid != $postid)) { ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php } endforeach; } ?>
- The topic ‘link to other posts in same category’ is closed to new replies.