• Resolved Ben Heath

    (@btheath)


    I’ve altered template to display content from another page.

    It works, but only if i’m logged in. When I checked it logged out, nothing displays.

    <? $the_query = new WP_Query( 'page_id=2' );
            while ( $the_query->have_posts() ) :
            $the_query->the_post();
            the_content();
            endwhile;
            wp_reset_postdata();
          ?>

    I don’t see any reason that would display only when logged in… does anyone have any idea?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Does it work if you use a different template? (try the default template) Is the page you’re querying visible when you’re logged out?

    Thread Starter Ben Heath

    (@btheath)

    So, here is the full story.

    We’ve created a CPT for Webinars. It uses the https://&#8230;./webinars/ slug, which is the archive-webinars.php file on the site.

    However, the client would like to manually control what is on the Archive page via the page editor, which Archive pages don’t have.

    So, I’m trying to display the content from another page that they can edit, which page will be no-indexed, and 301’d to the archive page.

    The sidebar shows up fine, the page title shows up fine, but the code above which is pulling in the content from another page is not showing up, unless logged in.

    If there is another way other than that code to accomplish my end goal of easily editable content on an archive page… I’m open to ideas there too. I searched, and couldn’t find other ways to approach this in google yet.

    I’d recommend doing it the opposite way, actually. Set up an archive listing on a page template. The key here is that you overwrite the query, then reinstate it

    Check out this snippet:

    <div id="latest_posts" class="container-fluid">
      <div class="row">
        <?php
        $temp_query = $wp_query;
          $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
          $args = array(
            'paged' => $paged,
            'post_type' => '<yourposttype>',
         );
    
          /* make a new query for the events */
    
          $wp_query = new WP_Query( $args );
    
        $i = 1;
        while ( have_posts() ) : the_post();?>
    
          the_title();
    the_content();
        endwhile;
        wp_reset_postdata();
        ?>
        <?php if ($wp_query->max_num_pages > 1) : ?>
          <div class="row">
            <nav class="pagination">
              <?php next_posts_link(__('Older posts', 'roots')); ?>
              <?php previous_posts_link(__('Newer posts', 'roots')); ?>
            </nav>
          </div>
        <?php endif; ?>
        <?php $wp_query = $temp_query; ?>
      </div>
    </div>

    Add this snippet to a page template, where you’d like to loop through the posts. You can take what you have in archive.php and paste it into a page template as a starting place.

    let me know if you have any questions.

    One thing to check might be to double check your publish status. If it’s in “protected” status, the content won’t be available if you’re logged in.

    Actually, an even better solution would be to use advanced custom fields to accomplish this. You can create field groups that will appear on taxonomy term edit pages. For items that you’d like to see on the main archive landing page, you could use their addon for global site options.

    ACF will let you add all kinds of fields that you can easily get to from the templates.

    The Options addon is a premium addon, but it’s definitely worth it – you can use it on unlimited sites!

    Thread Starter Ben Heath

    (@btheath)

    We are using the ACF plugin on that site, so perhaps I’ll try setting it up that way, instead of trying to pass regular content on a page.

    Thank you for your input Bryan. We can mark this closed for now, and if and when I find a solution, or am still stuck, I’ll update here so that others can benefit from the solution.

    Is the page, itself visible on the frontend, if you select the default template for the page?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Content doesn't show unless logged in’ is closed to new replies.