Hi am using get_posts to grab all posts tagged as 'news' and display them on a given page. I am using the_date() to get the date but what is weird is that the first post shows no date, whereas all posts after this display the date fine. Also I have used this identical code to show posts tagged as 'blogs' on another page, yet they work fine.
Here is the page:
http://appshare.nsdesign7.net/news/
Also here is the other page the same code is used on but works fine:
http://appshare.nsdesign7.net/blog/
<? $pageTitle = wp_title('',false,'');
if ($pageTitle == " News") { ?>
<?php $appsharenewspage = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby'=> 'title', 'category' => 3 );
$postslist = get_posts( $appsharenewspage ); foreach ($postslist as $post) : setup_postdata($post); ?>
<article class="newsstyle">
<span class="imagestyle"><?php the_post_thumbnail(array(120,120)); ?> </span>
<h3><?php the_title(); ?></h3>
<span class="date"><?php the_date(); ?></span>
<?php the_excerpt(); ?>
<div class="clear"></div>
</article>
<?php endforeach; ?>
<?php } ?>
Try using:
echo get_the_date();
Read Special Note on: http://codex.wordpress.org/Template_Tags/the_date
the_date() is restricted to only show the date once per day. So if you have more then one post from the same day, it will only show on one of those posts.
Use the_time instead.
Try using <?php echo get_the_date(); ?> instead.
the_date displays date if the post is published on the same day only.
Reference: http://codex.wordpress.org/Function_Reference/get_the_date
The date only occurs once per day. ie for 2 posts on the same day one will be without the date. It is a WP function. To get what you want replace with the php function get time. try to use the_time() instead of the_date()
Related
I have initialized WP_Query object and using that object to show post information. But, when I go the permalink for any post, it showing the home page rather than that post page. Here is my index.php code:
<?php
$myWpQuery = new WP_Query(array( 'author_name' => 'me' ));
if($myWpQuery->have_posts()){
while ($myWpQuery->have_posts()) {
$myWpQuery->the_post();
?>
<?php the_title(); ?><br />
<?php
}
}
?>
I have three post with the author name "me". When I am loading the home page (http://localhost/wordpress/) its showing the title of those three posts inside proper anchor tag. But when I am clicking on the title, It's taking me to the post page (http://localhost/wordpress/hello-world/). Problem is here. This post page is also showing those three title as home page. But I expected only the title of the post that I clicked on.
But when I am using the simple following code it's working properly.
<?php
if(have_posts()){
while (have_posts()) {
the_post();
?>
<?php the_title(); ?><br />
<?php
}
}
?>
What's happening after initializing the WP_Query object. Could anyone explain it please.
Because you use the_post() in your query, you need to reset after to restore the global $post variable of the main query loop. The right way to do this when using WP_Query() is to call wp_reset_postdata() after your custom loop like this:
<?php
$myWpQuery = new WP_Query(array( 'author_name' => 'me' ));
if($myWpQuery->have_posts()){
while ($myWpQuery->have_posts()) {
$myWpQuery->the_post();
?>
<?php the_title(); ?><br />
<?php
}
// Restore original Post Data
wp_reset_postdata();
} else {
// No posts found
}
I'm not sure if this is causing your problem, but it's definitely something to fix. See https://codex.wordpress.org/Function_Reference/wp_reset_postdata.
I think you are missing a data setup. Take a look at the example below (taken from wordpress.org), that how your query should look like:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
https://codex.wordpress.org/Function_Reference/setup_postdata - here look at Example 1
https://wordpress.stackexchange.com/questions/99597/what-does-setup-postdata-post-do - information on what setup_postdata() function does
Without setting up postdata, your loop may store data from the previous iteration. The same would work with new WP_Query instead of get_posts().
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 am trying to run a loop that queries only a specific category from my wordpress. this is what i have but it shows all of my post instead of just my post under the category of 'testimonials'
<div class="row">
<?php if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ('cat=$cat&showposts=5');
if ($posts) {
foreach ($posts as $post):
setup_postdata($post); ?>
<div class="col-sm-12">
<div class="box" data-toggle="modal" data-target="#myModal1">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
</div>
<?php endforeach;
}
}?>
</div>
Please any help would be great,
thnx
Thats because the arguments sent to get_posts isnt correct. Try:
$posts = get_posts(array(
'category'=>$cat,
'posts_per_page'=>5, //This changes how many posts are returned
'offset' => 0, //This changes where it search starts from
);
Have a look at the documentation here
There is also a better way to get the post category:
$cat = get_the_category();
You dont have to pass the post ID to it because it will default to the current $post->ID.
You can checkout that documentation here
Its also probably a bad idea to use $post in your foreach loop as that will overwrite the global $post variable. You may want to use something like $curPost
It seems, that standard WP loop works smoothly, when I need to categorize posts. When used in category.php it displays only posts added to a certain category, for example: I have two categories, movies and music, and i have posts assigned to each of the cetgory, and also I have posts assigned to both of the categories. If so, posts assigned to both of the categories are displayed in both categories perfectly, while posts assigned to one category are displayed only on one category page. I tried to sort them by date or id, but using query_posts doesn't work. It causes to display all posts on all category pages apart form they're groupped by certain category. Is there a way to sort them in different way?
edit:
it's the standard loop, where everything works perfectly. as I written above, while having 'movies' category page, posts assigned only to 'movies' category are displayed on that page, and it's like that with every category page. But it's sorted, I think, by date and descending.
<div class="category_info"><?php echo category_description(); ?></div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="categorymain">
<h4 class="posttitle"><?php the_title(); ?></h4>
<?php echo intro_text(250); ?>
read more ยป
</div><!--end categorymain-->
<?php endwhile; endif; ?>
but, when I wrap everything with
<?php query_posts('order=ASC'); ?>
//...above code here
<?php wp_reset_query();?>
everything is ruined. On 'movies' category page posts form 'movies' category are displayed, but also posts from every other category, not related to 'movies', and this happens on every category page. I hope I made it clear :) Oh, and by the way, everything is coded in category.php.
greets to everyone:)
edit
I foud a seamless solution, here it is:
<?php global $wp_query;
$args = array_merge( $wp_query->query, array( 'category__in' => array(get_query_var('cat')), 'order' => 'DESC', 'orderby' => 'title' ));
query_posts( $args );
?>
$categories = get_the_category();
query_posts('order=ASC&cat='.$categories[0]); ?>
//...above code here
<?php wp_reset_query();?>
I recommend you to read this:
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
And have a look on get the category function: http://codex.wordpress.org/Function_Reference/get_the_category
<?php if (have_posts()) : ?>
<?php query_posts('cat=1&posts_per_page='.get_option('posts_per_page')); ?>
I try to create a theme. In that theme I have create a custom post type and I quering the WordPress by using the wp_query to get the posts from that post type with the code that following :
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 18
);
$projects = new WP_Query($args);
while($projects->have_posts())
{
$projects->the_post();
?>
<h3><?php the_title(); ?></h3>
<span><?php the_date(); ?></span>
<?php
}
wp_reset_postdata();
the problem is, that while I get the title for all of my posts, I do not get the date for all of my posts. Some posts have the date, other they don't
Any idea for that issue ?
Replace the_date() with the_time() and specify a date format like so:
the_time('l, F j, Y');
Check out the codex article on formatting your date.