Wordpress: How to put 2 different featured posts on one page - php

hi I have a problem to put featured post without pagination and featured post with pagination in one page:
Here is the code the first one I use this:
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
if (have_posts()) : while (have_posts()) : the_post();
$postImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>
<div class="slider" style="background:url(<?php echo $postImage[0];?>) no-repeat center center">
<div class="slider-shadowbox"></div>
<a class="slider-hyperlink" href="<?php the_permalink(); ?>">
<div class="slider-title">
<div class="featured-post-author">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '100'); ?>
<?php the_author_posts_link(); ?>
</div>
<span class="slider-tagline"><?php the_title()?></span>
<em class="slider-author">By <?php the_author_posts_link(); ?></em>
<em class="slider-date"><i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></em>
</div>
</a>
</div>
<?php
endwhile;
endif;
?>
/and the second one I use this to create featured post with pagination, but somehow the pagination shows the numbers but the hyperlink for each number doesn't work properly./
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="featured-post">
<div class="zitem">
<?php the_post_thumbnail( array(360, 240));?>
</div>
<span class="featured-post-summary">
<h3><?php the_title(); ?></h3>
<?php echo get_excerpt(); ?>
</span>
<div class="featured-post-author-wrap">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '35'); ?>
<span class="featured-post-author">By <?php the_author_posts_link(); ?></span>
<span class="featured-post-date"><?php the_time('F j, Y'); ?><span>
</div>
</div>
<?php endwhile; ?>
<div class="featured-post-pagination pagination">
<div class="pagination-outer"><?php my_pagination(); ?></div>
</div>
<?php else : ?>
<strong>Sorry no post available.</strong>
<?php endif; ?>

Related

how to limit number of post for blog by same author

I have coded as below for Recent post by same author. Code is working fine and giving me recent post by same author, but it show more than one post (but I need 1 post to show)
<div class="popular-post-wrapper">
<h4 class="blog-post-sub-title">Most Recent</h4>
<div class="col-md-3 popular-post-thumbnail">
<?php
$query_posts=array(
'orderby' => 'display_name',
'order' => 'ASC',
'who' => 'authors',
'posts_per_page' => '1' );
if (have_posts()) : while (have_posts()) : the_post();
?>
<?php //if ( has_post_thumbnail() ) : ?>
<a href="<?php //the_permalink(); ?>" title="<?php //the_title_attribute(); ?>">
<?php //the_post_thumbnail(); ?>
</a>
<?php //endif; ?>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="https://kubalubra.is/wp-content/uploads/2017/11/default-thumbnail.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<div class="col-md-9 post-title-wrapper">
<h3> <a href="<?php the_permalink(); ?>">
<?php the_title();?>
</a></h3>
</div>
<?php
endwhile; endif;
wp_reset_query();
?>
</div>

How to place pagination in WordPress? I am trying similar code but it doesn't work

I am trying to paginate archived post in WordPress when I click "next button" but it doesn't work. Please guide me to do it in a simple way. My code is given below:
<div class="grid-wrapper ">
<?php $args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner-blog">
<div class="row">
<div class="col-lg-4 col-md-2">
<div class="post-thumbnail">
<img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
</div>
</div>
<div class="col-lg-8 col-md-9">
<h2 class="post-title"><?php the_title();?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php wp_reset_postdata(); ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?> »</li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
</div>
</div>

Pagination WordPress on the home page

I'm having an issue trying to get pagination to work on the homepage of a site I'm working on.
Here is the code I'm using, simplified of course:
How to do pagination for this?
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; endif; ?>
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
First of all, you must setup properly your query:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
And second, you need to add pagination links out of the loop
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
Whole code can looks like this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
Problem has been solved:
<?php
global $paged, $wp_query, $wp;
$args1 = array('cat' => '3, 7, 10, 12');
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=3&cat=3,7,10,12');
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>

WordPress Blog, only use archive for current page?

I am building a WP site, which will have a News and Blog.
They will be in separate pages, One for news and one for Blog, which will be separated by categories.
So for example, I have this code on 'News', which stops the loop getting posts:
<?php
$uncat = get_cat_ID('uncategorised');
$uncat2 = get_cat_ID('blog');
$args = array(
'posts_per_page' => 3,
'category__not_in' => array($uncat, $uncat2)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h4><?php the_title(); ?></h3>
<span class="entry-date-blue"><strong><?php echo get_the_date('d/m/y'); ?></strong></span>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> Read More ...' );
echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
</div>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
This works fine, but I also have 'Archives' on the right hand side, which filters by date posted. The issue is, this will get posts from News AND blog, which defeats the idea of splitting them up.
Is there a way to split these up, so if the user clicks 'March 2015' on the archive, it will only get the posts from this month from NEWS?
Here is my current code for Archive.php
<?php if (have_posts()) : ?>
<!-- First, the loop checks whether any posts were discovered with the have_posts() function. -->
<!-- If there were any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So, as long as the function have_posts() returns a true value, the while loop will keep looping (repeating). -->
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-12 col-sm-12 col-xs-12">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<h5 class="captext"><?php the_title(); ?></h5>
<span class="entry-date-orange"><?php echo get_the_date(); ?></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</a>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Try this , it will echo list
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
full example code
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php get_search_form(); ?>
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Display all the posts from category

I have a category.php with script inside:
<?php $categories = get_the_category(); ?>
<?php $category_id = $categories[0]->cat_ID; ?>
<?php query_posts('cat='.$category_id); ?>
<?php while (have_posts()) : ;?>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div class="article">
<div class="article_image" style="background-image: url('<?php echo $image[0]; ?>')">
<i class="fa article_favourite"><?php wpfp_link() ?></i>
</div>
<h3><?php the_title(); ?></h3>
<div class="article_subheading_wrapper">
<p>
<!--Cut the long text -->
<?php
$string = substr(get_field("subheading"), 0, 100);
echo $string."… ";
?>
</p>
</div>
<div class="bottom">
<ul>
<li><p id="views"><img id="eye" src="<?php bloginfo('template_directory'); ?>/icons/watched_icon.svg"> <span><?php if(function_exists('the_views')) { the_views(); } ?></span></p></li>
<li><p id="likes"><i class="fa"></i><?php if(function_exists('like_counter_p')) { like_counter_p(''); }
?></p></li>
<li><p id="comments"><i class="fa fa-comment"></i> <?php comments_number( '0', '1', '%' ); ?></p></li>
</ul>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
It supposed to be displaying all the posts that belong to this category. I does, but it displays only recent one and infinite number of times.
What do I do wrong?
PLEASE PLEASE PLEASE, don't use query_posts, it is evil. Rather use WP_Query
Try either of this,
get_posts('cat=3&posts_per_page=-1');
or
query_posts( array ( 'cat' => cat_id, 'posts_per_page' => -1 ) );
Hope this helps you

Categories