WordPress: Display Other Posts From Current Category - php

I have a function going that displays all posts under the same custom taxonomy called "issue". I need to adjust it so that it further narrows it down to also only display posts under the same category.
I took a look at the WordPress get_the_category() function but didn't have much luck with that.
Here is the code:
<?php
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid");
foreach ($postslist as $post) :
setup_postdata($post); ?>
<div class="sidebar-box">
<div class="sidebar-left">
<p><?php the_title(); ?></p>
<p><?php the_date(); ?></p>
</div>
<div class="sidebar-right">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
</div>
</div>
<?php endforeach; ?>
This will properly show the current category id:
<?php
$category = get_the_category();
echo $category[0]->cat_id;
?>
So I tried editing the first batch of code to only show posts within the current category id but it still returns everything:
$category = get_the_category();
$categoryid = $category[0]->cat_id;
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid&category=$categoryid");
foreach ($postslist as $post) :
setup_postdata($post); ?>
This is the get_the_category function reference: http://codex.wordpress.org/Function_Reference/get_the_category
Any help would be greatly appreciated.
Thanks,
Wade

get_the_term_list() returns an html string, not ID's of related categories. So when you pass $issueid to get_posts(), you are including an html string, not an integer. I believe the reason you are getting all posts returned is because WP is ignoring that query var because it isn't what its expecting.
You want to use get_posts() and include the ID for 'issue' to get all posts assigned the category of 'issue'.
You want to use get_the_category() to get all categories related to a post.
Can you clarify if you want to show all posts under the same categories as the current post which is under the category 'issue'? Do you want to list the related posts right after the current post, or do you want to display ALL the related posts to ALL the 'issue' posts in the sidebar?

Related

php generate links to all wordpress categories

Im having a little problem. I want to display all categories connected to a wordpress post.
I found several codes and plugins, but all of them have problems. Some dont display correct anchor text, others dont link to correct url.
So i took the working parts from different places and put them together to the code below, and its working.
The problem is that it does not generate links to ALL connected categories, it only show link to 1 single category (first one that was connected to the post)
what is wrong with the code ? Why will it not show links to all connected categories. I have been working on it for 2 days now, and i give up.
<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<?php
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>"title=”<?php echo $category_name ?>” ><?php echo $category_name ?></a>
Please try this code
<?php
$category_args_query = array(
'orderby' => 'name',
'parent' => 0,
'hierarchical' => 1,
'number'=> 10
);
$categories = get_categories( $category_args_query );?>
<ul>
<?php
foreach ( $categories as $category )
{ ?>
<li><?php echo $category->name; ?> </li>
<?php
}
?>
</ul>
If you want to show the category that is related with the posts on post details page and archive page add this code in your child theme functions.php and call the function where you want to display it.
function postsCategories()
{
$categories_list = get_the_category_list( esc_html__( ', ', 'your-text-domain' ) );
$allowed_tags_before_after=array('span' => array('class'=>array()),'i'=>array('class'=>array()),'a'=>array('class'=>array(),'href'=>array(),'rel'=>array()));
if ( $categories_list )
{
printf(wp_kses(sprintf(__('<span class="blogmeta cat-links"> <i class="fa fa-folder-open"></i> %1$s </span>','your-text-domain'), $categories_list ),$allowed_tags_before_after));
}
}
Call in the template : <?php printf(__('%s','your-text-domain'),postsCategories()); ?>
Actually just <?php the_category(', ') ?> should be enough (within the WP while loop). The , determines what should go in-between, if there is more than one category.

Getting first paragraph of most recent post based on category ID

It's been a while since I've worked with PHP and WordPress, so I am a bit rusty. What I want to do is to display the opening paragraph for the most recent post under a category for a WordPress site. Based on some research I did, I've compiled this piece of code:
<?php
$category_id = get_cat_ID('Downtown News');
$post = get_posts( $category_id );
if( !empty( $post ) ) {
setup_postdata( $post );
?><?php the_title(); ?>
<?php the_excerpt();
} ?>
<?php
$post = $wp_query->post;
setup_postdata( $post );
?>
I have code to get the category ID I want, and I have the code to display the first paragraph of the most recent article. However, the code does not seem to work. What is displayed is the first paragraph under an "uncategorized" category of postings, which is not what I want. How can I fix what I have so that it gets the correct category?
You have started at the right place: get_posts, however you don't have the correct parameters. So try the following out:
<?php
$args = array(
'category' => 'CAT_ID_1,CAT_ID_2',
'orderby' => 'post_date',
'order' => 'DESC');
$posts_array = get_posts( $args );
?>
From the function reference we know that:
The category parameter needs to be the ID of the category, and not the
category name
which means that you can have one or more category IDs (comma separated).
A list of all parameters can be found here.
I had the similar issue with yours, maybe this one will work for you
notice the $category_id gets pulled from get_cat_ID() function, this tells me you do not know the cat_id, you can go to the category where you created the downtown news, move your mouse over it to reveal the url address which it will tell you the category id, find that number and replace the line:
query_posts('cat='.$category_id.'&posts_per_page=1');
(using 99 as an example)
query_posts('cat=99&posts_per_page=1');
<?php
global $post;
$category_id = get_cat_ID('Downtown News');
query_posts('cat='.$category_id.'&posts_per_page=1');
if ( have_posts() ) {
?>
<?php the_title(); ?>
<?php the_excerpt();
}
wp_reset_query();
?>

