wp_query - limit number od posts, and pages - php

OK, I creating all posts page view, and stuck on loop. Here is the wp_query:
<?php if ( ! is_single() ) : ?>
<div class="entry-content">
<?php
$first_articles = new WP_Query(
array(
'posts_per_page' => 2,
'numberposts' => 2,
'category_name' => 'artykuly',
'order' => 'DESC',
)
);
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'twentyseventeen' ),
'after' => '</div>',
'link_before' => '<span class="page-number">',
'link_after' => '</span>',
) );
?>
<?php if ( $first_articles->have_posts() ) : while ( $first_articles->have_posts() ) : $first_articles->the_post(); ?>
<div class="kat-img" style="background: url('<?php the_post_thumbnail_url(); ?>')">
<div><?php the_title( '<h2 class="entry-title">', '</h2>' ); ?></div>
<div><?php echo excerpt(10); ?></div>
<div><?php echo get_the_date(); ?></div>
<div><?php echo the_tags( '<ul><li>', '</li><li>', '</li></ul>' ); ?></div>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
</div><!-- .entry-content -->
<?php endif; ?>
This is the code inside content.php (there is nothing more, only this) and problem with this loop is that it display 2 latest post but these 2 latest post loop is cloned and displayed 6 times. Where is the problem? Should I add some more limits for this loop? I want to display 2 latest posts only one time.

There is most probably another loop in the outer file like archive.php which is being used to display posts.
Either add this code directly in archive.php or category.php or remove the other loop from those files.
Also remove 'numberposts' => 2 from the wp query, it is not needed.

Related

Wordpress: List multiple post types on Index Page

I am working with a child theme of the twothousand-seventeen-theme because I would like to change a few things.
But I've got a problem in realizing this one idea:
I have created a custom post type named 'video' but on the index-page (index.php) I want to show both types of posts, the default post-type and the custom 'video' post-type, ordered by publishing date.
Since I really like the way the index.php features the latest posts a don't really want to create a custom archive (I guess I have to keep the get_template_part() function for that). All I wanna do is to show both post-types on the index page.
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php endif; ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-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 Format name) and that will be used instead.
*/
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
the_posts_pagination( array(
'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
) );
else :
get_template_part( 'template-parts/post/content', 'none' );
endif; ?>
I've tried many ways but none of them worked for my idea...
There's got to be a way to select both post-types in the wp-query and show them on the index page, ordered by publishing date and in the default content template.
Like to select all posts (of post and video type) for the get_post_format() function (but i am not really into PHP)
I appreciate any idea.
Regards,
Malte
You can do like this, In your index.php
Put the below code where you want to display all the posts title and content.
<?php
$args = array(
'post_type' => array( 'post', 'video' ),
'orderby' => 'date',
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="smal-sec">
<h3><?php the_title();?></h3>
<?php the_content();?>
</div>
<?php
endwhile;
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
From this loop we will able to get all posts form default post and "video" custom post type. Posts will display according to date.

how to customize post in wordpress?

I try to customize my own post using the shortcode way in wordpress. I want to display the post according to its category.
<?php
function sample_post($atts) {
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<label><?php the_title(); ?></label>
<?php
endwhile;
endif;
}
add_shortcode('sample_post', 'sample_post');
?>
the code is fine and I have a file in my template name content-page.php
here is the code of my content-page
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'unite' ),
'after' => '</div>',
) );
edit_post_link( __( 'Edit', 'foodgrower' ), '<span class="edit-link">', '</span>' );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
I don't know if I miss something about the setup or query on it. Here's I really want to displays. I only want to display <label><?php the_title(); ?></label> (the title of the post), but now it displays the content-page.php. I don't get it also why it appears in my page. I didn't call the content-page.php to display in my page.
Change your code to return the data, like the following,
function sample_post($atts) {
$return = '';
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$return .= "<label>".the_title()."</label>";
endwhile;
endif;
return $return;
}
add_shortcode('sample_post', 'sample_post');

