I'm trying to display the title, image, exterp and category of the latest post on top of a blog page so it stands out. I alredy have css styles in place to make it look right I just need to show the right elements in the right blocks. The image will be the background of the second column.
I came up with this code but it gives me critical WP errors. Could you point me in the right direction / help me correct my mistake please.
Here is the code:
function test() {
$args = array(
'posts_per_page' => 1,
'nopaging' => true
);
// set up new query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<a href="' . get_permalink() . '">';
echo '<h5 class="uppercase regular">' . get_cat_name() . '</h5></a>';
echo '</div>';
echo '<div class="is-divider divider clearfix"></div>';
echo '<div class="text">';
echo '<h2 class="m-font-size">' . get_the_title() . '</h2><p class="light">' . get_the_excerpt() . '</p>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="col medium-6 large-6">';
echo '<div class="col-inner">';
echo '<div class="banner has-hover bnr-bl">';
echo '<div class="banner-inner fill">';
echo '<div class="banner-bg fill">';
echo '<div class="bg fill bg-fill bg-loaded" style="background-image: url("' . get_the_post_thumbnail_url() . '");">';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
endwhile;
// reset post data
wp_reset_postdata();
}
add_shortcode( 'test', 'test' );
It slides in like this:
<div class="col medium-6 large-6">
<div class="col-inner">
<div class="banner has-hover">
<div class="banner-inner fill">
<div class="banner-bg fill">
<div class="bg fill bg-fill bg-loaded"></div>
</div>
<div class="banner-layers container">
<div class="fill banner-link"></div>
<div class="text-box banner-layer x50 md-x50 lg-x50 y50 md-y50 lg-y50 res-text">
<div class="text ">
<div class="text-inner text-center">
<div class="text">
[test]
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Shortcode callback must return not echo
https://developer.wordpress.org/reference/functions/add_shortcode/
Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode.
So instead of echo you need to collect your post into some variable and return it
function test() {
$html = '';
$args = array(
'posts_per_page' => 1,
'nopaging' => true
);
// set up new query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$html .= '<a href="' . get_permalink() . '">';
$html .= '<h5 class="uppercase regular">' . get_cat_name() . '</h5></a>';
// ....and so on
$html .= '</div>';
endwhile;
// reset post data
wp_reset_postdata();
return $html;
}
add_shortcode( 'test', 'test' );
Related
I am having issues in looping custom post type using while loop. Yes, I am display data without error but the_ID() function is being printed outside specific id i.e id="panelsStayOpen-heading'.the_ID().'" as shown in an image.
issue
function displayFaqs() {
$args = array(
'post_type' => 'advance_faq',
'posts_per_page' => '-1',
'post_status' => 'publish'
);
// The Query
$the_query = new WP_Query( $args );
ob_start();
// The Loop
if ( $the_query->have_posts() ) {
echo '<div class="accordion" id="accordionPanelsStayOpenExample">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading'.the_ID().'">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse'.the_ID().'" aria-expanded="true" aria-controls="panelsStayOpen-collapse'.the_ID().'">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse'.the_ID().'" class="accordion-collapse collapse show" aria-labelledby="panelsStayOpen-heading'.the_ID().'">
<div class="accordion-body">
'
.the_content() . '
</div>
</div>';
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
add_action( 'init', 'a_register_shortcodes');
How do I fix the issues to print in a specific id without printing outside
Replace the_ID() with get_the_ID() because you are already using echo.
the_ID() used to print out the post_id directly and get_the_ID() returns the id.
Below is my code to display recent posts from blog in html/php page
<div class="row">
<?php
include('blog/wp-load.php');
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3,
'post_type' => 'post',
'post_status' => 'publish'
));
foreach($recent_posts as $post) {
echo '<div class="col-lg-4">';
echo '<div class="blog-entry">';
echo '';
echo '<div class="text">';
echo '<div class="meta">';
echo '<div><span class="fa fa-calendar-alt"></span>', $post['post_date'], '</a></div>';
echo '<div><span class="fa fa-user"></span>', $post['post_author'], '</div>';
echo '<div><span class="fa fa-comments meta-chat"></span>', $post['comment_count'], '</div>';
echo '</div>';
echo '<div class="desc">';
echo '<h3 class="heading">', $post['post_title'], '</h3>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
</div>
How can I display post image using background-image: url(images/BlogImg.png);
In place of images/BlogImg.png I want post image url of respective blog post.
Can anyone please tell me how can I achieve this?
Use the following lines:
<div class="row">
<?PHP
include('blog/wp-load.php');
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3,
'post_type' => 'post',
'post_status' => 'publish'
));
foreach($recent_posts as $post) {
$imgurl = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
echo '<div class="col-lg-4">
<div class="blog-entry">
<div class="text">
<div class="meta">
<div><span class="fa fa-calendar-alt"></span>' . $post['post_date'] . '</a></div>
<div><span class="fa fa-user"></span>'. $post['post_author'] . '</div>
<div><span class="fa fa-comments meta-chat"></span>' . $post['comment_count'] . '</div>'
</div>
<div class="desc">
<h3 class="heading">' . $post['post_title'] . '</h3>
</div>
</div>
</div>
</div>';
}
?>
</div>
Depending how you have the image stored, if this is a featured image attached to your posts add this
$backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full' );
foreach($recent_posts as $post) {
$backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post['ID']), 'full' );
echo '<div class="col-lg-4">';
echo '<div class="blog-entry">';
echo '';
echo '<div class="text">';
echo '<div class="meta">';
echo '<div><span class="fa fa-calendar-alt"></span>', $post['post_date'], '</a></div>';
echo '<div><span class="fa fa-user"></span>', $post['post_author'], '</div>';
echo '<div><span class="fa fa-comments meta-chat"></span>', $post['comment_count'], '</div>';
echo '</div>';
echo '<div class="desc">';
echo '<h3 class="heading">', $post['post_title'], '</h3>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
I want to implement an ajax post filter based on category and found this which works: https://rudrastyh.com/wordpress/ajax-post-filters.html
The only thing is that I would like the post output from function.php to be different. Originally it is just:
echo '<h2>' . $query->post->post_title . '</h2>';
but I want to use my own post loop configuration:
<div <?php post_class( 'front-post-small col-front' ); ?> id="post-<?php the_ID(); ?>">
<a href="<?php echo esc_url( the_permalink() ); ?>">
<div class="front-post-img">
<?php the_post_thumbnail( array(756,512) ); ?>
<div class="post-caption">
<h3 class="text-uppercase front-post-title"><?php the_title(); ?></h3>
</div>
</div>
</a>
</div>
I have tried passing it with echo, but it either doesn't work at all or the classes and links are printed instead of producing links and css.
Solved:
echo '<div class="front-post-small col-front">';
echo '<a href="' . get_permalink() . '">';
echo '<div class="front-post-img">';
echo '<img' . the_post_thumbnail( array(756,512) ) . '>';
echo '<h3 class="text-uppercase front-post-title post-caption">' . $query->post->post_title . '</h3>';
echo '</div>';
echo '</a>';
echo '</div>';
I am trying to display recent post in staic page home-content.php so i have added this code in function.php
here it is a code
function my_recent_posts_shortcode($atts){
$q = new WP_Query(
array( 'orderby' => 'date')
);
$list ="";
while($q->have_posts()) : $q->the_post();
echo '<div class="item">';
$title=get_the_title();
if ( has_post_thumbnail() ) {
echo '<a class="single-image link-icon" href="' . get_permalink() . '">';
$list .=the_post_thumbnail(array(300,200),array('alt' =>$title));
echo '</a>';
}
echo '<h6 class="title"><span>'.$title.'</span></h6>';
echo '<div class="entry-body">';
$list .= wpe_excerpt('wpe_excerptlength_index', '');
echo '<a class="button default color" href="' . get_permalink() . '">Read More</a>';
echo '</div>';
echo '</div>';
endwhile;
wp_reset_postdata();
return $list ;
}
add_shortcode('recent-posts', 'my_recent_posts_shortcode');
[recent-posts] this is a shortcode for display recent post
and home-content.php for showing post
<?php
$post_id = 7;
$queried_post = get_post($post_id);
?>
<p><?php $check=$queried_post->post_content; ?></p>
<?php echo do_shortcode('["'.$check.'"]');?>
All the recent post display on home page of my custom theme http://templategraphy.com/wp-demo/businessguru/
but the problem is theme structure is not properly shown i
want this type of structure is shown http://templategraphy.com/demo/businessguru/
suggest some solution where i am doing wrong.
I'm trying to wrap every 2 post in a div "row-fluid" Right now my HTML looks like the following...
<div class="row-fluid">
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
</div>
I'd like it to look like this...
<div class="row-fluid">
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
</div>
<div class="row-fluid">
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
</div>
This is the php that I'm using to generate this but I think because I'm counting outside of my loop its not rendering the way I'd like it too.
<?php
$count = 0;
if(have_posts()) : while(have_posts()) : the_post();
$open = !($count%2) ? '<div class="row-fluid">' : '';
$close = !($count%2) && $count ? '</div>' : '';
echo $close.$open;
?>
<!--Custom Post Type Boilerplate-->
<?php
$args = array( 'post_type' => 'mysite_team', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div ';
$even_odd = (++$j % 2 == 0) ? 'featured-image-wrapper-even-post span6' : 'featured-image-wrapper-odd-post span6'; post_class( $even_odd );
echo '>';
echo '<h1>';
echo '<a href="';
the_permalink();
echo '">';
the_title();
echo '</a></h1>';
echo '<div class="featured-image alignleft">';
echo '<a href="';
the_permalink();
echo '">';
the_post_thumbnail('team-thumbnail-size');
echo '</a>';
echo '</div>';
echo '<div class="entry-content">';
the_excerpt();
echo '<p><a href="';
the_permalink();
echo '"><i>Read More';
echo '</i></a></p>';
echo '</div>';
echo '</div>';
endwhile;
?>
<?php
$count++;
endwhile;
else :
?>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; ?>
Any help would be appreciated as I'm starting to see circle and get confused.
Thank you!
I would avoid the extra div container, sorry. You can solve this with pure CSS.
.row-fluid:nth-child(odd) {
// Some code
}
.row-fluid:nth-child(even) {
// Some code
}
.span6 {
width:50%;
display:inline-cell;
float: left;
}