Why is my post being created inside another post? [ Wordpress ] - wordpress

I know there is probably a solved question like this already, but I can't seem to find it.
I am just starting out so this may be a noob question, please bear with me ><
I am trying to display the top 3 recent posts on front-page.php of my site, the problem is that the post keeps being created within itself.
Here is the CSS involved.
.news-container{ background-color: #fff;
padding: 2em;
max-width: 600px;
heigth:300px;
text-align: left;
}
.has-thumbnail {
position: relative;
padding-left: 200px;
}
.post-thumbnail {
position:absolute;
top:0;
left:0;
}
article.post { border-bottom: 1px solid #bbbbbb;
margin-bottom: 3em;
}
article.post:last-of-type { border-bottom: none;
}
Here is the front-page.php code involved.
<div class="news-container">
<?php
$recentposts=get_posts('showposts=5');
if ($recentposts) {
foreach($recentposts as $post) {
setup_postdata($post);
?>
<article class="post <?php if ( has_post_thumbnail() ) { ?> has-thumbnail <?php } ?> ">
<div class="post-thumbnail">
<?php the_post_thumbnail('small-thumbnail'); ?>
</div>
<p><?php the_time('F j, Y'); ?>
<h3><?php the_title()?> </h3>
<p>
<?php echo get_the_excerpt(); ?>
Read more »
<br><br> <br><br> <br><br>
</p>
<?php
}
}
?>
</article>
</div>
Why is this happening? Thanks for reading this!

Try this updated code i think you not close all tags properly. This will solve your problem.
<div class="news-container">
<?php
$args = array('posts_per_page' => 5, 'post_type'=>'post' );
query_posts($args); $post_query = new WP_Query( $args );
while($post_query->have_posts()){ $post_query->the_post();
?>
<article class="post <?php if ( has_post_thumbnail() ) { ?> has-thumbnail <?php } ?> ">
<div class="post-thumbnail">
<?php the_post_thumbnail('small-thumbnail'); ?>
</div>
<p><?php the_time('F j, Y'); ?> </p>
<h3><?php the_title()?> </h3>
<p><?php echo get_the_excerpt(); ?>
Read more »
<br><br> <br><br> <br><br>
</p>
</article>
<?php } ?>
</div>

You can accomplish this with the WP Latest Posts Plugin: https://wordpress.org/plugins/wp-latest-posts/screenshots/

Related

Running the loop outside of wordpress

I know that this has been posted many times, but I have truly spent close to 60 hours trying to figure this out - this is not my strong point!
Im running wordpress, standard installation, no plugins or anything at this stage.
I am simply trying to display the most recent 3 posts from the blog, or if we could go one step further, most recent 3 from catx.
Here is my code, which I believe should work.
<?php
require '/home1/digita/public_html/articles/wp-load.php'; ?>
<section class="services blog sec-normal">
<div class="container">
<div class="service-wrap">
<div class="row">
<style>.wp-block-image {
max-width: 100%;
margin-bottom: 1em;
display: none;
}
a.more-link {
display: none;
}
.services .service-wrap .service-section a {
margin-top: 0px!important;
}
</style>
<?php if (have_posts()) :
while ( have_posts() ) : the_post();
?> <!-- ***** BLOG ***** -->
<div class="col-md-4" style="width: 30%;margin-top: -7%;margin-bottom: 10%;">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<div class="service-section m-0" style="margin-top: 0.1%;">
<div class="title mt-0"><?php the_title(); ?></div>
<p class="subtitle"><?php the_content() ?> </p>
<hr>
Read more
</div>
</div>
<?php
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
?>
<p>Sorry no posts matched your criteria.</p>
<?php
endif;
?>
</div>
</div>
</div>
</div>
</section>
The code does not return posts, and instead returns, "Sorry no posts matched your criteria." . Any advice?
Main query does not set it's data, because wp function did not run after wp-load.php in your code and main query must be set base on $_SERVER['REQUEST_URI'] that is a few complex.
But its good to use secondary query as simple way for your work:
<?php
require '/home1/digita/public_html/articles/wp-load.php';
$query = new WP_Query(array(
'post_type' => 'post',
));
?>
<section class="services blog sec-normal">
<div class="container">
<div class="service-wrap">
<div class="row">
<style>.wp-block-image {
max-width: 100%;
margin-bottom: 1em;
display: none;
}
a.more-link {
display: none;
}
.services .service-wrap .service-section a {
margin-top: 0px!important;
}
</style>
<?php if ($query->have_posts()) :
while ( $query->have_posts() ) : $query->the_post();
?> <!-- ***** BLOG ***** -->
<div class="col-md-4" style="width: 30%;margin-top: -7%;margin-bottom: 10%;">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<div class="service-section m-0" style="margin-top: 0.1%;">
<div class="title mt-0"><?php the_title(); ?></div>
<p class="subtitle"><?php the_content() ?> </p>
<hr>
Read more
</div>
</div>
<?php
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
?>
<p>Sorry no posts matched your criteria.</p>
<?php
endif;
?>
</div>
</div>
</div>
</div>
</section>