Getting excerpt in loop within loop – Twenty sixteen theme Wordpress

Hello fellow wordpress expirienced users.
I’m having a struggle while doing a latest 3 posts excerpt with thumbnail in my homepage in Wordpress TwentySixteen Theme.
I tried various possibilities but i cant get it running.
The goal is to look the posts like this:
http://caenthemes.cekuj.net/?s=p%C5%99%C3%ADsp%C4%9Bvek
My thought is to use an already made template for the seach page.
But than it the text of the excerpt is nowhere:
http://caenthemes.cekuj.net/
The fact that its not styled leave aside please.
The code of the main page:
<?php
/**
* The template for displaying main-page without title.
* #package WordPress
* #subpackage Twenty_Sixteen
* #since Twenty Sixteen 1.0
*/
?>
<section id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!--<header class="entry-header">
<?php the_title('<h1 class="entry-title">', '</h1>'); ?>
</header><!-- .entry-header -->
<div class="wp-page-content">
<?php
the_content();
wp_link_pages(array(
'before' => '<div class="page-links"><span class="page-links-title">' . __('Pages:', 'twentysixteen') . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __('Page', 'twentysixteen') . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
));
?>
</div><!-- .entry-content -->
</section>
<section>
<header class="entry-header">
<h2>
<?php
if (get_locale() == 'cs_CZ') {
echo "Nejnovější příspěvky";
} else {
echo "Latest posts";
}
?>
</h2>
</header><!-- .entry-header -->
<?php
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post);
get_template_part('template-parts/content', 'search');
endforeach;
wp_reset_postdata();
?>
</section><!-- #wp-page-content-## -->
I’m 80% sure that i’m not handling right the inner loop within the main loop of twenty sixteen. Just to cover all posibilities i was also trying to do it not via get template but still i only get the posts categories titles and thumbnails but not the excerpt.
Can you help me with this ?
The whole page is based on twenty sixteen theme with my modifications.
Thank you very much,
Caen Ragestorm
For getting post details such as title,content and featured image you can use this code :
$latestPost = new WP_Query( array( 'post_type' => 'posts', 'posts_per_page' =>-1,'order' => 'ASC') );
while ( $latestPost->have_posts() ) : $latestPost->the_post();
$sTitle = the_title();
$sContent = the_content();
$feat_image_latestPost = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
endwhile;
Re write CSS according to your requirement.
Thanks to SJP i made it work. So the full code working for me is here, hope anyone else will find it usefull:
<section id="latest-posts">
<header class="entry-header">
<h2>
<?php
if (get_locale() == 'cs_CZ') {
echo "Nejnovější příspěvky";
} else {
echo "Latest posts";
}
?>
</h2>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
$args = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$latestPost = new WP_Query($args);
while ($latestPost->have_posts()) : $latestPost->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title(sprintf('<h2 class="entry-title">', esc_url(get_permalink())), '</h2>'); ?>
</header><!-- .entry-header -->
<div class="post-summary">
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
<?php the_post_thumbnail('post-thumbnail', array('alt' => the_title_attribute('echo=0'))); ?>
</a>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
</div>
<?php if ('post' === get_post_type()) : ?>
<footer class="entry-footer">
<?php twentysixteen_entry_meta(); ?>
<?php
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
__('Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen'), get_the_title()
), '<span class="edit-link">', '</span>'
);
?>
</footer><!-- .entry-footer -->
<?php else : ?>
<?php
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
__('Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen'), get_the_title()
), '<footer class="entry-footer"><span class="edit-link">', '</span></footer><!-- .entry-footer -->'
);
?>
<?php endif; ?>
</article>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</section>
Nice day to you all.
Caen Ragestorm
www.CaenRagestorm.cz

Show post if true/false is yes. Advanced custom fields

