I am attemtping - and failing - to display the category image (thumbnail) on my WordPress homepage. Here is my code:-
<?php $paged = get_query_var('paged') ? get_query_var('paged') : 1;
$cat_id = get_cat_ID( single_cat_title(null, false) );
query_posts( "cat=$cat_id&paged=$paged&posts_per_page=7" );
$i=1;
if(have_posts()): while(have_posts()): the_post();
?>
<div class="latest_cat_post col-md-3">
<div class="news_thumb">
<a href="<?php the_permalink();?>">
<?php if ( has_category_thumbnail() ) {
the_category_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-featured.png" alt="<?php the_title(); ?>" />
<?php } ?>
</a>
<div class="cat_name"><?php the_category('•'); ?></div>
</div>
However, this fails, citing "Fatal error: Call to undefined function has_category_thumbnail()", so I assume this function does not exist, but strangely, has_post_thumbnail() does, so I assumed this would be the case for categories too - perhaps I am wrong.
Can anyone give me 2 minutes of their day to fix this problem? I don't want to use a plugin, when it should be relatively straightforward to do. Thanks!
In WordPress you don't have category thumbnails, but you can try use a plugin. Try this one.
Assuming you will have the category ID, you have to do something like this:
$images = get_option('taxonomy_image_plugin');
$cat_id = $category->term_taxonomy_id;
if( array_key_exists( $cat_id, $images ) ) {
echo wp_get_attachment_image( $images[$cat_id] );
}
For more information you can visit this related question/answer.
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.
This could be a dumb question, but I just can't figure it out. I'm working on a custom WordPress theme I created by converting a html website. But when I use WP_Query, it doesn't load my blog posts. Yes, it displays a list of blog posts as it should with the loop, but when I click on each title or the 'read more' link of a post, it only changes the url in the address bar to the link of the post and then simply refreshes the hope page. It doesn't open I'm really tired. Here's the query loop in my index.php
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
//define args for query
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '30',
);
//The main query
$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(); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div><!-- blog-main-content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Please what do I do
Based on your comment above, you are missing a template file for your posts page. In your current configuration, the posts loop will be displayed on all pages of your website since you only have an index.php file. You should create a single.php template to display your single blog posts.
Here is the WordPress template hierarchy for reference: https://developer.wordpress.org/themes/basics/template-hierarchy/
code for pagination in custom type posts
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // For pagination
$loop = new WP_Query( array('post_type' => 'Portfolio','posts_per_page' => 3,'orderby'=> 'menu_order',
'paged'=>$paged ) ); ?> //For implementing pagination
<?php if ($loop->have_posts()): ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?> <div id="latestproimg">
<a href="<?php the_permalink(); ?>" rel="bookmark">
<?php the_post_thumbnail('large', array('title' => false)); ?></a>
</div>
<div id="latestpostser">
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark">
<?php echo get_the_title(); ?></a></h2> //displaying the title
<?php //echo get_the_excerpt(); ?>
<?php //the_content( 'Read the full post »' ); ?> // for displaying the content
</div>
<div class="clr"></div>
<?php endwhile;
endif; ?>
Please tell me where is the mistake in code
I also encountered this weird pagination problem even though I already put the $page option but heres what solved my problem try to change the paged parameter into page of your get_query_var function
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1; // For pagination
let me know if it works
get_query_var('paged') doesn't work if the permalinks setting changes the url to something like http://domain.com/page/2/.... So, a more flexible version of #IoQ's answer would be,
$search_values['paged'] = (get_query_var('paged')) ? get_query_var('paged')
: ((get_query_var('page')) ? get_query_var('page') : 1);
This works if either permalinks is set to something like postname OR paged=xx is part of the url
I'm trying to list posts in the category which shares the name of the page. I.e. If you are on the "Services" page it should display posts in the category "Services". I realize it is easy to do with conditionals such as:
<?php if ( (is_page('Groups')) ) { query_posts('category_name=groups');
while (have_posts()) { the_post();?>
<h2 class="title" id="sub">Upcoming Group Programs</h2>
<a href="<?php the_permalink() ?>">
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry"><?php the_content(); ?></div>
</div>
<?php } wp_reset_query(); //Restores Global Post Data }?>
But I would like to do this without having to set multiple specific conditionals, something like:
<?php //global $wp_query; // Uncomment if necessary, shouldn't be
$test = the_title();
$args = array( 'cat_name' => $test // ARGS HERE );
$args = array_merge( $args , $wp_query->query );
query_posts( $args ); while (have_posts()) { the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry"><?php the_content(); ?></div></div>
<?php } ?>
Any thoughts! Obviously I could interchange the page & category "name" with the "slug" or whatever works best. Thanks!
Thanks! I changed a few things around and got it working with your suggestion.
<?php
$catmatch = get_the_title();
//The Query
query_posts('category_name=' . $catmatch ); ?>
Hopefully on the last line there I did the concatenation correctly, it seems to work but if that isn't how it is supposed to be done properly please let me know!
Try changing:
$test = the_title();
to:
$test = get_the_title();
You may also have to remove the line with array_merge();
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();
?>