0

I want to check if user is logged in and if have created a page. If so, option 1 is shown. This all works fine.

But how to display page title+link of page created by that user between {page title with link to page}

See the code below I tested it with no results. As used in the code below, nothing special is showing, only option 1 text (which is good for the user have a page created and is logged in).

    <?php
        if ( is_user_logged_in() ) {
            global $wpdb;
        $user = wp_get_current_user();
        $where = get_posts_by_author_sql( 'page', true, $user->ID );
        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

            if ( $count >= 1 ) { 

            // this part is added to display page title+link created by current user
            foreach ( $results as $result )
                printf( '<a href="%1$s">%2$s</a><br>', 
                get_permalink( $result->ID ),
                    esc_html( $result-post_title )
                ); ?>

//option 1
    <h2>Hey <?php echo $current_user->display_name ?>, check your page here: {page title with link to page} </h2>

    <?php } else { ?>
    //option 2
    <h2>Welcome <?php echo $current_user->display_name ?>, etc.. text with tags etc.</h2>

    <?php } } else { ?>
    //option 3
    <h2>text with tags etc.</h2>

    <?php } ?>

Any suggetions?

2 Answers 2

4

To list all pages with title and permalink from one user you need $wpdb->get_results(). The following code is based on this answer: How to count current user's pages?

First, we move the counter into a separate helper function; we might need it later again:

/**
 * Get all post IDs and titles of a type for a user.
 *
 * @param int $user_id
 * @param string $post_type 'page' (default), 'post', attachment, a custom post
 *                          type or 'any' (which excludes attachments)
 * @return array
 */
function t5_user_pages( $user_id, $post_type = 'page' )
{
    global $wpdb;
    $where = get_posts_by_author_sql( $post_type, true, $user_id );
    return $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts $where" );
}

Now we use that function in our code:

if ( is_user_logged_in() )
{
    $user    = wp_get_current_user();
    $results = t5_user_pages( $user->ID );
    $count   = count( $results );

    if ( $count >= 1 )
    {
        print '<h2>Hello ' . esc_html( $user->display_name ) . '!</h2>
        <p>These are your pages:</p>
        <ul>';

        foreach ( $results as $result )
            printf( '<li><a href="%1$s">%2$s</a></li>',
                get_permalink( $result->ID ),
                esc_html( $result->post_title )
            );

        print '</ul>';
    } else {
        // user is logged in but hasn't written anything
    }
}
else
{
    // user is not logged in
}
7
  • Thanks, but this shows the page count ("1 page") instead of the page title+link to it.
    – tobe
    Commented Apr 24, 2013 at 10:09
  • @tobe I have tested this code. It does exactly what you have asked for. Make sure to copy the complete example.
    – fuxia
    Commented Apr 24, 2013 at 10:10
  • Sry my mistake, I forgot to update line 6 and 7. I also removed the line: print "<h2>$count pages</h2>"; for I don't need to display the amount of pages. While the page title+link is now showing above the text <h2>Hey <?php echo $current_user->display_name ?>, check your page here: {page title with link to page} </h2> But I want to display the page title+link between the {} in that line. If I de add the complete option 1 line here printf( ' <h2>option 1 text+code</h2> ' then the <?php echo $current_user->display_name ?> does not work of course. Is there no <?php ?> I can add between {}
    – tobe
    Commented Apr 24, 2013 at 11:55
  • The second option cannot work, because it will only be used when there is no known user, hence no name.
    – fuxia
    Commented Apr 24, 2013 at 11:57
  • Do you mean it not possible to have as result: <h2> hello {current user display name} you can find your page here {page title+link}</h2>
    – tobe
    Commented Apr 24, 2013 at 12:01
0

Found it! check the code below:

<?php
        if ( is_user_logged_in() ) {
        global $wpdb;
        $user = wp_get_current_user();
        $where = get_posts_by_author_sql( 'page', true, $user->ID );
        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

            if ( $count >= 1 ) { ?>

    //option 1 if user is logged in and have created a page
    // added Toscho's code from answer 1 between second '<? php ?>'
    <h2>Hey <?php echo $current_user->display_name ?>, check your page here: <?php foreach ( $results as $result )
                    printf( '<a href="%1$s">%2$s</a>',
                    get_permalink( $result->ID ),
                    esc_html( $result->post_title )
                ); ?></h2>

    <?php } else { ?>

    //option 2 if user is logged in, but have not yet submitted a page
    <h2>Welcome <?php echo $current_user->display_name ?>, etc.. text with tags and some form php code</h2>

    <?php } } else { ?>

    //option 3 is no user is not logged in
    <h2>text with tags etc and social login php code</h2>

    <?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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