Display WordPress Post Content in Multiple Columns

I would like to display my Wordpress posts in two columns instead of one.
I'm using Bootstrap and Advanced Custom Fields. What I have works but the posts are repeated in both of the col-lg-5 divs.
<div class="container">
<?php // Display blog posts on any page # https://m0n.co/l
$temp = $wp_query; $wp_quersy= null;
$wp_query = new WP_Query(); $wp_query->query('posts_per_page=-1' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="row">
<div class="col-lg-2">
<?php the_category(', '); ?>
</div>
<div class="col-lg-5 left">
<?php the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</div>
<div class="col-lg-5">
<?php the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<?php } else { ?>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
What you need to do is use columns in CSS.
Remove the additional column in your PHP file.
Apply the following CSS
.container {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
-moz-column-gap: 20px;
-webkit-column-gap: 20px;
column-gap: 20px;
}
.col-lg-5 {
padding-bottom: 20px;
width: auto;
-webkit-column-break-inside: avoid;
-moz-column-break-inside: avoid;
-moz-page-break-inside: avoid;
page-break-inside: avoid;
break-inside: avoid-column;
}
You can try the following:
<div class="container">
<div class="row">
<?php // Display blog posts on any page # https://m0n.co/l
$temp = $wp_query; $wp_quersy= null;
$wp_query = new WP_Query(); $wp_query->query('posts_per_page=-1' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="col-lg-2">
<?php the_category(', '); ?>
</div>
<div class="col-lg-5">
<?php the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php if ($paged > 1) { ?>
<?php } else { ?>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>

How to style last-child: exerpt in wordpress loop?

I've seen this article but it's not exactly relevant to what I'm trying to do. I have a simple blog loop in which each blog has an <div class="exerpt">for the preview of the blog text. All of my posts have a border-bottom and I'm simply trying to get rid of the border-bottom, as well as make other adjustments for the last-child. However every element is being styled, not just the last one. Please note I'm using the mighty html5blank
index.php
<div class="page-section" style="padding-top:150px;">
<div class="wrapper">
<?php get_template_part('loop'); ?>
<?php get_template_part('pagination'); ?>
</div>
</div>
loop.php
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="journal-title">
<?php the_title(); ?>
</h2>
<span class="date"><?php the_time('F j, Y'); ?> </span>
<!-- post thumbnail -->
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); // Declare pixel size you need inside the array ?>
</a>
<?php endif; ?>
<!-- /post thumbnail -->
<!-- post title -->
<!-- /post title -->
<div class="exerpt">
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
</div>
</article>
.exerpt {
margin-bottom:80px;
border-bottom:1px solid #999;
padding:40px 0px 80px 0px;
}
.exerpt:last-child {
border:none;
margin-bottom: 60px;
}
Thanks!
You can add a separate class for the last item in the loop by doing something like this:
First add this before the loop starts:
<?php $post_counter = 0; ?>
Add this within the loop:
<?php $post_counter++; ?>
Then modify your excerpt code:
<div class="exerpt <?php if( $post_counter == count( $posts ) ) echo 'last-post'?>">
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
</div>
CSS:
.last-post {
border:none;
margin-bottom: 60px;
}

Wordpress sidebar goes wrong way in bootstrap

Basically, i split up the page in 2 columns, and tried inserting sidebar code into the right one (span4). But for some reason, it keeps showing under span8
page code
<div class="row pull-right"><div class="span12" id="hdimg"></div></div>
<div class="row pull-right" id="pgg">
<div class="span8" id="pagecn">
<?php the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
<div class="ttl">
<?php if ( is_singular() ) {echo '<h1 class="entry-title">';} else {echo '<h2 class="entry-title">';} ?><a title="<?php printf( __('Read %s', 'blankslate'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a><?php if ( is_singular() ) {echo '</h1>';} else {echo '</h2>';} ?>
</div>
<div id="cn">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
<div class="span4">
<?php if ( is_active_sidebar('primary-widget-area') ) : ?>
<div id="primary" class="widget-area">
<ul class="sid">
<?php dynamic_sidebar('primary-widget-area'); ?>
</ul>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php get_footer(); ?>
css for it
#hdimg {
width: 948px;
height:185px;
background-image: url(images/banner.jpg);
}
#pgg {
width:948px;
margin-top:4px;
background-color: white;
padding-top: 10px;
padding-bottom: 20px;
}
#cn {margin-top: 15px;}
link to see live
http://soloveich.com/project3/
#pgg {
width:948px;
margin-top:4px;
background-color: white;
padding-top: 10px;
padding-bottom: 20px;
}
Remove width
#pgg {
margin-top:4px;
background-color: white;
padding-top: 10px;
padding-bottom: 20px;
}
I see what you tried to do but you used it incorrectly - fix background in a different way
<div class="" id="hdimg"></div>
This is for your header - remove grid class
change #hdimg width to
hdimg { width: 960px !important; }

