display posts under specific page - wordpress

I am having many number of pages, I would like to display some specific posts under one specific page.
for example Under News page i need to display only news related posts only
how to do this?

This is a new query, won't effect the main Wordpress loop and can be used multiple times on a page. Change parameters to your category name and number of posts to show. Use in a page template or in page editor with php execution enabled.
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php the_content(); ?>
<?php endwhile; ?>

Related

Custom templates, custom post type, and single page problems

I have set up a custom post type for a few sub sections of the website I am currently working on in wordpress. I need the custom post types to feed onto a template (which I have working) but I have a few questions and non working things. The read more link is not working, as in not showing up at all even? Also when I click the title links n the posts to go to the full post, they go to the correct url but there is no content in the body. I'm thinking I need to add more in the custom template.
Here is the template code :
<?php
/* Template Name:News */
?>
<?php get_header(); ?>
<div class="decade1">
<ul id="news_list">
<?php global $post; query_posts( 'post_type=news&orderby=ID&order=desc' ); while (have_posts()) : the_post(); ?>
<li>
<div class="fea_del">
<h2><?php the_title(); ?></h2>
<p><?php the_field('post_content',$post->ID); ?></p>
<a <?php $p=get_permalink( $post->ID ); ?> href="<?php echo $p; ?>" class="entire_job">Read More</a>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<?php get_footer(); ?>
The most important issue is the posts not showing up on their individual pages. Thanks!!

Pull latest posts from a specific category

I am trying to pull latest posts from a specific category.
I am currently able to pull all latest posts and display them the way I want using the code below but I am unable to do the same thing from a specific category.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="content"><div id="circle"><div id="circle_text1"><p><?php the_time('M') ?></p></div>
<div id="circle_text2"><p><?php the_time('dS') ?></p></div></div>
<div id="text"><div id="title"><p><?php the_title(); ?></p></div>
<div id="name"><p>By <?php the_author(); ?></p></div>
<div id="blurb"><p><?php the_content('<br />Read More'); ?></p></div></div>
<div id="line_rule"><p> </p><hr /></div></div>
<?php endwhile; ?><?php else : ?><h2>Not Found</h2><?php endif; ?>
Thanks in advance
This is a basic WP query that resets itself and can be used multiple times in a template. You can add your html to it. showposts is the number of posts to show; -1 shows all posts.
<?php $my_query = new WP_Query('category_name=mycategoryname&showposts=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
if you are getting posts from database then use order by ID desc that will show latest post on the top. if ID is auto-incremented. like
select * from posts order by ID desc
I do this all the time. This will work:
Change the cat= number to whatever category ID you want to use.
Add html markup as you please and whatever other content you want to pull.
<?php query_posts('cat=4&order=ASC');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
In the example I am only grabbing the post content, but you could put in the title, meta-data, etc. The important parts are all here.
Try it out.
Add this line of code above your opening IF statement.
<?php query_posts( 'cat=1' ); ?>
And then change the 1 to match the ID of the category you are trying to display.
:)
You can do that by simply by adding this first line to your code:
<?php query_posts( 'category_name=slug_of_your_category&posts_per_page=10' ); ?>
Replace "slug_of_your_category" with the slug of your category, and "10" with the amount of posts you need.

Getting post id in WP_Query

I am trying to execute the query in WordPress to retrieve the last post from featured category, then check for its custom field and show an image stored in it.
Well, my code is not working for some reason.Can you spot any error there? Or point me in the right direction?
<?php $featured = new WP_Query('showposts=1&category_name=featured'); ?>
<?php if($featured->have_posts()) : ?>
<?php while($featured->have_posts()) : $featured->the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'image3', true); ?>"></a>
<?php endwhile; ?>
<?php endif; ?>
Many thanks!
I hope this will do what you expecting
$args['category_name']='featured'; // posts from particular category
$args['orderby']='date'; //to get latest post
$args['order']='DESC';
$args['numberposts']='1';// to get only one post from post list
$featured = new WP_Query($args);

Make WordPress homepage a post category?

