Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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’);

    #347468

    Hey 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!
    Elliott

    #347682
    This reply has been marked as private.
    #347866

    Hey!

    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 ago by Elliott.
    #474785

    Hey,

    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

    #474921

    Seems you are already using has_excerpt.

    Best regards,
    Josue

    #474935

    Hey 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,
    rein

    #474940

    Make sure the ID of the Post is passed to has_excerpt

    has_excerpt( $post->ID )
    
    #475036

    thx Josue, its working perfect ! good weekend, leRenaar

    #475040

    You are welcome, glad to help :)

    Regards,
    Josue

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.