please don't give a negative feed like always and instead tell me the mistake.
I have a blog.
can i make the homepage display a summary of an article instead of the whole article?
if yes then where or how can i do it?
I have this pre-set in one of my WordPress sites. Here is what this looks like and it is in Appearance/Editor/Posts Page (home.php)
<div class="post-content">
<?php $content = get_the_content(); ?>
<?php echo wp_trim_words(strip_tags($content), 30); ?>
</div>
<a class="blog-post-read-more" href="<?php echo esc_url( get_the_permalink( get_the_ID() ) ); ?>"><?php echo esc_html( get_theme_mod( 'relia_blog_read_more', __( 'Read More', 'relia' ) ) ); ?></a>
</div>
So what this does is it will strip the words up to 30, wp_trim_words. And below that is the how to insert Read More.
Here are some links for you to check out:
https://codex.wordpress.org/Excerpt
https://codex.wordpress.org/Customizing_the_Read_More
Create one custom template that template you can assign your home page
/*
Template Name: Blog
*/
query_posts( array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ) ) );
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format()); ?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
wp_reset_postdata();
Related
I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I'm quite new to WordPress Development been following some online tuts and read bits and bobs of documentation but my WP_Query still displays nothing and when I add an else to my loop the else condition displays. I have created the page I'm trying to pull from the database what am I doing wrong here's the code:
<section id="home">
<?php
$query = new WP_Query( array( 'pagename' => 'home' ) );
if ($query->have_post() ) {
while ($query->have_posts() ){
$query->the_post();
echo '<div class="entry-content">';
the_content();
echo '</div>';
}
}
wp_reset_postdata();
?>
</section>
simply change
$query->have_post()
to
$query->have_posts()
use the below code for display content
$args=array(
'post_type' => 'post'
);
$the_query = null;
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content"><?php the_content(); ?></div>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
I use the normal pagination method in wordpress
<?php $i = 1 ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => "2", 'paged' => $paged, 'cat' => 26 );
query_posts($args);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="art_1">
<h5><?php echo $i ?>. <?php the_title(); ?></h5>
<?php $i++; ?>
</div>
<?php endwhile; ?>
<div class="er">
<?php next_posts_link(); ?>
</div>
<div class="err">
<?php previous_posts_link(); ?>
It works fine but the problem is this pagination I use for listing out articles in a particular category ,now I have another category of articles in same page which needs pagination too.pagination of 1st should no affect the other and vice versa. Can anyone suggest me what should I do?
Use this hook after pagination..
<?php wp_reset_query(); ?>
I'm developing a wordpress 3.3.1 theme and I'm having troubles with the single.php file.
It displays - no matter what post (&p=111 e.g.) you select - only the content of the newest post.
This is my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1 class="page-title"><?php the_title() ?></h1>
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="cover" />
<?php endif; ?>
<p class="page-text">
<?php the_content(); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
What could be wrong? I hope you've understood my problem. Thank you!
edit:
I recently updated the header file. When I delete this loop, it works fine:
<ul class="nav-dropdown">
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 5,
'exclude' => '1,2,3,4,5,6,8,9,10,11,12,13,14'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
$post_args = array(
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<li class="nav-dropdown"><?php the_title(); ?></li>
<?php
}
}
?>
</ul>
I'd change your variable names in the header as ones such as $post are reserved by Wordpress for handling single post pages.
I am not sure but please change the $post variable to any other variable and than try
may be your problem be solved.
Because $post is global variable of post.
On the Wordpress site I'm working on, it lists posts by category, but I am also after a page that lists ALL the posts (with pagination, showing 10 per page). How would I go about achieving this?
Thanks
You could create a new page template with this loop in it:
<?php
$paged = get_query_var('paged')? get_query_var('paged') : 1;
$args = [
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
];
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; ?>
<!-- then the pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>
For others who might be Googling this... If you have replaced the front page of your site with a static page, but still want your list of posts to appear under a separate link, you need to:
Create an empty page (and specify any URL/slug you like)
Under Settings > Reading, choose this new page as your "Posts page"
Now when you click the link to this page in your menu, it should list all your recent posts (no messing with code required).
A bit more fancy solution based on #Gavins answer
<?php
/*
Template Name: List-all-chronological
*/
function trimStringIfTooLong($s) {
$maxLength = 60;
if (strlen($s) > $maxLength) {
echo substr($s, 0, $maxLength - 5) . ' ...';
} else {
echo $s;
}
}
?>
<ul>
<?php
$query = array( 'posts_per_page' => -1, 'order' => 'ASC' );
$wp_query = new WP_Query($query);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php the_time( 'Y-m-d' ) ?>
<?php trimStringIfTooLong(get_the_title()); ?>
</a>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts published so far.'); ?></p>
<?php endif; ?>
</ul>