• I’m currently showing my blog posts on a single page (via a shortcode), and I only show the excerpts to each posts. I then want only logged in users to be able to read the entire blog post, but can’t seem to figure it out.

    This is my shortcode function in functions.php:

    function custom_query_shortcode($atts) {
    
       // EXAMPLE USAGE:
       // [loop_shortcode the_query="showposts=100&post_type=page&post_parent=453"]
    
       // Defaults
       extract(shortcode_atts(array(
          "the_query" => ''
       ), $atts));
    
       // de-funkify query
       $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
       $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);
    
       // query is made
       query_posts($the_query);
    
       // Reset and setup variables
       $output = '';
       $temp_title = '';
       $temp_link = '';
       $temp_ex = '';
       $temp_content = '';
       $temp_thumb = '';
       $temp_id = '';
    
       // the loop
       if (have_posts()) : while (have_posts()) : the_post();
          $temp_id = $post->ID;
          $temp_title = get_the_title($post->ID);
          $temp_link = get_permalink($post->ID);
          $temp_content = get_the_content($post->ID);
          $temp_ex = get_the_excerpt();
            if ( has_post_thumbnail() ) {
            $temp_thumb = get_the_post_thumbnail($post->ID);
            } else {
            $temp_thumb = "" ;
            }
    
          // output all findings -
         $output .= "<div class='post-$temp_id' id='post-$temp_id'>
                    <h2 class='entry-title'>
                        <a title='$temp_title' rel='bookmark' href='$temp_link'>$temp_title</a>
                    </h2><!--BEGIN .entry-content-->
                    <div class='entry-content'>
                        <div class='theExrp'>
                        <p>
                            <a href='$temp_link'>
                                $temp_thumb
                            </a>
                        </p>
                        <p>
                                $temp_ex
                        </p>
                        </div>
                    </div><!--END .hentry-->
                </div>";
        endwhile; else:
          $output .= "Not found.";
       endif;
       wp_reset_query();
       return $output;
    }
    add_shortcode("loop_shortcode", "custom_query_shortcode");

    How can I alter this code to show excerpt to public users, but only logged in users can access the single posts? Non-logged in users should get redirected to the login page.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can use the function is_user_logged_in() which will return true if user logged in or false if the user is not logged in.
    Try conditional tag like this like this

    if(is_user_logged_in()){
    //code for show post contents
    }
    else{
    //code for show excerpt
    }
    Thread Starter Pestbarn

    (@pestbarn)

    I already tried this in my original code:

    // the loop
       if (have_posts()) : while (have_posts()) : the_post();
          $temp_id = $post->ID;
          $temp_title = get_the_title($post->ID);
          if(!is_user_logged_in()){
                 $temp_link = "login.php";
          } else {
          $temp_link = get_permalink($post->ID);
          }
          $temp_content = get_the_content($post->ID);
          $temp_ex = get_the_excerpt();

    This, however, crashed the entire site, forcing me to SSH into my server and revert the changes via Nano.

    Well, I used that code for my site for cache some pages, if user not logged in. It works without any problem, And the above code of you don’t have any problem, it must be a problem with the other part

    Thread Starter Pestbarn

    (@pestbarn)

    It’s indeed very strange, as I have been using the is_user_logged_in() function several times before. Is it because I have the if statement inside the loop or something?

    No, it doesnt matter, but even one syntax error in ur PHP script could crash the entire script, I think the best way do is edit the code of your theme loop.php, find the part that prints the contents ( get_the_content() function call) and replace with

    if(is_user_logged_in()){
       get_the_content();
    }
    else{
       get_the_excerpt();
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Different permalinks for users who are not logged in’ is closed to new replies.