I am looking to feed a custom post type onto a custom template. I basically want a few sub sections with custom (seperate) post types on the backend panel feed out onto a page, like a news, blog, testimonials kind of thing.
I think have the right idea but I believe I am going around this wrong. Please forgive me, as I do not normally work on wordpess. I basically want (let's say the news page) to feed all the posts in the custom post type "news". I have a blog page linked to a template that looks like so -
<?php
/*
Template Name:News*/
?>
<?php get_header(); ?>
<ul id="News">
<?php global $post; query_posts( 'post_type=newst&orderby=ID&order=desc' ); while (have_posts()) : the_post(); ?>
<li>
<div class="fea_del">
<h1><?php the_title(); ?></h1>
<p><?php the_field('post_content',$post->ID); ?></p>
<a <?php $p=get_permalink( $post->ID ); ?> href="<?php echo $p; ?>"class="entire_job">SEE ENTIRE ARTICLE</a>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
<?php get_footer(); ?>
I have the [display-posts] plugin in my blog page and just have [display-posts] in the body in hopes it would just feed in all the posts in blog. I've been banging my head against this for a while with no success, I don't work in wordpress much so I'm a bit in the dark here.
You have to pass the $args array to WP_Query, try:
$args = array( 'post_type' => 'news', 'order' => 'desc' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
//rest of you code
endwhile;
Related
I just started WP, and made a table with many rows arrived at WP house. But don't know how to show. basic content well shown but custom fields. learnt that a page have to be created to deal them in order to retrieve custom typed posts.
the following is from my content-movie.php under twentyfourteenchild:
/* translators: %s: Name of current post */
the_content();
// handling movie stuff starts
$p_id = $post->ID;
$ar_fields = array( 'studio','director','starring','grade');
.
.
foreach $ar_fields as $field
$some_field = get_post_custom_values($field, $p_id);
do some thing dealing $some_field...
end for
===================
In order to make a regular page to populate movie-typed custom posts, do I have to put such codes in archive-movie, page-movie single-movie etc ?
I guess, somewhere it can be dealt instead of putting same scripts in many files.
I really want someone to help me go right direction.
firstly create a custom page template by doing the following:
Copy the regular page.php and rename it to page-movies.php.
In the page header add the following php code:
/**
* Template Name: Movie Page
*/
Now the page template should be available in the backend, select the Movie Page as your page template.
Now replace the regular page query with your query, here is an example below:
<?php $args = array( 'numberposts' => 7, 'order'=> 'ASC', 'post_type' => 'movies');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="item">
<div class="item-content">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumbnails"> <a class="ajax" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'projects-thumbnail' ); ?>
</a>
<div class="copy">
<h3><a href="<?php the_permalink(); ?> ">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
This should do the trick...
The problem I have is I cannot get Wordpress to show the posts where I need to.
I have already tried using two methods the first in the reading settings by selecting a page to display the posts which did not work. The second was a getpost plugin which displays the post using short code [get_posts] it didn't display any post just the short code and the plugin is active.
I want it to appear like this http://www.completesource.co.uk/category/ironkey-id-theft/ but on this page http://beta.completesource.co.uk/it-news/
I have been searching for this for a while any help would be greatly appreciated.
Thank you
EDIT: code
<?php
if (is_page() ) {
$category = get_post_meta($posts[0]->ID, 'category', true);
}
if ($category) {
$cat = get_cat_ID($category);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => array($cat),
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $post_per_page,
'caller_get_posts' => $do_not_show_stickies
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_excerpt('Read the rest of this entry »'); ?>
Read More..
<BR><BR>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</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;
$wp_query = $temp; //reset back to original query
} // if ($category)
?>
Personally I would write a page template for the page, then create a new page and use the template. Start reading about Page Templates and go from there.
Every Wordpress theme on the market will handle the display of posts very differently. It can very by page, shortcode, template, settings, etc.
We need to know what theme you are using in order to help you. In order to help yourself, investigate how to activate the blog by checking your themes document files or the theme developer or any help forums for the theme.
<?php $query = new WP_Query('posts_per_page=1&'); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?><br />
<?php echo paginate_links( $args ) ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
Here is the query I'm using, it should display the first post title then displays the pagination links, however it only shows the post title without the pagination links. What's wrong?
The paginate_links function should go outside the loop and you should use the query variable "paged"
Please see this link http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html for more information on using this.
I haven't worked much with wordpress, what I am trying to do is have a page that displays random posts.
It would be similar to my main page where the latest posts are shown but it would display random posts every time the page is refreshed.
If the main page is at http://example.com i want my random page to be at http://example.com/random
how to do it?
The orderby argument to get_posts accepts the value rand.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
More info in the documentation: http://codex.wordpress.org/Template_Tags/get_posts#Random_posts
Enable permalinks for you wordpress installation by visiting http://example.com/wp-admin/options-permalink.php, If you would like the title of the post to be the last url segment you would select Custom Structure and set this value to %post_name%
Create a new page template in your current theme folder as described here: Creating Your Own Page Templates
Create a new page witht the title Random and select your newly created page template under Page Attributes in the pages edit screen.
Then in your page template you would do something like this as Filip suggested:
<ul>
<?php
$args = array('posts_per_page' => 5, 'orderby' => 'rand');
$rand_posts = get_posts($args);
foreach ($rand_posts as $post) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Try This ...
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
You can use this code to display random posts on your page.
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
Basically, I want to have two columns Wordpress page; one displaying video posts, and the other audio posts.
What's the easier way of doing this?
<div class="leftcol">
<?php $args = array( 'post_type' => 'video_post' ); //select only posts of type 'video_post'
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<div class="post">
<h3><?php the_title() ?></h3>
<?php the_content() ?>
</div>
<?php endwhile; rewind_posts(); ?>
</div>
<div class="rightcol">
<?php $args = array( 'post_type' => 'audio_post' ); //select only posts of type 'audio_post'
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; rewind_posts(); ?>
</div>
I'm assuming you do know the basics of editing a theme, but if not, just say so! Alternatively, you could do the same thing using 'cat' instead of 'post_type' to separate your posts by category instead of two different types. I'd recommend using categories if you don't intend on adding any extra fields to enable your audio/video functionality.
I'm not sure if I understood you well, but I would do it with Widgets using Widget Logic plugin http://wordpress.org/extend/plugins/widget-logic/
If you want to hardcode your template you may need to check the query_posts() and reset_posts() finctions: http://codex.wordpress.org/Function_Reference/query_posts
Do you know how to use WordPress Codex? Do you know how the loop works? If yes you may want to use the second way, if not try to do it with the first option.
Good luck,
gezope