Display content from custom post type if match to post category

I solved the problem of displaying from a custom post type in a post with this solution, however, I want to filter even more and only display the posts from custom post type that match the main post's category (or to be more precise the slug, but there's no difference in solution).
I get the slug of the main post by using this:
$category_main = get_the_category();
$cat_slug = $category_main[0]->slug;
echo $cat_slug; // This is just to see if I got the right output
I get the slug from the custom post type on the same way, but it's within a loop that loops through the custom post types.
$category_course = get_the_category();
$cat_slug_course = $category_course[0]->slug;
echo $cat_slug_course;
So, what I want now, is to only display the posts from the custom type that match the slug of the original post.
In pseudo-code this would be something like:
If $cat_slug_course is equal to $cat_slug, display all custom posts with slug $cat_slug_course and none other
I feel this is something not-Wordpress-specific but PHP, which is why I post it here.
This is the loop used to display the custom type posts.
$args = array( 'post_type' => 'Course', 'posts_per_page' => 2 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$category_course = get_the_category();
$cat_slug_course = $category_course[0]->slug;
echo $cat_slug_course; // This is just to see if I got the right output
echo '<br />';
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile; ?>
Okay, it was more simple than expected.
<?php if ($cat_slug_course == $cat_slug): ?>
<div class="landing_title">
<?php the_title(); ?>
</div>
<?php the_content();?>
</div>
<?php endif; ?>
Solves it. Did not expect that, but it does.

How to get just one category and not all of them

Here is the problem:
I've been using this code to get other posts to display in the end of the page (as a list), and those posts should be in the same category as the post that a visitor is currently reading. It works fine, but the problem that occurs is when the post is assigned to multiple categories, then I have multiple listings which is not needed.
How can I restrict this , to just one category (by name, or by id or the first one), so that the loop shows me posts from just one category when post with multiple categories is displayed?
Many thx.
<?php global $post;
$categories = get_the_category();
foreach ($categories as $category) :
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$posts = get_posts('numberposts=10&category='. $category->term_id);
foreach($posts as $post) :
?>
<li>
<?php
$title = get_the_title($ID);
$link = get_permalink();
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
the_post_thumbnail('thumb-232');
echo '<div id="excerptcu">';
echo excerpt(25);
echo '</div>';
?>
<p class="more-link-wrapper2"><?php _e( 'Opširnije »', 'fearless' ); ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
You can add the category id to get_posts just as in your code
Instead of looping the categories try like below: here 3 is the category id
$posts = get_posts('category=3&numberposts=10');
Also if you want to retrieve the category id of specific category by name you can use
get_cat_ID( $cat_name )
Perhaps you could just take the first category in your foreach loop instead of iterating through all of them? You can do this by setting a variable like
$first = true;
Before your loop. Then, set your foreach loop in an "if" that checks to see if $first == false. If so, do the loop. In your loop, do your stuff and set $first = true and it will only iterate once.

Wordpress post count for category including trashed

I've got this working nicely but I need to do something similar on another template. This code lists all the categories and shows how many posts are contained in each:
<?php
$categories = get_categories('exclude=1&order=DESC');
foreach ($categories as $cat) { ?>
<div class="cats">
<h2><?php echo $cat->cat_name; ?></h2>
<p>There are <?php echo $cat->category_count; ?> posts in this category.</p>
</div>
<?php }
?>
Now what I need to do is the same thing but the numbers output need to also include the posts that have been trashed.
Taking just one category as an example - let's call it 'Phones', let's say it has 5 published posts and 3 trashed posts, then the number returned would need to be 8.
I hope I explained that properly.
Thanks!
It is not that hard, however, you have to go about it in a slightly different way.
In foreach you should call either get_posts or WP_Query, to grab all the posts in a particular category with customized parameter post_status; then dispaly count of it.
In WP_Query you might use
['post_status' => array('publish', 'trash', 'inherit', ...)]
get_posts as
('post_status=any&categor=1&...')
EDIT
Here is a way, how to accomplish that.
<?php
$categories = get_categories('exclude=1&order=DESC');
foreach ($categories as $cat) {
$posts = new WP_Query( array('post_type' => 'post', 'post_status' => array('trash', 'publish'), 'cat' => $cat->cat_ID));?>
<div class="cats">
<h2><?php echo $cat->cat_name; ?></h2>
<p>There are <?php echo $posts->post_count; ?> posts in this category.</p>
</div>
<?php } ?>
$count = $wpdb -> get_results("SELECT count(*) as count
FROM `wp_posts`, `wp_term_relationships`
WHERE id=object_id AND term_taxonomy_id=$cat_id AND post_type='post' AND
post_status='publish'");
$count = $count[0] -> count;

Categories