I have managed to get my loop to show only the posts that display true on an advanced custom field.
But I now only want to show one post. I cant seem to get it to only loop one of the posts that features the true/false field as yes.
'posts_per_page' => '1'
Doesn't work as it only shows the latest post.. which if its not ticked it just shows blank.
<?php
$args = array(
'post_type' => 'event'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 'yes' == get_field('sponsored_event') ): ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You should use Meta Query here. Change your args array to:
// args
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1'
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
The ACF Radio button Yes/No field is to be manipulated the other way. You have to get the Output and then compare with the value that you need.
Syntax:
$variable = get_field('field_name', $post->ID);
Where the $post->ID will be appering from the Loop that you use.
if (get_field('sponsored_event') == 'yes') {
// code to run if the above is true
}
else
{
// code for else part
}
Make sure that the value saved into the DB for the radio button is like you give in the if statement
This is an Optional for you to use the meta_value in the Query. if you dod't use you can fetch the acf value with the help of the post ID that you get from the loop of Wp_Query
Change the Wp_Query like this if you need only one post that to the latest one that has been stored.
$args = array( 'post_type' => 'event', 'posts_per_page' => 1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Else if you want all the posts that has been stored you can use like this.
$args = array( 'post_type' => 'event', 'posts_per_page' => -1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Note:
post_per_page=10 -> Will bring the 10 posts from your post type
post_per_page=-1 -> will bring infinite posts that your post type has
Entire Loop:
<?php
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Your If statement in the query seems to be Incorrect and i have added the Query Executed output over to that if statement.

How to display a short description about a Post in Wordpress?

I get Post using Wp_query(). And then show post_thumbnail and the title.
<?php
$args = array(
'type' => 'post',
'category__in' => '23',
'posts_per_page' => 1,
'offset' => 2,
);
$lastBlog = new WP_Query($args);
if ($lastBlog->have_posts()):
while ($lastBlog->have_posts()): $lastBlog->the_post();
if (has_post_thumbnail()) {
the_post_thumbnail();
the_title(sprintf('<h4 class="entry-title">', esc_url(get_permalink())), '</h4>');
}
endwhile;
endif;
wp_reset_postdata();
?>
If I want to show short description about the post under the Title and insert Read More, how can I do that?
Thanks
There are two ways you could do that. One way is to just output the content of the article, and put a 'Read more' tag in the text <!--more-->, and you'll get the text outputted with the 'Read more' button after it:
<?php
$args = array(
'type' => 'post',
'category__in' => '23',
'posts_per_page' => 1,
'offset' => 2,
);
$lastBlog = new WP_Query( $args );
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post();
if ( has_post_thumbnail() ) {
echo '<div class="post_wrapper">';
the_post_thumbnail();
the_title( sprintf('<h4 class="entry-title">', esc_url( get_permalink() ) ),'</h4>' );
the_content( 'Read more ...' );
echo '</div>';
}
endwhile;
endif;
wp_reset_postdata();
?>
I've wrapped everything in .post_wrapper div, for easier handling.
The other way is to use the excerpt() with a manually added 'Read more' button
<?php
$args = array(
'type' => 'post',
'category__in' => '23',
'posts_per_page' => 1,
'offset' => 2,
);
$lastBlog = new WP_Query( $args );
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post();
if ( has_post_thumbnail() ) {
echo '<div class="post_wrapper">';
the_post_thumbnail();
the_title( sprintf('<h4 class="entry-title">', esc_url( get_permalink() ) ),'</h4>' );
the_excerpt();
echo ''.esc_html__('Read more...', 'theme_slug').'';
echo '</div>';
}
endwhile;
endif;
wp_reset_postdata();
?>
You can choose which one to use.
Also are you sure you don't want to display the post if you don't have the thumbnail? if not, just move the if condition before the title, and you should have the post, even if you didn't set the post thumbnail. If you meant it this way, then all is ok. :D

Categories