I have found this code:
<?php
$myposts = get_posts('');
foreach($myposts as $post) :
setup_postdata($post);
?>
<div class="post-item">
<div class="post-info">
<h2 class="post-title">
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<p class="post-meta">Posted by <?php the_author(); ?></p>
</div>
<div class="post-content">
<?php the_content(); ?>
</div>
</div>
<?php comments_template(); ?>
<?php endforeach; wp_reset_postdata();
?>
Is it good way to display posts? Because it shows, but if I click some post, it shows again all posts but with comments..Is it possible to show only that topic which I have clicked?
(or if you have some better code to show posts, please write it here)
Related
I'm wanting to display Wordpress posts in a two column row using Bootstrap. Here's what I have, i feel it's close but the second post is displaying underneath the first one insead of displaying next to each other.
<div class="container">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<?php if (have_posts()) : ?>
<?php 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; ?>
<h2 class="blog-post-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</div>
Any help would be appreciated!
Solved!
<div class="container">
<div class="row">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-lg-6 col-md col-sm-12 col-xs-12">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<h2 class="blog-post-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
</div>
<?php endwhile; else: ?>
<p>There are no posts to show</p>
<?php endif; ?>
</div>
</div>
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:.
I have the following code (inherited, I didn't write it) and I'm struggling to understand why it's only bringing back the 4 most recent posts instead of all of them. That being said, I don't WANT all at once, but I want to be able to control how many come back.
Additionally, I'm going to need to write a function with a link at the bottom of this that says "Load more" and, when clicked, will bring back (via AJAX) the next four (or however many) posts to render immediately beneath the already displayed ones. How would I go about that?
Thanks!
<?php if (have_posts()): ?>
<div class="postSection">
<?php
while (have_posts()) : the_post();
$pdf_download = get_post_meta($post->ID, 'pdf_download', true);
$pdf_label = get_post_meta($post->ID, 'pdf_label', true);
?>
<div class='postBox <?php foreach ((get_the_category()) as $cats) {
echo $cats->slug . ' ';
} ?>'>
<div class="postImage">
<a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<div class="postContent">
<h3><a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h3>
<div class="postExcerpt">
<p><?php the_excerpt_rss(); ?> <a class="readMore"
href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>">Read
More</a></p>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif;
I've searched and searched and have not solved my problem. I seems like there are solutions but they are not working for my particular code.
What I would like to do is to have two loops - one that calls up the most recent post and acts as the main "featured post". Second is simply the second most recent post that is styled differently - then below the second or "sub post" I would like pagination.
Problem is... the pagination does not work. The numbers show up, but when I click on them nothing happens. Would appreciate any help!
Here is the code...
<div id="featuredpost">
<?php $my_query = new WP_Query('posts_per_page=1&cat=4');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<span class="subposttitle"><?php the_title(); ?></span><br>
<small>Posted in <?php the_category(', ') ?> on <?php the_time('l, F jS') ?>.</small>
<br><br>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<br>
<hr>
<br><br>
<?php endwhile; ?>
</div>
<div id="subposts">
<?php $my_query = new WP_Query('posts_per_page=1&offset=1&cat=4');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<span class="subposttitle"><?php the_title(); ?></span><br>
<small>Posted in <?php the_category(', ') ?> on <?php the_time('l, F jS') ?>.</small>
<br><br>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<br>
<hr>
<br>
<?php endwhile; ?>
<center>
<?php wp_pagenavi() ?>
</center>
</div>
pass multiple properties to wp_query as an array:
<?php $wp_query = new WP_Query(array('posts_per_page' => 1, 'cat' => 4));
I am using a nice jquery slideshow plugin I found and trying to get it to work into my Wordpress template. I have tried the code below in various formats but I can't seem to get it the way I want.
The first part is where the title and content of the post reads into the slider, using a specific category. I have 3 of these sections:
<div class="details_wrapper">
<div class="details">
<div class="detail">
<?php query_posts('cat_ID=7&showposts=1');
if (have_posts()) : while (have_posts()) : the_post();?>
<h2 class="Lexia-Bold"><a href="<?php the_permalink() ?>">
<?php the_title() ?></a><?php the_excerpt(); ?></h2>
<?php endwhile; endif;
?>
</div><!-- /detail -->
<div class="detail">
<?php query_posts('cat_ID=8&showposts=1');
if (have_posts()) : while (have_posts()) : the_post();?>
<h2 class="Lexia-Bold"><a href="<?php the_permalink() ?>">
<?php the_title() ?></a><?php the_excerpt(); ?></h2>
<?php endwhile; endif;
?>
</div><!-- /detail -->
<div class="detail">
<?php query_posts('cat_ID=9&showposts=1');
if (have_posts()) : while (have_posts()) : the_post();?>
<h2 class="Lexia-Bold"><a href="<?php the_permalink() ?>">
<?php the_title() ?></a><?php the_excerpt(); ?></h2>
<?php endwhile; endif;
?>
</div><!-- /detail -->
</div><!-- /details -->
</div>
Now this actually works, but I just need it to post the title and excerpt from one of the posts from the category noted. I was reading that I may need to add the wp_reset_query(); line somewhere to destroy the previous loop's query, but I'm not sure.
Here's the second part of the code where the post's featured image is retrieved:
<div class="item item_1">
<?php query_posts('cat_ID=7&posts_per_page=1'); ?>
<?php the_post_thumbnail( 'single-post-thumbnail' ); ?>
</div><!-- /item -->
<div class="item item_2">
<?php query_posts('cat_ID=8&posts_per_page=1'); ?>
<?php the_post_thumbnail( 'single-post-thumbnail' ); ?>
</div><!-- /item -->
<div class="item item_3">
<?php query_posts('cat_ID=9&posts_per_page=1'); ?>
<?php the_post_thumbnail( 'single-post-thumbnail' ); ?>
</div>
Any help would be greatly appreciated :) Here's an example.
Have you tried using...
query_posts('cat_ID=9&posts_per_page=1');
Or I have used get_post before as while to get certain amount of post like so...
<?php
global $post;
$myposts = get_posts('posts_per_page=1&numberposts=-1&category=1');
foreach($myposts as $post) :
?>
<h6><?php the_title(); ?></h6>
<?php setup_postdata($post);?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
</div>
If its just one post you want you wouldn't really need the foreach or while loop.
I used the method Tianbo84 suggested above both to query the posts AND the featured images from that post to finish the job :) Thanks Tianbo84! To my understanding, the get_posts and <?php endforeach; ?> lines were key... like opening a query and then closing it once the data was retrieved.