How to Get Posts in Multiple Places in Wordpress theme? - wordpress

I'm really newbie to wordpress theme development and I want to get recent 10 posts inside front-page.php and all posts inside the index.php (I have no idea if a better way is there) followed by pagination.
Update : I want to have a home page that shows 10 posts and a articles page that shows all posts.
Is this approach right? if it is, how do I do that?

after some research I found a solution to do that.
at first I created 2 pages called home and articles, then I changed Setting -> Reading :
Front Page -> home
and
Posts Page -> articles.
then I put the following in the index.php as articles page :
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
then inside of front-page.php :
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 10)); ?>
<?php if ($wpb_all_query->have_posts()) : ?>
<!-- the loop -->
<?php while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post(); ?>
<div class="post">
<div class="col-xs-12 col-lg-6">
<div class="panel panel-default ">
<div class="panel-body">
<div class="col-xs-4">
<a href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail()) : ?>
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<?php else: ?>
<img
src="<?php bloginfo('template_directory'); ?>/assets/image/placeholder.jpg"
alt="">
<?php endif; ?>
</a>
</div>
<div class="col-xs-8">
<div class="title">
<a href="<?php the_permalink(); ?>">
<h2><?php the_title(); ?></h2>
</a>
</div>
<div class="content">
<p><?php echo substr(get_the_content(), 0, 320); ?>...</p></div>
<div class="read-more">
Read More
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
finally it works in the way exactly what I wanted.

Related

Wordpress hierarchy issue with categorized post not using specific template

I'm trying to implement a dedicated template for any post with a category slug of 'rentals'. There are posts in the database with a category of rentals set up and applied. I've created the template with a standard loop and called it category-rentals.php, saving it in my root folder.
It's just not working. It keeps reverting to single post template (index.php specifically).
My rendered body classes are as such:
post-template-default single single-post postid-278 single-format-standard
Am I missing a step here?
category-rentals.php:
/*
* Rental property template
*/
?>
<?php get_header(); ?>
<div class="jumbotron jumbotron-fluid" style="background-image: url('<?php bloginfo( 'template_url' ); ?>/img/bg-semi-trans.png'), url('<?php bloginfo( 'template_url' ); ?>/img/bg-jumbotron.jpg');">
<div class="container">
<div class="headline text-center animated fadeInUp">
<p><?php wp_title(''); ?></p>
</div>
</div>
</div>
<section>
<div class="container">
<div class="row">
<div class="col-12">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php esc_html_e( 'Sorry, no content matched your criteria.' ); ?></p>
<?php endif; ?>
<hr>
← Go Back | Contact Us
</div>
</div>
</div>
</section>
<?php get_footer(); ?>

<?php wp_link_pages(); ?> is working on posts but not on pages to display pagination

I created a theme from scratch and have pagination working on the post pages using , it's working on single.php.
<?php get_header(); ?>
<?php
while(have_posts()) {
the_post();
?>
<div class="mainConent">
<div class="leftSidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('left_sidebar') ?>
</div>
</div>
<div class="recentBlogsWrapper">
<div class="blogWrapper">
<h2><?php the_title(); ?></h2>
<p><?php the_time('F j, Y') ?></p>
<?php if(has_post_thumbnail()) { ?>
<div class="card-image">
<img class="page-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card Image">
</div>
<?php } ?>
<div class="card-description">
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php comments_template(); ?>
<?php } ?>
<div class="backarrowwrap">
<a class="backhomelink" href="<?php echo site_url(); ?>" <?php if(is_front_page()) echo 'class="active"' ?>>
<img class="backarrow" src="<?php echo get_template_directory_uri(); ?>/img/backarrow.png" alt="back arrow" />
Go Back Home
</a>
</div>
</div>
</div>
</div>
<div class="rightSidebar" id="sidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('right_sidebar') ?>
</div>
</div>
I put it right below the_content(); and it's working exactly like I want it to. However, it's not working on page.php, even though it's in the same place on the page.
<?php get_header();
while(have_posts()) {
the_post();
?>
<div class="pageWrapper">
<h2><?php the_title(); ?></h2>
<?php if(has_post_thumbnail()) { ?>
<div class="card-image">
<img class="page-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card Image">
</div>
<?php } ?>
<div class="card-description">
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php } ?>
</div>
</div>
<?php get_footer(); ?>
I've included the
<!–-nextpage-–>
code in one of my pages just like I did for the blogs, but it's not paginating between the paragraphs.
What am I doing wrong?
You need at least one <!--nextpage-->, if the content of a page (or post) has at least one <!--nextpage--> tag (and this code is in The Loop), this prints linked page numbers (“Pages: 1 2 3 4 and so on...”), without a link on current page number, and by default within tags:.

