Do not display the last post on the archive page - wordpress

I need the last post not to be displayed on the post archive page.
What should I do to do this?
You can see the page code of my page archive below.
<?php
/* Start the Loop */
$i=0;
while ( have_posts() ) :
if($i==5) {
echo '
adv code
';
}
the_post();
get_template_part( 'template-parts/content', 'archive' );
$i++;
endwhile;
custom_pagination();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
note: Only the last post. Because the last post of the page archive is displayed manually in the slider of that page. I do not want the last post in the list to be displayed and repeated.

Using offset to the query will do the trick like
function exclude_last_post( $query ) {
if ( $query->is_main_query() && !is_admin() && is_archive() ) {
$query->set( 'offset', '1' );
}
}
add_action( 'pre_get_posts', 'exclude_last_post' );

Related

how to create restrictions on viewing posts on WordPress? something like medium

I want to know what is the logic of the code that they use for this type of restriction and how can I do it on my WordPress website.
This is just a simple example of how you could do this.
Method A:
You need to update the excerpt of each article.
Edit single.php:
while ( have_posts() ) {
the_post();
if ( is_user_logged_in() ) :
// Also you can include the template part here.
the_content();
else :
// Show just the article excerpt.
the_excerpt();
endif;
}
Method B
Here you need to edit articles and add Read More tag (Shift + Alt + T) in the place where you want to restrict the article visibility.
Edit single.php:
while ( have_posts() ) {
the_post();
// Show part of article for visitors.
if ( ! is_user_logged_in() ) :
global $more;
$more = 0;
endif;
the_content();
}
Edit functions.php:
/**
* Change read more link.
*
* #return mixed Sign up page link.
*/
function modify_read_more_link() {
$url = wp_login_url(); // Your sign up page url goes here.
$title = __( 'Login or Sign up to view the rest of the article.', 'text-domain' );
return sprintf( '<a class="more-link" href="%s" title="%s">%s</a>',
esc_url( $url ),
esc_attr( $title ),
$title
);
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );

WordPress - Adding a Post Type heading within a foreach loop when multiple post types are present

I'm working on customizing the search results loop within WordPress. I'm displaying search results of two custom post types: Webinars and Research. I'd like to segment the Webinars and Research into their own sections with a heading for each section.
CURRENT LOOP
<?php
if( have_posts() ){
$types = array('webinars', 'research');
foreach( $types as $type ) {
while( have_posts() ){
the_post();
if( $type == get_post_type() ){
get_template_part('loop-templates/content', 'search');
}
}
rewind_posts();
}
}
?>
Any suggestions on getting all Webinars to live in their own div container, and all Research to live in their own div container?
You could simply specify the post type (doesn't have to be in the loop) for each of them straight in the search template. if you want 2 different templates, 1 for webinars and 1 for research, you could do something like that:
<?php if( have_posts() ){
if ( 'webinars' === get_post_type() ):
echo '<h2>Webinars</h2>';
while( have_posts() ){
the_post();
//Template for webinars
the_title(); echo '<br/>';
};
endif;
if ( 'research' === get_post_type() ):
echo '<h2>Research</h2>';
while( have_posts() ){
the_post();
//Template for research
the_title(); echo '<br/>';
};
endif;
}; ?>
You can also restrict the search parameters to go further using pre_get_posts
from the function.php file:
<?php add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_search() && $query->is_main_query() && in_array ( $query->get( 'post_type' ), array( 'webinars', 'research' ) ) ) {
$query->set( 'post_type', array( 'webinars', 'research' ) );
$query->set( 'posts_per_page', 12 );
};
} ); ?>
You can pretty much do anything just keep in mind that your search page must adapt itself to any search query, don't lock it to one specific post type.

Add banner after the third post in wordpress 4.0

How do I add a banner or a DIV with content after the third post on the index or category of Wordpress 4.0?
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
You can also make us of the inbuilt current_post property that is populated in WP_Query so it looks like:
// Start the Loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
//count starts at 0
if( $current_post == 2 ) :
echo '<div class="banner"><img src="your_banner_url" /></div>';
endif;
endwhile;
$counter = 1;
// Start the Loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
if( $counter == 3 )
echo '<div class="banner"><img src="your_banner_url" /></div>';
$counter++;
endwhile;

Woocommerce - show products in search results before everything else

Currently I have the following code:
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ( get_post_type() !== 'post' ) {
if ( get_post_type() == 'landing-pages' ) {
get_template_part( 'templates/content/archive', 'landingpages' );
} elseif ( get_post_type() == 'product' ) {
get_template_part( 'templates/content/archive', 'product' );
} else {
get_template_part( 'templates/content/archive', 'cpt' );
}
} else {
get_template_part( 'templates/content/archive', 'posts' );
};
?>
<?php endwhile; ?>
<?php get_template_part( 'templates/modules/nav', 'pagination' ); ?>
However, this just displays most recent to oldest. With custom posts, woocommerce products, posts, pages, all mixed togather.
I want only products to be displayed, then once all matching products have been displayed, anything else that matches the search query is returned, eg: other custom post types, pages, and posts.
Any suggestions on how to achieve this?
Thanks.
Copy and paste the following code in functions.php file of your theme.
add_filter('posts_orderby','search_sort_custom',10,2);
function search_sort_custom( $orderby, $query )
{
global $wpdb;
if(!is_admin() && is_search())
$orderby = $wpdb->prefix."posts.post_type DESC, {$wpdb->prefix}posts.post_date DESC";
return $orderby;
}

Show monthly archive of only one category (Wordpress)

On my blog's archive page if I click on a month, it takes me to a page showing me all the posts that I've created that month (obviously). Is there a way to filter that page so it only shows me posts from one of my categories?
archive.php
<?php if ( have_posts() ) : ?>
<div class="rightColumn">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
get_template_part( 'content', 'none' );
endif;
?>
</div>
<?php
get_footer();
Thanks.
I ended up finding a solution that worked for me:
function only_show_blog_posts( $query ) {
// Only modify the main loop query
// on the front end
if ( $query->is_main_query() && ! is_admin() ) {
// Only modify date-based archives
if ( is_date() ) {
// Only display posts from category ID 1
$query->set( 'cat', '12' );
}
}
}
add_action( 'pre_get_posts', 'only_show_blog_posts' );
try using the pre_get_posts hook, something along the lines of:
function filter_by_category( $query ) {
if ( $query->is_archive() && $query->is_main_query() && basename( $_SERVER['PHP_SELF'] ) == 'archive.php' ) {
$category_id = get_cat_ID( 'THE_CATEGORY_NAME' ); //change to the actual name of the category you are filtering with
$query->set( 'cat', $category_id );
}
}
add_action( 'pre_get_posts', 'filter_by_category' );
you can drop this code into your functions.php file
you can find more info about the pre_get_posts hook here
That simple hook helped me too. I modified the function a little to get the category Id from $_GET:
function only_show_blog_posts( $query ) {
if ( $query->is_main_query() && ! is_admin() ) {
$catId = (int)$_GET['catId'];
if ( is_date() && is_int($catId))
$query->set( 'cat', $catId);
}
}
No filters or hooks necessary. Just pass the category you want to filter for in the URL.
With an ID
https://myblog.com/2018/?cat=1234
With a slug
https://myblog.com/2018/?category_name=my-category-slug

Resources