-
AuthorPosts
-
November 6, 2014 at 6:03 pm #347212
Hey,
i wrote a shortcode function in the functions.php and want to display it in a 1/3 colum. The result is that the posts are displayed at the top of the page , outside the 1/3 column. What is wrong?
best regards,
leRenaar/* CALL NEWS-POST @ HOME */
function recent_posts_function() {
$args = array(‘cat’ => 1);
$category_posts = new WP_Query($args);if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<h1><?php the_title() ?></h1>
<div class=’post-content’><?php the_content() ?></div><?php
endwhile;
else:
?>
Oops, no news.
<?php
endif;
}function register_shortcodes(){
add_shortcode(‘recent-posts’, ‘recent_posts_function’);
}add_action( ‘init’, ‘register_shortcodes’);
November 7, 2014 at 12:33 am #347468Hey leRenaar!
You will need to return your output instead of echoing it out. Here is a good read on how to do that, https://wordpress.org/support/topic/shortcode-output-always-appearing-at-top-of-page-content.
Cheers!
ElliottNovember 7, 2014 at 12:28 pm #347682This reply has been marked as private.November 7, 2014 at 7:00 pm #347866Hey!
I did not test this but probably something like so.
/* CALL NEWS-POST @ HOME */ function recent_posts_function() { $args = array(‘cat’ => 1); $category_posts = new WP_Query($args); var $out = ''; if($category_posts->have_posts()) : while($category_posts->have_posts()) : $category_posts->the_post(); $out .= '<h1>'.get_the_title().'</h1>'; $out .= '<div class = "post-content">'.get_the_content().'</div>'; endwhile; else: $out = 'Oops, no news'; endif; return $out; } function register_shortcodes(){ add_shortcode(‘recent-posts’, ‘recent_posts_function’); } add_action( ‘init’, ‘register_shortcodes’);
This kind of customization is not included in our support though so if your still having problems then it would be best to hire a freelancer to code a shortcode for you.
Best regards,
Elliott- This reply was modified 10 years, 1 month ago by Elliott.
July 17, 2015 at 10:53 am #474785Hey,
the shortcode to display post from cat_1 is displaying nice.
I want to extent this code with a toggle function (is working) in combination with the read_more in the post content.
How can i check if the post has a excerpt?The code used:
/* CALL NEWS-POST @ HOME */
function recent_posts_function() {ob_start ();
$args = array(‘cat’ => 1, ‘posts_per_page’ => 4, ‘orderby’ => ‘post_date’);
$category_posts = new WP_Query($args);if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php
// Get $post if you’re inside a function
global $post;if ( has_excerpt() ) {
echo “this post HAS EXCERPT”;
} else {
echo “NO EXCERPT”;
}
?><ul class=’homepost’>
<div class=’date’><?php the_time(‘j F Y’); ?></div>
<h5><?php the_title() ?></h5>
<div class=”excerpt”><?php the_excerpt(); ?>Read More </div>
<?php global $more; // Declare global $more (before the loop). ?>
<?php $more = 1; // Set (inside the loop) to display all content, including text below more. ?>
<div class=”post-content”><?php the_content(); ?>Read Less</div>
<div class=’dividerpost’></div><?php
endwhile;
else:
?>
Oops, no news.
<?php
endif;$listpost = ob_get_clean();
return $listpost;
}function register_shortcodes(){
add_shortcode(‘recent-posts’, ‘recent_posts_function’);
}add_action( ‘init’, ‘register_shortcodes’);
/* END OF CODEBLOCK CALL NEWS-POST @ HOME */
best regards, leRenaar
July 17, 2015 at 1:32 pm #474921Seems you are already using has_excerpt.
Best regards,
JosueJuly 17, 2015 at 1:52 pm #474935Hey Josue,
the first two post are with the read-more (excerpt) in the post. The last 2 post (of 4) dont have a excerpt.
The result is all 4 the same: echo “no_excerpt”.Do you have any idea why this lines of code don’t recognize has__excerpt ?
best regards,
reinJuly 17, 2015 at 2:01 pm #474940Make sure the ID of the Post is passed to has_excerpt
has_excerpt( $post->ID )
July 17, 2015 at 4:15 pm #475036thx Josue, its working perfect ! good weekend, leRenaar
July 17, 2015 at 4:25 pm #475040You are welcome, glad to help :)
Regards,
Josue -
AuthorPosts
- You must be logged in to reply to this topic.