0

I am using this shortcode to load a list of links to post per user profile or author:

/*
*
* Shortcode to display Post as links on Profile Pages
* 
*/

function imwz_golfs_list_function( $atts ) {
    if ( is_user_logged_in() ):

    global $current_user;
    wp_get_current_user();
    $author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
    $author_posts = new WP_Query($author_query);
    while($author_posts->have_posts()) : $author_posts->the_post();
    ?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php           
    endwhile;

    else :

    echo "not logged in";

    endif;
    }

add_shortcode( 'golfslist', 'imwz_golfs_list_function' );

Now it does work on Twenty Seventeen except for its position. However on the Total theme it is loaded just after the body tag as well on top of the positioning issue. So on both themes it is loaded before the main content and not as positioned in the post or page after the content using [golfslist] in the WYSIWYG editor.

Any ideas how I can load the list where I want in the post by placing it in the editor? Any idea how it could be loaded in the post content and also just below the body tag?

0

1 Answer 1

1

Please try with this-

/*
*
* Shortcode to display Post as links on Profile Pages
* 
*/
function imwz_golfs_list_function( $atts ) {
    if ( is_user_logged_in() ):

    global $current_user;
    wp_get_current_user();
    $author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
    $author_posts = new WP_Query($author_query);

    ob_start();

    if($author_posts->have_posts()) :

    echo '<ul>';

    while($author_posts->have_posts()) : $author_posts->the_post();
    ?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php           
    endwhile;

    echo '</ul>';

    else :

    echo "not logged in";

    endif;

    $content = ob_get_clean();

    return $content;

    endif;
}

add_shortcode( 'golfslist', 'imwz_golfs_list_function' );

Not tested, but should work.

Please note, your content should be "returned", not "echoed" while using for shortcode.

2
  • Had to change the if else to make it work and the comment needs an / but it seems to be working. Testing live in a bit. Also clearly need some more reading on the add_shortcode and content in shortcode.
    – rhand
    Commented Mar 1, 2018 at 6:32
  • So please update code with fixed comment and fixed if else and I will approve. Unfortunately it will not load on final destination, but that seems to be a theme conflict or plugin conflict..
    – rhand
    Commented Mar 1, 2018 at 10:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.