Wordpress list all posts (excerpt) php loop - php

I am creating a wordpress template and I now need some code that creates a loop to show all posts but not full posts, just excerpts.
Can anyone help please?

Use this code to generate the excerpt into the loop:
<?php
if(have_posts())
{
while(have_posts())
{
the_post();
the_excerpt();
}
}
?>
The above will generate only the excerpt of the posts. If you need extra options like post title, date, author and more you have to read the WordPress codex. http://codex.wordpress.org/Main_Page
You can read more by following the link given by markratledge below.

Everything you need to know - with examples - is here: http://codex.wordpress.org/The_Loop
The most basic loop is
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
and you want to use the_excerpt() instead of the_content() See http://codex.wordpress.org/Function_Reference/the_excerpt

Related

Getting Post Thumbnail to show for Single Post Obect (WP + ACF)

I am having a hard time getting the Wordpress thumbnail to show up when I am using Advanced Custom Fields Pro Post Object.
My goal is to have the user select a single featured post to show up after the 6th post on the blog page.
This is my code (which is pretty much directly from ACF documentation):
<?php
$featured_post = get_field('featured_post', 'option');
if( $featured_post ): ?>
<?php echo esc_html( $featured_post->post_title ); ?></h3>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
This returns the correct title but the wrong featured image (from the post above).
The code above is called inside the loop on index.php inside the featured template part.
$counter = 1;
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', 'get_post_type()' );
//check the counter and display your content
if( $counter === 3 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'subscribe'); ?>
<?php } elseif ( $counter === 6 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'featured'); ?>
<?php }
//update the counter on every loop
$counter++;
endwhile;
I have tried the other code examples on the documentation page but those return all of my posts (again with only one thumbnail, not the correct thumbnail per post).
I am not sure where to go from here.
Anyone who can help would be greatly appreciated!
Since this is not in the loop context, the function does not find the correct post object set up to take the ID from. Use get_the_post_thumbnail instead, that takes the post id as parameter.
<?php echo get_the_post_thumbnail( $featured_post->ID ); ?>

Wordpress function get_post_gallery() does not register post's gallery

I want to extract the images contained in a gallery block while maintaining their correct order. Since get_children() and get_attached_media() do not seem to register when the image order is changed in wp-admin, I'm trying to use get_post_gallery() instead.
My problem is that the function returns false, even though the post does have a gallery.
I tried both the example and the plain usage from Codex. Currently, my entire single.php looks like this:
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if ( get_post_gallery() ) :
echo get_post_gallery();
else :
echo (the_ID() . " has no gallery.");
endif;
endwhile;
endif;
?>
… which results in "ID has no gallery" every time.
However, the output of print_r($post->post_content); does include the following, which seems to confirm that there is in a fact a gallery:
<!-- wp:gallery {"ids":[80,81,82]} -->
<figure class="wp-block-gallery columns-3 is-cropped">
<ul class ="blocks-gallery-grid">
<!-- … -->
I'm also attaching a screenshot from wp-admin to make sure I don't misunderstand what constitutes a gallery.
get_post_gallery() works only for native galleries which have been created in classig WYSIWYG editor. You can find more about it there. Hovewer, if you create Gallery via Gutenberg, it create the whole HTML code instead of classic editor gallery which create shortcode like this: [gallery ids="400097,400052,400051"] . Functions get_post_gallery(), get_post_galleries() and get_post_gallery_images() works only on classic galleries added via native shortcode.
Are you only trying to display the gallery block as opposed to all content? If so, maybe try this?
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if (has_block('gallery', $post->post_content)) {
echo 'yes, there is a gallery';
$post_blocks = parse_blocks($post->post_content);
foreach ($post_blocks as $post_block){
if ($post_block['blockName'] == 'core/gallery'){
echo do_shortcode( $post_block['innerHTML'] );
}
}
}
// if there is not a gallery block do this
else {
echo 'no gallery';
}
endwhile;
endif;
get_footer();
?>
I got some inspiration from reading this post btw.

How to call limit post of author on author page (wordpress)

