In wordpress, I have custom pages and inside the pages I'm trying to call the most recent posts for a specific category.
Doing so, I added this to create the variable...
$cat = get_post_meta($post->ID, "mom_cat", true);
And in the custom field of the page, I added mom_cat = (variable #)
In my post, I am trying to show the recent posts based o the variable category # I put in the custom field. I tried this, but it didn't work...
<?php
global $post;
$myposts = get_posts('numberposts=4&category=$cat' );
foreach($myposts as $post) :
?>
Hoever that is not working. How do I add a variable to "category = " in order to display a category based on my custom field setting?
Thanks
You shouldn't be using Custom Fields to define categories when Wordpress has all of the tools for you at your disposal. What you should do is find where your custom post is being registered, and add:
'taxonomies' => array('category')
...to your argument array. This will enable you to check off the categories that your custom posts require. If you can't find where the custom post type is being registered, add this instead to your functions.php file:
add_action('init', 'add_category_to_custom');
function add_category_to_custom()
{
register_taxonomy_for_object_type('category', 'custom_post_name');
}
THEN you can reference your custom post like so:
$posts = get_posts(array('numberposts' => 4, 'category' => $cat_ID, 'post_type' => 'custom_post_name'));
UPDATE: I wouldn't advise using Pages to display Category information, but I'm sure you have your reasons. I would still avoid placing category IDs within Custom Fields, simply because if one of your category IDs change, then it can cause LOTS of problems.
What I would advise in your case is to name those particular Pages exactly the same as their matching categories. Then do something like this:
<?php
/*
IF THE PAGE HAS A MATCHING CATEGORY,
DISPLAY 5 OF THE MOST RECENT POSTS IN THAT CATEGORY
*/
if($catID = get_cat_ID(get_the_title(get_the_ID())))
{
$posts = get_posts(array('numberposts' => 5, 'category' => $catID));
foreach($posts as $post) : setup_postdata($post);
?>
<!-- POST HTML GOES HERE -->
<?php
endforeach;
}
?>
Related
I have a wordpress-based website where I'm trying to move some custom functionality that used to be just plain mysql table and some code to insert/update/show stuff from it. I'm trying to merge it in WP as custom posts so I can use filters, tags, pagination and such.
It supposed to show a few custom items inside the user profile (Ultimate Member). Like, user info, then 10 of user's top items of this kind, then on next tab 10 items of some other kind, etc.
But seems like things aren't as simple in WP, and you can't just toss things on top of each other and expect it to not overlap. >_> So when I'm trying to add a block with custom post type data, it returns only some of the data mixed with profile data mixed with nothingness.
Yeah, I get it, there's probably a profile loop and some data already inside variables, as far as I could understand from manuals. What I can't understand is how to fix it. Here's how it looks:
$args = array(
'author' => $uid,
'numberposts' => 10,
'post_type' => 'ff',
);
$ff = get_posts($args);
if($ff){
foreach($ff as $f){
setup_postdata($f);
the_content(); //shows what's needed, as well as ID, the_time() and some more
the_title(); //shows author's name instead of post title
the_tags(); //shows nothing, as well as excerpt, etc
get_the_excerpt(); //still nothing
$f->post_excerpt; //but this shows the excerpt, as well as print_r($f)
}
wp_reset_postdata();
}
Maybe someone could give a hint what am I missing? Thanks in advance!
Try using the post ID to get your data and echo it. A little more code but may work better.
// Set the global post up here
global $post;
$args = array(
'author' => $uid,
'numberposts' => 10,
'post_type' => 'ff',
);
$ff = get_posts($args);
if($ff){
foreach($ff as $f){
setup_postdata($f);
$id = $f->ID; // get post ID
$content = get_the_content($id); // get the content to echo later
$tags = get_the_tags($id); // use to get tags, these are not part of the get_posts return object
echo $content; // show the content
echo $f->post_title; // show the returned post title. can use get_the_title($id) and echo it if this does not work
// Display the tags after getting them above
foreach ($tags as $tag) {
echo $tag->name . ', ';
}
// You can get the excerpt this way too but you said your other worked okay
$excerpt = get_the_excerpt($id);
echo $excerpt;
}
wp_reset_postdata();
}
(Original) To elaborate. This sets up the global post object. Which is generally a requirement for your functions to work in the loop. Not always the case as I've found over the years, but a good practice if you're not using the post ID in the loop to get your data.
I have a custom post type of 'products' and in single-products.php I want to show all child categories of the current posts' primary category (used for navigation)
How would I do this? Have tried a few options and nothing works thus far.
Any help would be appreciated.
First you will get the category of the post using: $cat = get_the_category($post->ID);
Then you will get the children of that category using: $cat_children = get_term_children( $cat, 'category' );
Then you will loop through the children like so:
foreach($cat_children as $child) {
$term = get_term_by('id', $child, 'category');
// Display children here
}
Here are links to each of the functions referenced here:
get_the_category()
get_term_children()
get_term_by()
EDIT: You can swap out the category term in each of these functions to target custom taxonomies.
I'm trying to display posts within the WP loop and am able to successfully do so with <?php query_posts('tag_id=10'); ?>Here the loop will display all posts with the tag ID of 10, but I'd also like the loop to display posts from within a Custom Post type by the same tag.
I'm able to successfully display posts with tag_id=10 that originate from a custom post type using <?php query_posts('tag_id=10&post_type=videos'); ?>
But how can I merge the two?
I gave this a shot: <?php query_posts('tag_id=10, tag_id=10&post_type=videos'); ?>
but that had no effect.
Any ideas on this one?
You can use this
query_posts(
array(
'post_type' => array('post', 'videos'),
'tag_id' => 10
));
while (have_posts()) : the_post();
// loop code
endwhile;
wp_reset_query();
This runs an action before the posts are actually queried, thus altering the original output to your specific needs.
function tag_archive_mod( $query ) {
if ( is_tag() && $query->is_main_query() ){
$query->set('post_type',array('post','video'));
}
}
add_action('pre_get_posts', 'tag_archive_mod');
Very, very useful.
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
I need to find out how to list my content data on a page/ section when I click on a specific category link.
After a few hours I realise that help from stackoverflow is what i need!
The function remind a little of: http://www.hashbangcode.com/blog/wordpress-category-post-list-493.html
I need to list my data (title, the_content etc) from the specific category and not all categories with the titles.
While that post gives you a good start, here is the code that I would use
// Set the desired category
$category = 1;
// Make query for posts in the category
$my_query = new WP_Query();
$my_query->query(
array(
'cat' => $category,
// Does not show sticky posts; use 'caller_get_posts' if using < WP 3.1
'ignore_sticky_posts' => 1
)
);
// Make sure some posts were found.
if($my_query->have_posts())
{
// Loop through each post found.
while($my_query->have_posts())
{
// Setup the post data to use
$my_query->the_post();
global $post;
// Echo out the title; Note that no formatting has been done
the_title();
the_content();
}
}
Now, you can also get the title with either:
$title = get_the_title($post->ID);
$title = $post->post_title;
Additionally, you can get the post content with:
$content = $post->post_content;
Also, you can get the category using any of these parameters:
cat (int) - use category id.
category_name (string) - use category slug (NOT name).
category__and (array) - use category id.
category__in (array) - use category id.
category__not_in (array) - use category id.
More about the WP_Query class can be found here: http://codex.wordpress.org/Function_Reference/WP_Query
I have a WordPress site, where on the main page I list the content from more categories.
My question is, is there a plugin where I can paginate the results from a category? I mean something like $this->plugin_paginate('category_id'); or smth?
Best Regards,
If you're using the standard Wordpress loop, even with a query_posts for a category, pagination is automatic with the usual posts_nav_link. Are you trying to paginate for more than one query and more than one category on the same page?
Edit 11/20: I use this in several different places on one page to show the latest post in a category:
<?php
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
That link then goes to a category page that paginates for that category: Category Templates « WordPress Codex
I don't know how to paginate different categories on the same page. Must be possible. Maybe ask in the Wordpress forums.
This sounds like something that a simple, well-formed query_posts() call can do. I doubt that you'll even need to rely on a plugin. :)
I'm going to assume that you're familiar with the query_posts() function, so let's go ahead and use this example as a base:
// let's get the first 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
Now, to get the 11th to 20th posts from category 3 (that is, the NEXT 10 posts), we'll want to use the [offset] parameter of query_posts():
// let's get the next 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3&offset=10');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
For most purposes, that should be sufficient. However, you did mention that you plan on paginating category post lists from the main page alone? I'm assuming that you mean that you have multiple category post lists on your main page, and all these are paginated independently.
With something like that, it looks like you'll have to work a bit with Javascript to do the work for you, along with what I illustrated above.
I believe you could do something like this:
<?php
if(isset($_GET['paged'])){
$page = $_GET['paged']-1;
}else{
$page = 0;
}
$postsPerPage = 5;
$theOffset = $page*$postsPerPage;
?>
<?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>
Hope this will help you :)
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $page,
);
query_posts($args);?>
?>