如何显示页面标题+用户创建的页面链接

时间:2013-04-24 作者:tobe

我想检查用户是否已登录,以及是否已创建页面。如果是,则显示选项1。这一切都很好。

但是how to display page title+link of page created by that user 在{链接到页面的页面标题}之间

请参阅下面的代码,我对其进行了测试,但没有结果。正如在下面的代码中使用的那样,没有显示任何特殊内容,只有选项1文本(这对用户创建页面并登录有好处)。

    <?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 } ?>
有什么建议吗?

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

要列出一个用户提供的标题和永久链接的所有页面,您需要$wpdb->get_results(). 以下代码基于此答案:How to count current user\'s pages?

首先,我们将计数器移动到一个单独的辅助函数中;我们以后可能还会需要它:

/**
 * 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" );
}
现在,我们在代码中使用该函数:

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
}

SO网友:tobe

找到了!检查以下代码:

<?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 } ?>

结束