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; ?>
Related
I have my own custom WordPress theme and I am building out the 'Latest News' section at the moment. I have a custom field for my page header image and alt tag for each page on my site, I am using the following code to display those custom fields on each page:
<section class="page-heading">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endwhile; endif; ?>
</section>
Which works fine. But on my 'Latest News' page all the custom fields from all the news stories (posts) are being displayed, so for example if I have 3 post displayed per page then I am getting 3 images and 3 alt tag displayed.
In my settings I have my 'Posts Page' set to 'Latest News'.
Is there a way I can only have 1 image and 1 alt tag displayed and not all 3?
Thanks.
EDIT
I've added an image to better explain... The images that span the screen are from the news stories and not the custom field for the 'Latest News' page.
I don't know if I understand your question, but you want your first post show the image and the rest not?
I guess you have to write something like this:
<section class="page-heading">
<php $show_first_image = true; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($show_first_image): $show_first_image = false; ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endif; ?>
<?php endwhile; endif; ?>
I think I understand your problem, your latest news is also a Page (post type), which has the header image.
Your index file/archive is like this:
<?php
get_header();
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
the problem is, got its items loaded before the get_header(), where your header image will be showed.
What you do is something like this:
<?php
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$page = get_query_var( 'pagename' );
$wp_query = new WP_Query( array('post_type' => 'page', 'pagename' => $page ) );
get_header(); // in the get_header() you will probably have the <section class="page-heading">
$wp_query = null;
$wp_query = $original_query;
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
I hope this is your answer to your question.
I got around this by creating a new template for my news page and using WP_Query inside that template to bring in the latest news stories. I also unset 'Posts Page' in my reading settings as that's what was causing all the issues.
I'm not sure if that is the most efficient way of doing things but it worked for me.
VVC, I appreciate your help and effort.
<!-- WP Query Arguments -->
<?php
$args = array(
'page_id' => '103'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
<!-- WP Query Arguments -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '5'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="news-story">
<div class="news-heading">
<h5><?php the_title(); ?></h5>
<p class="publish-date"><strong>Posted on:</strong> <?php the_time('j M, Y'); ?></p>
</div>
<?php the_excerpt(); ?>
<p>Full Story <i class="fa fa-angle-double-right"></i></p>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
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
I've created a custom post type to do hand-crafted excerpts from my blog on my portfolio site. I've got a content window, a link, and a featured image for the post type, which I have called blog.
The issue is that, no matter what I try, the posts are displayed oldest to newest, whereas I would like to display the newest first. Here's the query_posts() call:
<?php query_posts( 'post_type=blog&order=ASC'); ?>
But I've also tried more elaborate queries such as:
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC')); ?>
My complete template file looks like:
`
">
<div class="sliderContent">
<!--first loop-->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('Read More »', THEMENAME)); ?>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
<!--second loop, displays custom post type-->
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC') ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
</div>
</div>
<!-- content end -->
<?php } ?>
`
So I'm displaying the content from the page that this template is applied, then I'm displaying my custom post type.
Thanks for the help, I'm stumped!
Your query is in Ascending order. Ascending order IS oldest to newest. You want DESCENDING order if you want newest to oldest. Also, you should avoid using query_posts if at all possible, as it modifies the default Wordpress loop.
Your second query isn't that much more elaborate than the first. The only difference is you're using an array rather than a string to define the query parameters (which an array is arguably the correct way to go about it), and you're setting the orderby parameter.
Lastly, the default order is by date in descending order (newest to oldest) so you theoretically don't even NEED to define order and orderby parameters.
Try this:
<!--second loop, displays custom post type-->
<?php
$args = array('post_type' => 'blog', 'orderby'=>'date','order'=>'DESC');
/*Consider changing to: $args = array('post_type' => 'blog');*/
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php
endif;
wp_reset_postdata();
?>
</div>
Well, as majorano84 alluded to, after further reading query_posts() is not the right function to use at all (because I guess it makes more work for the server?)
Instead, I used get_posts(), and that displays the posts in the preferred order without any further effort on my part:
<?php
$args = array( 'post_type' => 'blog' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endforeach; ?>
`
So I'm going to call that the answer, because it solves the problem I was having. Thanks!
So I have a custom template that display posts from a particular category (depending on the get_post_meta('Category')).
The page works flawlessly for displaying posts from a 'News' Category. However, using the exact same code (minus how its displayed), it has problems with the Pagination for posts from the Calendar Category. Basically, the first page of Calendar posts display correctly, and then when you hit 'next page', the url updates to page/2, but the same posts are on the page.
Here's the code:
<?php $category = get_post_meta($post->ID, 'Category', true); ?>
<?php $cat = get_cat_ID($category); ?>
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1; ?>
<?php $args = array(
'cat' => $cat,
'paged' => $paged
);
?>
<?php query_posts($args); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- If its a Calendar page -->
<?php if ($cat == 1): ?>
<div class='entry'>
<!-- List all Calendar info and Custom Fields -->
<ul>
<li><h3><?php the_title(); ?></h3></li>
<li><?php the_content(); ?></li>
<!-- ...And displays other data, etc..... -->
</ul>
</div>
<?php else: ?>
<div class='entry'>
<ul>
<li><?php the_title(); ?></a></h3></li>
<li><?php the_time('F jS, Y'); ?></li>
<li><?php the_content(); ?></li>
<!-- And display other data, etc .... -->
</ul>
</div>
<?php endif; ?>
<?php endwhile; ?>
<!-- Posts Nav Links -->
<?php posts_nav_link(' | ', '« Newer Entries', 'Older Entries »'); ?>
<?php endif; ?>
More than likely this has to do with how you are setting the paged parameter for query_posts() in a custom template.
Read more about this here: http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html
In a nutshell you may need to use get_query_var('paged').
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; ?>