<! --nextpage--> pagination is not working in Wordpress

I'm trying to make it so posts can be easily paginated in a custom theme, but I'm doing something wrong. I've included wp_link_pages(); in the while like which I believe is required, but maybe I have it in the wrong place or I'm missing something else?
<?php get_header(); ?>
<?php
while(have_posts()) {
the_post();
wp_link_pages();
?>
<div class="mainConent">
<div class="leftSidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('left_sidebar') ?>
</div>
</div>
<div class="recentBlogsWrapper">
<div class="blogWrapper">
<h2><?php the_title(); ?></h2>
<p><?php the_time('F j, Y') ?></p>
<?php if(has_post_thumbnail()) { ?>
<div class="card-image">
<img class="page-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card Image">
</div>
<?php } ?>
<div class="card-description">
<?php the_content(); ?>
<?php comments_template(); ?>
<?php } ?>
<div class="backarrowwrap">
<a class="backhomelink" href="<?php echo site_url(); ?>" <?php if(is_front_page()) echo 'class="active"' ?>>
<img class="backarrow" src="<?php echo get_template_directory_uri(); ?>/img/backarrow.png" alt="back arrow" />
Go Back Home
</a>
</div>
</div>
</div>
</div>
<div class="rightSidebar" id="sidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('right_sidebar') ?>
</div>
</div>
<?php get_footer(); ?>
Another odd thing that's happening is that the code block is showing the code on the page instead of hiding it like it should.
The wordpress editor
My blog post page which shows the code
Gutenburg was preventing me from writing the comment as code, even in a code box. Once I switched to the classic view the problem went away since I could write the comment in the html directly.

Why isn't my single post template showing up correctly?

I'm trying to figure out how to create a custom single post template on wordpress. I'm not very familiar with it but was required to use it.
I've followed the steps required to create it but it seems that my new post template called educators is still referencing the original post template instead of the one i've created for it.
Here is my single-post_educator.php file code.
<?php get_header(); ?>
<div class="container-fluid">
<img src="<?php the_field('post_banner','option');?>" style="width :100%;">
<div class="container">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<?php if( get_field('post_content_right') ) { ?>
<div class="post-right col-sm-6 col-xs-12">
<h2><?php echo get_the_title();?></h2>
<p><?php echo the_field('post_content');?></p>
</div>
<div class="col-sm-6 col-xs-12 post-right">
<a href="<?php echo home_url();?>/#educator" style="font-size:
15px;color:#000000;float:right;font-family: 'calibri';text-
decoration: underline;text-align:right;">Back</a>
<p><?php echo the_field('post_content_right');?></p>
</div>
<?php } else { ; ?>
<div class="post-right col-sm-6 col-xs-12">
<a href="<?php echo home_url();?>/#educator" style="font-size:
15px;color:#000000;float:right;font-family: 'calibri';text-
decoration:underline;text-align:right;">Back</a>
<h2><?php echo get_the_title();?></h2>
<p><?php echo the_field('post_content');?></p>
</div>
<?php } ;?>
<?php
endwhile;
endif; ?>
</div>
</div>
<?php get_footer(); ?>
I included a photo of how it currently is and how i actually want it to be like:
Thanks in advance.. help is much appreciated
The file should be named:
single-{post_type}.php
So in your case it should be
single-post_educators.php
You're missing the s

WordPress custom post type pagination on Zylyz theme

Sorry for asking in my first question, but I have a big problem with my theme and nobody could solve it. Actually it's so simple:
I am using Zylyz recipe theme. I set the home page to a custom post type (recipes), but as you might guess, when I press the "Older Entries" button it gives "not found" error, because it tries to get ordinary blog posts, not "recipes" (you know, if I had enough blog post it wouldn't give error, but would show posts, not recipes).
So, how can I get rid of this problem?
Thank you very much in advance.
Here's the home page's codes:
<div id="content">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=recipes'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post clearfix" id="post-<?php the_ID(); ?>">
<?php
if ( has_post_thumbnail() ) { ?>
<img class="postimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=200&w=200&zc=1" alt="<?php the_title(); ?> Recipe"/>
<?php } else { ?>
<img class="postimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" />
<?php } ?>
<div class="cover">
<div class="title">
<h2><?php the_title(); ?></h2>
</div>
<div class="recipemeta">
<span class="cooktime"> <strong>ready in</strong> <?php $cooktime=get_post_meta($post->ID, 'wtf_cooktime', true); echo $cooktime; ?> mins </span> <span class="serve"> <strong>Serving:</strong> <?php $serving=get_post_meta($post->ID, 'wtf_serving', true); echo $serving; ?> people</span>
</div>
<div class="entry" align="justify">
<?php wpe_excerpt('wpe_excerptlength_recipe', ''); ?>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>

Resources