<div class="page-content">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php the_title(); ?>
<br />
<?php
the_content();
echo get_the_date('l,d');
endwhile;
endif;
?>
</div>
This example is not repeating the date for each post. I am using the code in a page template, namely services-template.php and I have read the note that states get_the_date() will repeat the date for each post. Can someone help me figure out what I am doing wrong?
I could not make the original code work, I do not know all the internal workings of Wordpress but my guess is that what ever variable holds post data (most likely $post) was empty. The following code did what I was looking to do. Again I am using this code on a custom page. services-template.php.
$args = array( 'category' => '4' );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li><h2>' . get_the_title() . '</h2></li>';
echo '<li>' . get_the_date() . '</li>';
echo '<li>' . get_the_excerpt() . '</li>';
the_tags();
echo '<br /><br />';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
;
?>
Related
Okay, I am trying run the wordpress post loop to print out the post titles and content using the_title() and the_content(). But It is not printing out the posts on the page?
Any ideas?
index.php
<div class="blog-container">
<!-- The POST LOOP -->
<?php
if(have_posts()) :
while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else :
echo '<p>No content found</p>';
endif;
?>
</div>
EDIT Okay, I'm getting ONLY THE content printed out not the post titles, here is an image: http://imgur.com/a/DiBqG.
EDIT 2 Inside wordpress admin post should look like: http://imgur.com/a/1DQAp
You don't define the query and use the Wordpress build in functions in the wrong way. Here is the correction.
<?php
// The Query
$the_query = new WP_Query( );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
add below code for fetch post data
<?php
$post_args=array(
'type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'ASC',
);
$post_my_query = null;
$post_my_query = new WP_Query($post_args);
if( $post_my_query->have_posts() )
{
while ($post_my_query->have_posts()) : $post_my_query->the_post();
?>
<h2><?php echo get_the_title( $post_my_query->ID );?></h2>
<?php
echo get_the_content( $post_my_query->ID );
endwhile;
}
wp_reset_query($post_my_query);
?>
I build this site with wordpress:
http://mida.org.il/
As you can see, homepage takes a lot of time to load.
I'm trying to fix this - there are five custom loops in that page, three of them using posts_per_page and cat to query posts, and posts_per_page set to 3.
My question is, if the loop gets to the third post, it stops and and breaks out, or its keep looping until it gets to the last post?
If the second is correct, no wonder that it's so slow, this site holds thousands of posts.
The code for the loops:
if ( $first_special_cat ){
$args = array( 'cat'=>$first_special_cat, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky );
$cat_name = $first_special_cat;
$cat_id = get_cat_ID($first_special_cat);
}else{
$args = array( 'cat'=>50, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky );
$cat_name = get_cat_name(50);
$cat_id = 50;
}
$the_query = new WP_Query($args);
echo '<div class="special-proj-main-title">';
echo '<div class="homepage-blueline-title"></div>';
echo '<h4 class="special-cat-name"><a href="' . esc_url( get_term_link($cat_id) ) . '">' . $cat_name . '</h4>';
echo '</div>';
?>
<div class="row">
<div class="col-sm-4">
<?php if ( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); //Setting the three posts to the right: ?>
<h2 class="special-project-title"><a class="special-proj-title-link" href="<?php echo esc_url( get_the_permalink() )?>"><?php the_title()?></a></h2> <br/>
<div class="post-meta special-project-meta"><?php mida_post_meta()?></div><br/>
<?php
endwhile;
wp_reset_postdata(); ?>
<span class="to-all-posts"><?php echo sprintf( __('Load more posts from %s', 'mida'), $cat_name ); ?></span>
<?php
else:
echo "You put wrong id";
endif;
?>
</div>
<div class="col-sm-8 home-background-img">
<?php
if ( $first_special_post )
$args = array('name' => $first_special_post, 'posts_per_page' => 1 );
else
$args = array('cat'=>50, 'posts_per_page' => 1, 'orderby'=>'date' );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$first_special_img = get_field('rectangular_image');
if ( $first_special_img )
$first_special_img_src = wp_get_attachment_image_src( $first_special_img['id'], 'full' );
else
$first_special_img_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
?>
<div class="special-project-section" style="background: url('<?php echo $first_special_img_src[0]; ?>');background-size: contain;">
<a href="<?php echo esc_url( get_the_permalink() )?>" title="<?php the_title() ?>"><span style="
position:absolute;
width:100%;
height:100%;
top:0;
left:0;
z-index: 1;"></</span>
</a>
<?php
echo '<div class="special-cat-on-img">';
echo $first_special_text ? '<h5><div class="special-cat-name-img">' . $first_special_text . '</div></h5>' : '<h5><div class="special-cat-name-img">' . __('Special Project', 'mida') . '</div></h5>'; ?>
<h6 class="speical-cat-title-img"> <?php the_title() ?> </h6>
<?php echo '</div>'; ?>
<div class="blue-line"><?php echo '<div class="special-proj-ex">' . $first_special_cat_ex . '</div>'; ?></div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div>
</div>
X3.
every loop query different categories ( $first_special_ are custom fields, input from the user ).
So can anyone help me optimize this code (and answer the above question)?
Thanks!
You problem is NOT in your loop but rather in the page itself. Total page size is a whopping 11.8MB! This looks primarily due to a ton of images. You might try a little image optimization (use jpg's for post thumbs/images) and make sure images are sized correctly. Honestly, lazy loading might be a good solution here! There are plenty of options out there to make it easy to implement.
I want to add Blog Page in front-page.php.I want to show only post title with link to actual post.I dont know where to put the_permalink in my code.My code like below:
<div class="profile">
<?php
$query = new WP_query( 'pagename=blog' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ){
$query->the_post();
echo '<h2 class="text-center">' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
?>
</div><!--end blog-content -->
When I put ,the hyperlink goes to domain.com/get_permalink() not to actual url.
Example the actual url is domain.com/abc.
When I click to the post title,it goes to domain.com/get_permalink()
Anyone can help me to solve my problem?
May be you are using get_permalink in wrong way. Try following code.
<div class="profile">
<?php
$query = new WP_query( 'pagename=blog' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ){
$query->the_post();
echo '<h2 class="text-center"><a href="' . get_permalink() . '">';
the_title();
echo '</a></h2>';
}
}
wp_reset_postdata();
?>
</div><!--end blog-content -->
I'm to list categories with images in the sidebar this is how I do it (and it works) I do this because I've certain categories I do not want to display!
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php echo get_cat_name(12); ?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
but I need to copy past that code for every single category... and all this code for only changing a number is a good practice I guess. Is there another way it could be done?
I've tried with a foreach but it seems to be wrong
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php foreach($latests as $latest) :?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
Well, You can do it like this:
<ul>
<?php
$cat_args=array(
// 'include' => '3,6,9', // display only these categories
'exclude' => '3,6,9', // display all categories except categories 3,6,9
'orderby' => 'name', // the order
'order' => 'ASC' // asc or desc
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => 2, // how many posts you want to display
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<h3> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in: %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
foreach($posts as $post) {
setup_postdata($post);
?>
<li>
<div>
<div><?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?></div>
<div><?php the_title(); ?></div>
</div>
</li>
<?php
} // close foreach
} // close if
} // close foreach
?>
</ul>
The easiest way is like that
<?php wp_list_categories('orderby=name&exclude=3,5,9,16'); ?>
So, this will return you all the categories excluding the one you specified. After this, you can get the actual image you want for your categories and all.
Hey there, I'm trying to create a page showing specific pages (hope that makes sense), probably by calling their post ID's or something.
I want to pull in the page thumbnail/featured image, the page title, the page's description, and then a link to that page.
Something along the lines of this.
<ul>
<li>
<?php the_post_thumbnail(); ?>
<h2>Page Title</h2>
<p>Page Description</p>
Link to page
</li>
</ul>
Any help will be appreciated, thanks in advance.
UPDATE: At the moment I've got something like this. Using a custom field to bring in the description. I'm still trying to work out how I'd only show pages that are under a parent page called "Culture".
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>
UPDATE 2: Solved it! Used the following if anyone's interested.
Pulled in all subpages from parent page (id=7).
Then the post thumbnail, followed by the page title, description using a custom field called description and finally the permalink.
Hope this helps anyone in a similar situation.
<?php query_posts('post_type=page&post_parent=7'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('culture-page-listing'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>
I wrote a loop in WP some time ago which I'm sure isn't perfect, but it did basically something like that (delimited by categories).
http://www.kyleboddy.com/2010/10/14/wordpress-code-attachment-category-loop/
<?php
$areas = array(1 => 'Seattle','East Side & Mercer Island','North Side','South Side');
$slugs = array(1 => 'seattle-jobs','east-side-and-mercer-island-jobs','north-end-jobs','south-end-and-west-seattle-jobs');
$i = count($areas);
$n = 1;
while ($n <= $i)
{
global $post;
$myposts = get_posts('numberposts=-1&offset=0&category_name=' . $slugs[$n]);
echo '<div id="imageList">';
echo '<a name="' . $areas[$n] . '"></a><h2>' . $areas[$n] . '</h2>';
echo '<table id="ourwork"><tr>';
$x = 1;
foreach($myposts as $post)
{
setup_postdata($post);
echo '<td>';
$args = array(
'post_type' => 'attachment',
'numberposts' => '-1',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
$y = count($attachments);
$y--;
echo '<a href="' . $post->guid . '">';
echo wp_get_attachment_image($id = $attachments[$y]->ID, $size=array(200,133), $icon = false);
echo '<strong><br><br>';
echo apply_filters('the_title', $attachments[$y]->post_title);
echo '</strong></a>';
echo '</td>';
if ($x == 4)
{
echo '</tr><tr>';
$x = 0;
}
$x++;
}
}
echo '</tr></table>';
echo '</div><div class="blog"></div>';
$n++;
}