I am trying to set my WordPress homepage to a category but it only allows me to set it to either the latest posts or a static page.
Is it possible to set your homepage as a post category?
I am hoping that you know about how to set static page. So first create an empty .php file and name it whatever you like and put it along the other files (index.php, arhive.php etc).
and then enter following code
<?php
/*
* Template Name: Category based Homepage
*/
?>
<?php get_header(); ?>
<div class="main">
<?php
$cat_ID = '1'; //it should be your category ID, you can get the id of the category by going to categories and edit and then in url you can find the tag_ID.
$posts_to_show = '10'; // number of posts from the category you want to show on homepage
//query_posts("cat=$cat_ID&showposts=$posts_to_show");
$category_posts = new WP_Query("cat=$cat_ID&showposts=$posts_to_show");
//if (have_posts())
if ($category_posts->have_posts())
: $first = true;
?>
<ul class="post-list">
<?php
//while (have_posts()) : the_post();
while ($category_posts->have_posts()) : $category_posts->the_post();
if ($first)
$class = "first-in-row";
else
$class = "";
$first = !$first;
?>
<!-- Start: Post -->
<li <?php post_class($class); ?>>
<?php the_post_thumbnail(); ?>
<p class="categories"><?php the_category(", "); ?></p>
<h2><?php the_title(); ?> <?php edit_post_link(__('Edit', 'your_theme_text_domain'), '', ''); ?></h2>
<p class="post-meta"><span class="date"><?php the_time(get_option('date_format')) ?></span> <?php if (comments_open()) : ?>, <span class="comments"><?php comments_popup_link(_x('0', 'comments number', 'your_theme_text_domain'), _x('1', 'comments number', 'your_theme_text_domain'), _x('%', 'comments number', 'your_theme_text_domain')); ?></span> <?php endif; ?> <span class="author"><?php the_author() ?></span></p>
<?php the_excerpt(); ?>
<p class="more"><?php _e('Read More »» ', 'your_theme_text_domain'); ?></p>
<?php if (has_tag()): ?><p class="tags"><span><?php the_tags(""); ?></span></p><?php endif; ?>
</li>
<!-- End: Post -->
<?php endwhile; ?>
</ul>
<?php else : ?>
<h2 class="center"><?php _e('Not found', 'your_theme_text_domain'); ?></h2>
<p class="center"><?php _e('Sorry, but you are looking for something that isn\'t here.', 'your_theme_text_domain'); ?></p>
<?php
endif;
//wp_reset_query();
wp_reset_postdata();
?>
</div>
<?php get_sidebar(); //optional?>
<?php get_footer(); ?>
and replace $cat_ID and $posts_to_show to your liking. And I have used both query methods adjust it to your needs.
Hope it helps somebody who is looking for similar solution.
You can create a custom template that mimics a category page using get_posts and set a page using that template to home, but it won't be perfectly dynamic in the sense that you have to hard code the category slug or ID into that query. Assuming that you don't want to change that category often, that shouldn't be an issue. Alternatively, you could use wp_safe_redirect in a template to redirect to the category page - that would be if you want the user to be put directly on the real category page, URL and all.
I'm not sure what you mean by having your home page as a category, you mean that in your home page posts that will be displayed will be only from a certain category ?
You only need to perform a WP_Query before the loop;
$query = new WP_Query("cat=10, paged=".get_query_var('paged'));
Then use use the WP_Query object to perform the loop;
if($the_query->have_posts()):
while($the_query->have_posts()):
the_title();
the_content();
//Use all the loop function normally
endwhile;
endif;
The paged parameter is used to determine in which page you are, if you need paginantion.
Instead of using the category id, it is good to retrieve the id by the slug.
$home = get_category_by_slug('home-category-slug');
Then your query will be like this
$the_query = new WP_Query("cat=".$home->cat_ID.", paged=".get_query_var('paged'));
Yes this is possible Go to dashboard>>Setting>>Reading>>Static page Choose page from drop down and SAVE. On that page you can create your own stuff...

wordpress featured posts

i'm trying to create a portfolio website using wordpress,
each post has view costum fields, one of which is called type - with the value of "featured" or "not-featured"
now when user clicks on the post title - they go the the single.php to see the entire post, here i would love to display all featured thumbnails
i tried this
<?php while ( have_posts() ) : the_post() ?>
<?php if(get_post_meta($post->ID, 'type', true) == "featured") {; ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img src="<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
</a></h2>
<?php }; ?>
<div class="entry-content">
</div><!– .entry-content –>
<?php endwhile; ?>
(THIS CODE IS SIMILAR TO THE CODE I USE AT INDEX.PHP AND THERE IT DOES WORK, HERE AT SINGLE.PHP IT DOES NOT WORK)
but this does not display all the thumbnails (only the current posts' thumbnail (is it's a feature post))
this is my first attempt of trying to create a theme from blank so i'm not sure what the error could be
thanks for your help
The code in your question only loops through the posts returned by the query made for the current view, in the case of a single post view that is one post. You want to perform a new query to retrieve all the posts that have the required meta value:
<?php
query_posts(array("meta_key" => "type", "meta_value" => "featured"));
if (have_posts()) : while (have_posts()) : the_post();
?>
<!-- Display thumbnails -->
<?php endwhile; endif; ?>

Resources