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.
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...
I have this code which works okay for what I wanted but I will probably need to put a condition if there's only 2 posts then wrap it in a <div class="large-6"> then if there's 3 posts then wrap it in large-4.
Just a little confused how to add a condition statement.
<?php
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'showposts' => '3', 'offset' => '1' ) );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="large-4 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
To Pieter:
something like this? if so, that didn't work though unless I'm doing it totally wrong and noobishly.
<?php
$query = new WP_Query(array(
'posts_per_page' => '3',
'post_type' => 'portfolio',
'offset' => '1'
));
while ($query->have_posts()): $query->the_post(); ?>
<?php if(!isset ($query->posts[2])){ ?>
<div class="large-6 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php } else {?>
<div class="large-4 columns"> </div>
<?php }?>
<?php endwhile; ?>
This is just a theory but should work. Here is the idea:
$loop->posts holds an array with all the posts with all their resepective postdata. So, with that in mind, it should be easy to determine whether you have one, two or three posts in that array by simply checking if a certain array key exists
So you basically needs to check if you have a third post, so you can try something like this
if( !isset( $loop->posts[2] ) ) {
//add div if less than 3 posts
}else{
//add div if you have 3 posts
}
EDIT
Just a note. The OP has changed the query variable from $loop to $query in his original code. $loop should be changed accordingly
You can put conditionals like
<?php if(condition) : ?>
code to execute if condition == true
<?php elseif(condition2) :?> // repeat as many times as necessary
...
<?php else :?>
final else if needed, you can just end the if statement if it's only one condition, if you have else if else this is needed
<?php endif; ?>
That's basically it...
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 am new to wordpress. I am using my blog category to show all the posts as static pages. Every thing is fine except the pagination. When i googled i found that it's a known bug in wordpress.Here is my page.php code:
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
I am using list_category_post plugin for pagination but it not working with static pages. Please help me ?
When I want to show posts on the static page, I generate the query myself.
I add something like this before while loop:
<?
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'posts_per_page' => 5,
'paged' => $paged
);
$wp_query = new WP_Query($args);
?>
after the loop I would have navigation links:
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link(); ?></div>
<div class="nav-next"><?php previous_posts_link(); ?></div>
</div><!-- #nav-below -->
and after that I would add wp_reset_query() to reset the original query, just in case some other code needs it.
<?php wp_reset_query(); ?>
hope you guys can help me.
I have two very simple custom loops from one category: one sticky, one non-sticky. I want the sticky posts to only display on the first page, and not on the rest of the pages when I paginate. There must also be exactly 8 post (including the Sticky posts on page 1) on every page. All this is working fine with the code I got below.
The problem is this: Page one: 1 sticky & posts #15-9, pages two: post #7-1. WP skips one post (#8) on the second page because of the sticky post. Anyone got a solution for this? I would appreciate the help very much.
<!-- Sticky -->
<?php if ( $paged != True ): ?>
<?php $wp_query = new WP_Query(array('post__in' => get_option('sticky_posts'), 'category_name' => Work)); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop', 'index' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php endif ?>
<?php
$postcount = $wp_query->post_count;
$postnumber = 8;
if ( $paged != True ){ $postnumber = $postnumber - $postcount; }
?>
<!-- Non-Sticky -->
<?php $wp_query = new WP_Query(array('post__not_in' => get_option('sticky_posts'), 'category_name' => Work, 'posts_per_page' => $postnumber, 'paged' => $paged)); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop', 'index' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
take a look at my answer to a similar question here:
Can post_nav_link navigation work with WordPress custom page templates?
the key is offset