I want to show author's limited post titles on the author page.
I'm using the code outside the wordpress loop to show limited post of author and the code is <?php query_posts('posts_per_page=20'); ?> . but its showing recent post and i want only posts from author.
How to do this, anybody have idea?
Sample Code
<?php
$the_query = new WP_Query(array('posts_per_page'=>20,'author'=>$put_author_id_here) );
while ( $the_query->have_posts() ) : the_post();
/* display the post as you do in loop*/
endwhile;
wp_reset_postdata();
?>
use
query_posts('posts_per_page=20&author=$ID');
or use
query_posts('posts_per_page=20&author_name="admin"');

Can anybody explain "The Loop" of wordpress?

Im forced to work with wordpress, and if you work with it, you probably know what i mean:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Its working, no question. But i do not understand what this actually means. Its not a ternary operator nor anything else i know. Ive never seen a statement like this in any php-projects ive worked on. So i got several questions:
What is this line exactly doing? I know that it gets all posts, iterates over them and ... what is this the_post() doing? And what are these doubledots doing?
Is this Wordpress-Only or could it be used somwhere else too?
Where is the current post stored?
Ive already googled it, but there are no information regarding my problem, noone seems to be interested in how wordpress works. I am, but i do not get it. If somebody got an explanation for me, it would be great.
<?php define('WP_USE_THEMES', false); get_header(); ?>
The loop starts here:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
and ends here:
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
This is using PHP's alternative syntax for control structures, and could also be expressed as:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
the post()
This function does not accept any parameters.
Return Values
This function does not return any values.
<?php
while ( have_posts() ) : the_post();
echo '<h2>';
the_title();
echo '</h2>';
the_content();
endwhile;
?>
have_posts()
Parameters
This function does not accept any parameters.
Return Values
(boolean)
True on success, false on failure.
Examples
The following example can be used to determine if any posts exist, and if they do, loop through them.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>
Note
Calling this function within the loop will cause an infinite loop. For example, see the following code:
<?php
while ( have_posts() ): the_post();
// Display post
if ( have_posts() ): // If this is the last post, the loop will start over
// Do something if this isn't the last post
endif;
endwhile;
?>
If you want to check if there are more posts in the current loop without this unfortunate side effect, you can use this function.
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}
1. What is LOOP
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags.
It will fetch data related to specific page
:(colon) is used to tell condition/loop starts from here. You can replace it with { }(bracket quotes)
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
2. Is this Wordpress-Only or could it be used somwhere else too?
yes of course you can use it.
You can access full wordpress functionality by including one core file with name of "wp-blog-header.php" that is located on root of wordpress directory.
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
Include this file in the top of your external file, you can access wordpress database, wordpress function , wordpress hooks too.
3. Where is the current post stored?
11 default tables are existed in wordpress database. You can see wp_posts table in database. all posts are store in this table.
suppose, if your are creating meta tag in your post, it will store in wp_postmeta
It's just an alternative syntax for:
if ( have_posts() ) { //open if
while ( have_posts() ) { //start while loop
the_post(); //call a function
See http://php.net/manual/en/control-structures.alternative-syntax.php
It's not wordpress specific and can be used in any php code.
To whom it may concern.
What is WordPress Loop?
When we save data inside WordPress(posts, pages, almost everything) data gets saved as a row inside our database fx. MySQL.
WordPress dynamically query the database and finds which row corresponds to the page you are on, and pulls that data then displays it in that section.
As this is a dynamic query and executed in a repeated manner, it is called a WordPress Loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
What are these doubledots doing? condition/loop starts from here :(colon) and is a ternary operators.
I think rest of the questions has been answered by others.

How to limit number of posts in index.php

I have an index.php with loop
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// render post here
<?php endwhile; ?>
<?php endif; ?>
Now I want to limit number of post by N on the page and create
a link to next/previous N posts.
A small code snippet is very appreciated.
UPDATE
What about the URL string? I want to make functionality similar to SO
where query is determined by URL for example
get next twenty questions from the recent
https://stackoverflow.com/questions?page=2&sort=active
You can achieve that in this way:
query_posts( 'posts_per_page=5' );
References:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query

Categories