I need a normal loop to show users (with custom meta fields etc.) so i have this code that shows a loop. But i need to show users that have no posts, too.
<?php
$authors=get_users();
$i=0;
//get all users list
foreach($authors as $author){
$authorList[$i]['id']=$author->data->ID;
$authorList[$i]['name']=$author->data->display_name;
$i++;
}
?>
<ul>
<?php
foreach($authorList as $author){
$args=array(
'showposts'=>1,
'author'=>$author['id'],
'caller_get_posts'=>1
);
$query = new WP_Query($args);
if($query->have_posts() ) {
while ($query->have_posts()){
$query->the_post();
?>
<li>
<h2><?php echo $author['name']; ?></h2>
<?php echo get_the_title(); ?>
</li>
<?php
}
wp_reset_postdata();
}
}
?>
</ul>
I'm doing it now with "WP_User_Query", it works way better. example: http://blog.shaunscovil.com/post/55272793162/the-difference-between-get-user-meta-and-get-userdata
Related
Client's wordpress is pretty customized and has had tons of stuff gutted and only functions can really be used. Any idea how to customize this:
<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
to only display a certain category?
<?php
$catquery = new WP_Query( 'cat=1&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><?php the_title(); ?></h3></li>
</ul>
<?php endwhile; ?>
i'm trying to build a navigation for a website, and i'm struggling with it. I'm trying to make a foldable navigation that shows only categories at first, when clicked, they link to the category, and show current category posts only.
I came this far:
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
The problem with this is: when i fill in the query so that it only takes the current cat, it displays current cat posts on both my categories.
this is what i want for the nav actually:
graphic design
Other Projects
when clicked on graphic design:
graphic design
Project 1
Project 2
Other Projects
when clicking on Other projects:
graphic design
Other Projects
Project 1
Project 2
so basically:
- when clicking from Index page to a category, only that category should expand
- when clicking the other category, the current category changes, so the previous category collapses and the other one expands.
and a bonus: is it possible, that when on a single post, there expands another level of info? for example, a few custom fields per post. like this:
graphic design
Other Projects
Project 1
custom field 1
custom field 2
…
Project 2
thank you very much
Try this code
<?php $article_categories = get_categories(array(
'child_of' => get_category_by_slug('graphic design')->term_id
));
$talentChildren = get_categories(array('child_of' => get_category_by_slug('Project 1')->term_id));
?>
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<div class="post-list">
<?php foreach($talentChildren as $talent): ?>
<?php
$talentSubChildren = new WP_Query();
$talentSubChildren->query(array('category_name' => $talent->slug));
?>
<h2><?php echo $talent->name; ?></h2>
<ul>
<?php while ($talentSubChildren->have_posts()) : $talentSubChildren->the_post(); ?>
<li>
<?php talent_thumbnail(); ?>
<h4>
<?php the_title(); ?>
</h4>
<p><?php the_excerpt(); ?></p>
read on »
</li>
<?php endwhile; ?>
</ul>
<?php endforeach; ?>
<?php if($wp_query->max_num_pages!=1):?>
<div class="pagination">
<?php previous_posts_link('« prev') ?>
<span class="current"><?php echo $wp_query->query['paged']; ?></span>
of <span class="total"><?php echo $wp_query->max_num_pages; ?></span>
<?php next_posts_link('next »') ?>
</div><!-- .pagination -->
<?php endif; ?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
Ive gotten a bit further, but i need some help now.
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id"); ?>
<?php if (in_category($cat_id)) { ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } else { ?>
<?php } ?>
<?php } // done the foreach statement ?>
So this is working now: it lists the posts per category, if in the current category, else it doesnt show anything.
now what i still want: i want to add something extra within the loop IF i'm on a single page. I have this code:
<?php wp_reset_query(); ?>
<?php if (is_single('84')) { ?>
Yes
<?php } else { ?>
no
<?php } ?>
But that would mean i have to break a query in the middle of a loop. and the is_single thing does not work inside a loop / without the query reset.
I want it looking like this with above code:
Graphic Design
project 1 (for example id=84)
Yes
project 2 (for example id=101)
No
thanks
First of all it's supposed to be used in the single page. After the single post I want to display the categories that the post belongs to.
The basic code for this is <?php the_category(' | '); ?> which outputs a simple link. Then we have <?php echo get_the_category_list(); ?> which outputs a more specific code (http://codex.wordpress.org/Function_Reference/get_the_category_list):
<ul class="post-categories">
<li>
Business
</li>
</ul>
However I need to decompose this code. For example, I want the <a> tag to be before the <li> tag. I've got a code that does what I want, but it's supposed to display all the categories available in my page, which is:
<?php
$categories = get_categories();
foreach($categories as $category)
{
?>
<li><?php echo $category->name ?> <span class="lower">(<?php echo $category->count ?>)</span></li>
<?php
}
?>
Any idea how I can make this work?
Thanks!
You can use get_the_category() its return you category objects that you can loop and do with them what you want.
$category = get_the_category( /* $post_id */ );
print_r($category);
foreach($category as $cat) {
?>
<?php echo $cat->name; ?>
<?php
}
<?php
$category = get_the_category($post_id);
foreach($category as $cat)
{
?>
<li><?php echo $cat->name ?></li>
<?php
}
?>
I am using the code below to attempt to display a list of tags associated with posts in the category 'html'
<ul>
<?php
query_posts('category_name=bikes');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
?>
</ul>
I am not seeing any results when i run it though, I have checked and there are lots of tags associated with the posts in the category.
Can anyone help?
You'll have to remove the $posttags = since you don't want to assign a variable but output it
<ul>
<?php
query_posts('category_name=bikes');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
?>
</ul>
A better way to get the results you're looking for would be not to use query_posts at all. Rather, use a new query to add to your loop. If my category were named photography, I would use this:
<ul>
<?php $photographyTags = new WP_Query(array('category_name' => 'photography')); ?>
<?php if($photographyTags->have_posts()) : while($photographyTags->have_posts()) : $photographyTags->the_post(); ?>
<?php
if( get_the_tag_list() ){
echo get_the_tag_list('<li>','</li><li>','</li>');
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
</ul>
What is the syntax for printing the sequence of category name and its follow up posts in my new template in a WordPress site? I have tried a lot via Google but all are not working properly.
There are many ways to do this, but here is a simple one (you will have to improve it with the category and posts links at least):
<?php $categories= get_categories();
if( !empty($categories) ):
?>
<ul>
<?php foreach ($categories as $category): ?>
<li>
<?=$category->cat_name?>
<?php $posts = get_posts($category->cat_id);
if( !empty($posts) ):
?>
<ul>
<?php foreach( $posts as $post ): ?>
<li><?= $post->post_title; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>