WordPress.org

Support

Support » How-To and Troubleshooting » how to display a random page in a static front page

how to display a random page in a static front page

  • Dear all,
    for the above mentioned problem I used a solution from another closed topic :
    Random Pages
    by Triple P

    I have no doubt about solution but in my site/template if I place the following code

    <?php
    $args = array( 'post_type' => 'page', 'numberposts' => 1, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    	<a>"><?php the_title(); ?>
            <?php the_content(); ?>
    <?php endforeach; ?>

    inside my index page it duplicates the content of all pages.
    How can I fix this problem???
    Thank you for your kind cooperation

Viewing 3 replies - 1 through 3 (of 3 total)
  • try to amend the code:

    <?php
    $args = array( 'post_type' => 'page', 'numberposts' => 1, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : setup_postdata( $post ); ?>
    	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php the_content(); ?>
    <?php endforeach; wp_reset_postdata(); ?>

    what theme are you using?

    does the theme have a page.php template?

    what is the full code of your edited template?

    Hi alchymyth and vthanks for your interest.
    The theme I’m using is Customizr 3.1.22 and it has not page.php,
    following the full code of the index.php:

    <?php
    /**
     * The main template file. Includes the loop.
     *
     *
     * @package Customizr
     * @since Customizr 1.0
     */
    ?>
    
    <?php do_action( '__before_main_wrapper' ); ##hook of the header with get_header ?>
    <div id="main-wrapper" class="<?php echo tc__f( 'tc_main_wrapper_classes' , 'container' ) ?>">
    
        <?php do_action( '__before_main_container' ); ##hook of the featured page (priority 10) and breadcrumb (priority 20)...and whatever you need! ?>
    
        <div class="container" role="main">
            <div class="<?php echo tc__f( 'tc_column_content_wrapper_classes' , 'row column-content-wrapper' ) ?>">
    
                <?php do_action( '__before_article_container'); ##hook of left sidebar?>
    
                    <div id="content" class="<?php echo tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' ) ?> article-container">
    
                        <?php do_action ('__before_loop');##hooks the header of the list of post : archive, search... ?>
    
                            <?php if ( tc__f('__is_no_results') || is_404() ) : ##no search results or 404 cases ?>
    
                                <article <?php tc__f('__article_selectors') ?>>
                                    <?php do_action( '__loop' ); ?>
                                </article>
    
                            <?php endif; ?>
    
                            <?php if ( have_posts() && !is_404() ) : ?>
                                <?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
                                    <?php the_post(); ?>
    
                                    <?php do_action ('__before_article') ?>
                                        <article <?php tc__f('__article_selectors') ?>>
                                            <?php do_action( '__loop' ); ?>
                                        </article>
                                    <?php do_action ('__after_article') ?>
    
                                <?php endwhile; ?>
    
                            <?php endif; ##end if have posts ?>
    
                        <?php do_action ('__after_loop');##hook of the comments and the posts navigation with priorities 10 and 20 ?>
    
                    </div><!--.article-container -->
    
               <?php do_action( '__after_article_container'); ##hook of left sidebar ?>
    
            </div><!--.row -->
        </div><!-- .container role: main -->
    
        <?php do_action( '__after_main_container' ); ?>
    
    </div><!--#main-wrapper"-->
    <!-- ===================================== -->
    <?php
    $args = array( 'post_type' => 'page', 'numberposts' => 1, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php the_content(); ?>
    <?php endforeach; ?>
    <!-- ===================================== -->
    
    <?php do_action( '__after_main_wrapper' );##hook of the footer with get_get_footer ?>

    to get theme specific suggestions, please post in https://wordpress.org/support/theme/customizr/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how to display a random page in a static front page’ is closed to new replies.
Skip to toolbar