I would like to display a few post titles from a specific category on homepage. First one have to be with a small thumbnail and excerpt and rest of them just title. Below this section I would like to have a link clicking on which will show all the posts under this category.
like this http://i.stack.imgur.com/N5jUA.jpg
As arslaan ejaz said, you can use wp_query. But I think that answer is not enough for your question. You want to show first post with thumbnail and others with titles only right?. It can be done by php count. Here is what I am using on my site. check below code, it will show first post with thumbnail, title and excerpt, other three posts with title only from category ID 1.
<div class="main-div">
<div class="thumbnaildiv">
<?php $count=1; $query = new WP_Query('showposts=4&cat=1&offset=0'); if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php if($count==1){ ?>
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</h2>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<p><?php the_excerpt(); ?> ...</p>
</div>
</div><!--div with thumbnail, title, and excerpt end-->
<div style="clear:both"></div>
<div class="without-thumb">
<ul>
<?php } if($count>1){ ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</li>
<?php } if($count==4){ ?>
</ul>
<?php } ?>
<?php $count++; endwhile; else: endif; wp_reset_postdata(); ?>
</div><!--div without thumbnail end-->
</div><!--main div end-->
The div's I have used is for information purpose only. You can change and style it as desired.
Use WP-Query:
<?php
$args = array('cat'=>1);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '<li>' . the_permalink() . '</li>';
echo '<li>' . the_excerpt() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
It will list all posts of category 1, with title, permalink, and excerpt
More info: wp_query
I can recommend you to add 'Elementor' plugin. With this plugin, you can add 'read more' and split your text. If you add 'read more' in the beginning of your text then there will be shown only title and under the title a 'read more' link.
Here is the Screenshot
Related
I want to make a plugin in wordpress like quiz but not with special questions. Only with posts. i will get 4 post tittle and 1 featured image between those 4 post. guests will select right title of featured image. i'm getting random posts with;
<ul>
<?php $posts = get_posts('orderby=rand&numberposts=4'); foreach($posts as $post) { ?>
<li><p desc="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></p>
</li>
<?php } ?>
</ul>
but i couldnt solve how i will select randomly right answer and get it's featured image as a question.
pls help
<ul>
<?php
$arr = array();
$posts = get_posts('orderby=rand&numberposts=4');
foreach($posts as $post) { ?>
<li>
<p desc="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?> </p>
<?php $arr[ get_the_ID() ]; // store every post id in array ?>
</li>
<?php } ?>
</ul>
<?php
$id = array_rand( $arr ); // choose one random post id
echo get_the_post_thumbnail( $id,'thumbnail' ); // get thubnail against id
?>
Use this code and compare user selected post id to '$id' value.
Cheers!!!
I'm not super familiar with wordpress, but I've been working on getting my layout written as a theme so I can use the wordpress platform. For some reason I can't get the_post_thumbnail function working. I already added
add_theme_support( 'post-thumbnails' );
to my functions.php file and yes I did set a featured image and include the_post_thumbnail(); in the loop.
What am I doing wrong? I went as far as to go through the actual function and found that it is having problems with the $image variable in the wp_get_attachment_image() function while referencing the wp_get_attachment_image_src() function. $image is empty, when I'm guessing it shouldn't be. It get's the post_thumbnail id just fine though so I know it knows there's an image set. It just won't display the darn thing.
I'm writing myself a custom theme from scratch so functions.php only has add_theme_support( 'post-thumbnails' ); in it right now if you're curious.
Edit:
Here's my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-entry clearfix" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" >
<?php
if (has_post_thumbnail()) {
the_post_thumbnail();
} else {
echo '<img class="home-thumb trans-border" src="' . catch_first_image() . '" width="200px" height="150px" title="' . the_title() . '" />';
}
?>
</a>
<div class="home-post">
<div class="home-meta">Posted <?php the_time('M j, Y'); ?> - <?php the_category(' , ') ?> - <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></div>
<h2 class="post-title">
<?php the_title(); ?>
<a class="read" href="<?php the_permalink(); ?>" title="Read More">Read More</a>
</h2>
<div class="home-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php echo paginate_links() ?>
<?php else : ?>
<h2>Nothing Found</h2>
<?php endif; ?>
Continued:
So I went and found a theme with featured image support and copied that part of the loop exactly and still nothing:
<?php if(has_post_thumbnail()) {
echo '<span class="thumbnail">';the_post_thumbnail(array(100,100)); echo '</span>';
} else {
$image = evlget_first_image();
if ($image):
echo '<span class="thumbnail"><img src="'.$image.'" alt="';the_title();echo'" /></span>';
endif;
} ?>
So what the heck could it be? I'm so confused...
This function will echo itself.
How to solve: Remove the echo in front of the function.
check this
Are your images locally stored on your server/local machine?
If not, the_post_thumbnail() won't work as it can't retrieve an image based on a URL.
I am using an if elseif statement to check what page it is, and it requires that there are posts on the page so when a search is returned with a result of 0, my code stops working. Any ideas how to code this better?
this code is in my sidebar and is showing the recent articles, you can see an example of a search working here(the sidebar is setup as a sub-footer):
http://ivry.sweetyams.ca/?s=new
and a search not working here:
http://ivry.sweetyams.ca/?s=asjdfkl%3B
Code I am using:
(I have tried putting stuff into the else{ code and it doesn't work either because there are no posts on the 'nothing found' search page
<?php if (have_posts()) : ?>
<?php /* IF SEARCH PAGE */ if (is_search() ) { ?>
<?php query_posts('category_name=0&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF ESCALADE PAGE */ }elseif (is_category_or_sub(6)) { ?>
<?php query_posts('category_name=escalade&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php query_posts('category_name=montagne&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php } else { ?>
DO SOMETHING ELSE
<?php }?>
<?php endif; ?>`
I am trying to get all recent articles for my sidebar and when there is no result in the search, it also stops my sidebar from working.
The code I added is not in my search page but in my SIDEBAR, it separates the comments into 2 categories, (6 and 14) I am essentially splitting my site using categories for navigation, anything as a child below 6 will be styled one way and 14 the other, you can see the difference on the escalade and montagne links:
This is getting every post below escalade, OR every post below the montagne category
My search pulls results from ALL categories, but if there is no search result, for some reason my sidebar code, the code I included, doesn't work.
first, showposts is deprecited since v2.1, used posts_per_page instead.
second, after each query_post, you need to reset the query like this:
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
this is how I understand it. see more detail here:query_posts(), hope this helps.
I found a way to get it to work,
I found that it is encouraged to use WP_Query for secondary posts within a page, http://new2wp.com/noob/query_posts-wp_query-differences/
also, for anyone that is wondering the is_category_or_sub function is from here: http://valendesigns.com/wordpress/is-category-or-subcategory-wp-function/
This code checks whether you are on a category or subcategory,
<?php
$my_posts = new WP_Query(); ?>
<?php /* IF ESCALADE PAGE */ if (is_category_or_sub(6)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=6');?>
<?php /* ELSEIF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=14'); ?>
<?php } /* AND OTHER PAGE */ else { ?>
<?php $my_posts->query('posts_per_page=5'); ?>
<?php }?>
<?php while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts ?>
<a href="<?php echo esc_url( get_permalink( $post->the_permalink ) ) ?>" title="Permanent Link to <?php echo substr($post->post_title,0,200);?>">
<p><?php echo substr($post->post_title,0,200); ?></p>
</a>
<?php endwhile; ?>
<?php wp_reset_query() // RESET QUERY ?>
I would like to list all post on one page, but I would like the posts to be grouped by tags. Is this possible in Wordpress?
Basically this way:
Bikes:
Posted 2/8/2011
Some post title 1
Tag bikes
Posted 1/8/2011
Some post title 2
Tag bikes
Cars:
Posted 5/8/2011
Some post title 5
Tag cars
Posted 29/7/2011
Some post title 6
Tag cars
Boats:
Posted 30/7/2011
Some post title 4
Tag boats
Is this possible to do? It should dynamic so that I can create new tags from WP admin and they would show up automatically.
Loop though the tags using get_tags() and use get_posts with the 'tag_in' argument.
ie.
<?php foreach(get_tags() as $term){ ?>
<?php $posts = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'project', 'tag__in' => $term->term_id ) ); ?>
<?php if($posts) : ?>
<h3><?php echo $term->name; ?></h3>
<?php foreach($posts as $post) : ?>
<?php setup_postdata($post); ?>
<div class="item col-sm-12">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?> <br/>
</a>
<a class="button" href="<?php the_permalink() ?>">Read More</a>
</div>
<?php endforeach ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php } ?>
It depends. Are you trying to show posts for all tags, or just listing all posts by specific tags you've already decided on?
If you know the tags you want to display, here is how you would list posts by tag.
<?php $bikePosts = new WP_Query('tag=bikes');
while ($bikePosts->have_posts()) : $bikePosts->the_post(); ?>
<h2>Bikes:</h2>
<p>Posted <?php the_time('j/m/Y'); ?> <?php the_title(); ?>
<?php endwhile;
//reset post data for next tag
wp_reset_postdata();
?>
More info: http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
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();
?>