First post in Wordpress blog displays incorrectly

I am styling a wordpress theme and the top blog post always displays incorrectly. The code is here:
<div class="bordered centered">
<p class="negative-margin alignleft header"><?php the_title(); ?></p><p class="negative-margin alignright date"><?php the_date(); ?></p><div style="clear:both;"></div>
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php if ( has_post_thumbnail() ):?>
<div id="entry-left"><?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail($post->ID, 'large');
echo '</a>';?>
</div>
<div class="entry-right">
<?php the_content( __( 'More <span class="meta-nav">→</span>', 'huckleberry' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'huckleberry' ), 'after' => '</div>' ) ); ?>
</div>
<div class="entry-right">
<table>
<tr>
<td><p class="header">Details</p><br />
<?php $my_meta = get_post_meta( $post->ID, 'link', false ); ?>
<?php if ($my_meta): var_dump($my_meta) ?>
<?php foreach ($my_meta as $currMeta): ?>
<?php if( $currMeta && '' != $currMeta ): $currMeta=explode($currMeta,";",2);?>
<?php $my_meta[0];?>
<?php endif; endforeach; endif; ?>
</td>
<td>
<a href="<?php echo MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image');?>">
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); endif; ?>
</a>
</td>
<td>
<a href="<?php echo MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'tertiary-image');?>">
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'tertiary-image'); endif; ?>
</a>
</td>
<td>
<a href="<?php echo MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'fourth-image');?>">
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'fourth-image'); endif; ?>
</a>
<td>
</tr>
</table>
</div>
with the relevant CSS here:
.alignleft {
float:left;
}
.alignright {
float:right;
}
.bordered {
border:2px solid black;
}
.negative-margin {
margin-top:-25px;
margin-left:5px;
background:white;
}
.header {
font-size:200%;
}
.date {
font-size:150%;
}
.centered {
width:640px;
margin-left:auto;
margin-right:auto;
}
#entry-left {
float:left;
width:200px;
}
.entry-right {
float:right;
width:430px;
}
All the other posts have the border, with the title and date on the border, but the top post has a border that goes over the header, like so: http://imgur.com/6pE79ro
I'm relatively new to Wordpress and can't seem to figure out what's going on here.
Also, I'm trying to use Custom Fields to display links under "details." The idea is to have the key for the links always be "link", and the value to be in the format "description;url" so that I can break the value into two values to have a link description and URL display. I'm unable to get that to actually display any links. Any help would be greatly appreciated.
Perhaps irrelevant, but where do you close the following divs? <div class="bordered centered"> and <div id="entry-left">?
This is not exactly answering your second questions, but as for custom fields: I would suggest using a great WP module - ACF. It allows you to create and read values of custom-created fields easily, coming with many more useful features. The plugin can be found here: http://wordpress.org/extend/plugins/advanced-custom-fields/

Resources