I need to add links to WordPress Posts on a static HTML page. I got some information that has been helpful but need a few more elements to make it complete.
Here is the code I have so far:
<?php
$number = 5;
$wordpress_header = "blog/wp-blog-header.php";
// Include wordpress header
if (file_exists($wordpress_header))
{
include ($wordpress_header);
$myposts = get_posts('numberposts=$number&offset=0&category=0');
echo "<ul class='Bloglinks'>";
foreach(array_slice($myposts, 0, $number) as $post)
{
echo '<li><a href="';
the_permalink();
echo '">';
the_date();
echo " ";
the_title();
echo '</a></li>';
}
echo "</ul>";
}
else
{
echo "Unable to connect to Wordpress header file.";
die();
}
?>
This only shows the titles of the most recent posts. I need to be able to display the following:
<h5>The truth about Lazy Eye</h5>
<p class="blog-info">07.16.10 | <a class="comment-ref">3 Comments</a></p>
<h5>More Adult Learning Problems Linked to Eyes</h5>
<p class="blog-info">06.12.10 | <a class="comment-ref">1 Comments</a></p>
<h5>New Vision Examination Instruments Arrived!</h5>
<p class="blog-info">05.10.10 | <a class="comment-ref">13 Comments</a></p>
You should use the function query_posts() with the functions have_posts() and the_post(). Here is an example for the WordPress API:
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
That will loop through all posts you have queried. So you can just insert your query from the get_posts() function into the query_posts() function.
EDIT: I think if you want to stick with the get_posts() function, you have to call the setup_postdata() function to get the new post (source code for the API):
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
But I would recommend to take the query_posts() function instead.
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
$args = array(
// 'cat' => 3, // Only source posts from a specific category
'posts_per_page' => 6 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<article class="vertical-item content-padding ls">
<div class="item-media">
<img src="<?php the_post_thumbnail() ?>" alt="<?php the_title(); ?>">
</div>
<div class="item-content">
<h4 class="entry-title">
<?php the_title(); ?>
</h4>
<div>
<div class="media-body media-middle greylinks">
<br><a class="small-text" href="#"><?php the_time('l jS F, Y') ?></a>
</div>
</div>
</div>
<div class="item-footer">
<a class="lato lightgrey weight-black" href="<?php the_permalink(); ?>">Read this Article</a>
</div>
</article>
<? }
} else {
echo '<p>There are no posts available</p>';
}
wp_reset_postdata();
?>
Related
I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?
<?php if ( ! is_single() ) { ?>
<div class="post-container">
<?php } ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
//Post Title code
//Post Thumbnail code
//Post Content / Excerpt code
//Post Meta code
</article> <!-- /post -->
<?php if ( ! is_single() ) { ?>
</div>
<?php
<?php } ?>
It's very easy to add latest post from each category.
First of all get all the categories of blog by using below code:
$categories = get_categories();
Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.
Now you need to define the arguments for your query. Inside the braces, add this:
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Next, insert your query, using the WP_Query class:
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt( __( 'Continue Reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
This will display each category posts in your home page. Please try to use it and let me know if you have any issue.
I’m using this code to display wordpress posts in divs with specific classes. Is there a way to get the posts in a random order and still do this? I’ve searched and can’t find anything that has helped me figure this out. Thanks.
<?php $count = 0; if (have_posts()) : while(have_posts()) : the_post() ?>
<div class="grid4 noall
<?php if( $count%3 == 0 ) { echo 'first'; }; if( $count%3 == 1 ) { echo 'mid'; }; if( $count%3 == 2 ) { echo 'last'; }; $count++; ?>">
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<a class="plink" href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ?>">
<section class="portfolio-wrapper">
<?php the_post_thumbnail( 'thumb-800' ); ?>
<div class="portfolio-title">
<h3 class="portfolio-header"><?php the_title(); ?></h3>
<?php if( get_field('portfolio_subtitle') ): ?>
<h5 class="portfolio-category">
<?php the_field('portfolio_subtitle'); ?>
</h5>
<?php endif; ?>
</div>
</section>
</a>
</article>
</div>
<?php endwhile; ?>
Is this in the main WordPress loop, or are you running a custom query with WP_Query? If the latter, you could just simply pass the 'orderby' => 'rand' parameter in your arguments array. If it's in the main WordPress loop, the answer's slightly different, but similar. Essentially, you'd want to apply the set_query_var function to the main loop, conditionally depending on which page / view you're working with.
https://codex.wordpress.org/Function_Reference/set_query_var
for instance,
if(is_post_type_archive('products')){
set_query_var('orderby','rand');
}
I recently updated wordpress and now all content is outputting for this module, except "the_excerpt()"
<?php
function blog_feed_content(){
?>
<ul id="blog_list" class="jscroll">
<?php
global $post;
$args = array('category' => 4 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="post clearfix" id="post-<?php the_ID(); ?>">
<div class="post-content clearfix">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-date"><?php the_time('F Y') ?></div>
<div class="post-text">
<?php the_excerpt(); ?>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php
}
function widget_blog_feed($args){
extract($args);
echo $before_widget;
echo $before_title;?>Blog Feed<?php echo $after_title;
blog_feed_content();
echo $after_widget;
}
function init_blog_feed() {
register_sidebar_widget(__('blog_widget'), 'widget_blog_feed');
}
add_action("plugins_loaded", "init_blog_feed");
?>
Why on earth isn't it outputting this one piece of content?
YOU'RE ALL AWESOME. Thanks.
setup_postdata is not exactly the same as using a normal WP_Query with the_post() so not all template tags work as expected with this method of displaying posts.
You should rewrite the code to use a custom WP_Query and traditional Loop rather than using a foreach to iterate through post objects.
Something like:
$myposts = new WP_Query('cat=4');
if( $myposts->have_posts() ) : while( $myposts->have_posts() ) : $myposts->the_post(); ?>
<li class="post clearfix" id="post-<?php the_ID(); ?>">
<div class="post-content clearfix">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-date"><?php the_time('F Y') ?></div>
<div class="post-text">
<?php the_excerpt(); ?>
</div>
</div>
</li>
<?php endwhile;
wp_reset_query;
endif; ?>
That should point you in the right direction. If you are set on sticking with the foreach and get_posts method you could always use some simple string functions (i.e. substr() to truncate a little excerpt from the post_content property of the $post object, like replacing the <?php the_content(); ?> line with:
<?php echo substr($post->the_content, 0, 80); // displays first 80 chars of post ?>
you should define id.
<?php echo get_the_excerpt($post->ID); ?
I'm trying to display 3 posts below my single post view (I have custom post types set up so want this query to work on all single post pages regardless of the post type).
However with the code below I don't get any related posts displayed. When I removed .'&exclude=' . $current I get 3 related posts display but with the current post being one of them. Hence why I added 'exclude' but I don't see why its not displaying any when I add this in.
Any help would be appreciate.
Thanks
<?php
$backup = $post;
$current = $post->ID; //current page ID
global $post;
$thisPost = get_post_type(); //current custom post
$myposts = get_posts('numberposts=3&order=DESC&orderby=ID&post_type=' . $thisPost .
'&exclude=' . $current);
$check = count($myposts);
if ($check > 1 ) { ?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
<ul class="group">
<?php
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
<article>
<h1 class="entry-title"><?php the_title() ?></h1>
<div class="name-date"><?php the_time('F j, Y'); ?></div>
<div class="theExcerpt"><?php the_excerpt(); ?></div>
</article>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php
$post = $backup;
wp_reset_query();
?>
</div><!-- #related -->
<?php } ?>
Instead of using get_posts(), you could use WP_Query
<?php
// You might need to use wp_reset_query();
// here if you have another query before this one
global $post;
$current_post_type = get_post_type( $post );
// The query arguments
$args = array(
'posts_per_page' => 3,
'order' => 'DESC',
'orderby' => 'ID',
'post_type' => $current_post_type,
'post__not_in' => array( $post->ID )
);
// Create the related query
$rel_query = new WP_Query( $args );
// Check if there is any related posts
if( $rel_query->have_posts() ) :
?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
<ul class="group">
<?php
// The Loop
while ( $rel_query->have_posts() ) :
$rel_query->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
<article>
<h1 class="entry-title"><?php the_title() ?></h1>
<div class="name-date"><?php the_time('F j, Y'); ?></div>
<div class="theExcerpt"><?php the_excerpt(); ?></div>
</article>
</a>
</li>
<?php
endwhile;
?>
</ul><!-- .group -->
</div><!-- #related -->
<?php
endif;
// Reset the query
wp_reset_query();
?>
Try out the code above and modify it for your own need. Modified to suit your own markup.
i want to display portfolio category above the content where all portfolio is displayed. i know some of the modification is required in loop portfolio.php but how put that so that it display above the content post. i want to display in ul li format how it possible?? i m using broadscope theme
my code for loop portfolio.php is given below
<?php
global $avia_config;
if(isset($avia_config['new_query'])) {
query_posts($avia_config['new_query']);
}
// check if we got a page to display:
if (have_posts()) :
$loop_counter = 1;
$extraClass = 'first';
//iterate over the posts
while (have_posts()) : the_post();
//get the categories for each post and create a string that serves as classes so the javascript can sort by those classes
$sort_classes = "";
$item_categories = get_the_terms( $id, 'portfolio_entries' );
if(is_object($item_categories) || is_array($item_categories))
{
foreach ($item_categories as $cat)
{
$sort_classes .= $cat->slug.'_sort ';
}
}
?>
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>
<?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>
<?php the_title(); ?>
<span class='date_sort hidden'><?php the_time('Y m d H i s'); ?></span>
<div class="entry-content">
<!--<?php the_excerpt(); ?> -->
<!--<a class="more-link" href="<?php echo get_permalink(); ?>"><?php _e('Read more','avia_framework'); ?></a>
-->
</div>
<!-- end post-entry-->
</div>
<?php
$loop_counter++;
$extraClass = "";
if($loop_counter > 3)
{
$loop_counter = 1;
$extraClass = 'first';
}
endwhile;
else:
?>
<div class="entry">
<h1 class='post-title'><?php _e('Nothing Found', 'avia_framework'); ?></h1>
<p><?php _e('Sorry, no posts matched your criteria', 'avia_framework'); ?></p>
</div>
<?php
endif;
?>
i want to display portfolio category before
<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php
_e('Permanent Link:','avia_framework')?> <?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>">
inside
<div class='post-entry one_third all_sort <?php echo
$sort_classes.$extraClass; ?>'>
so please help me if anyone know this. hope friends you help me regarding this question
Try to insert the following code
<?php
$post_id = get_the_ID()
$item_terms = get_the_terms( $post_id, 'portfolio_entries' );
if($item_terms !== false && count($item_terms) > 0)
{
echo '<ul>';
foreach ($item_terms as $term)
{
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}
?>
just after
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>