Random front page

  • Condorino

    #5191

    Hi,

    On my front page at the top there is the slider, then just below a static fron page (i think) and below that some other pages (with read more link).

    I was wondering if instead to have a static page (on the front page) would be possible to replace it with a RANDOM PAGE ?

    I presume by replacing where is necessary with the following code would do the trick?

    Quote:
    <?php

    $args = array( ‘post_type’ => ‘page’, ‘numberposts’ => 1, ‘orderby’ => ‘rand’ );

    $rand_posts = get_posts( $args );

    foreach( $rand_posts as $post ) : ?>

    “><?php the_title(); ?>

    <?php the_content(); ?>

    <?php endforeach; ?>

    I would appreciate if I can receive some directions on with php file I need to edit.

    Thanks in advance

    Antonio

    Anonymous

    #31010

    Try this, it shows a randon page on homepage. Refresh homepage and you’ll see another page! It is working in my test site – http://test.prasannasp.net/

    Add this code to your child theme’s functions.php file anywhere between <?php and ?>

    function random_home_page_function() {
    $args=array(
    'orderby' => 'rand',
    'post_type' => 'page',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class( 'clearfix post' ); ?>>
    <div class="entry clearfix">
    <h1 class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
    <div class="entry-content clearfix">
    <?php the_content() ?>
    </div>
    </div>
    </div>
    <?php
    endwhile;
    }
    wp_reset_query();
    }

    add_action('graphene_after_slider', 'random_home_page_function')

    Condorino

    #31011

    Thank you for the prompt reply, unfortunately by adding your suggested code I do get a random page but underneath that random page still I do get the static default page.

    I was wondering which is the file which that is coding the front page?

    Thanks Antonio

    Anonymous

    #31012

    Just add this code to custom CSS or in Child theme style sheet to hide your static front page.

    .post-2 {
    display: none;
    }

    Condorino

    #31013

    Thank you vey much Prasanna for the super suggestion, now your tweaking

    is working fine, actually I have included in the function the following code:

    Quote:
    ‘post_parent’ => 2,

    I done this because I prefer to show only random pages belonging to a certain parent page, (On my case number 2).

    But now I have another little problem, before applying your random code in the Graphene setting I had setup and still showing all child pages belonging to that parent page.

    Whenever a visitor is visiting the front page he/she will see a random page and below that child pages, but one of the child page will have the same title and description as the top random full page.

    To avoid script complications I was wondering if it could be possible to list the child pages BUT BELONGING TO THE PARENT PAGE, therefore to exclude to be listed pages belonging to the parent on my case number 2 ?

    Your help is much appreciated.

    Thanks Antonio

    Anonymous

    #31014

    Just create a blank page and set it as static front page. So, no child pages will show up. Simple!

    When you change the static frontpage, find out it’s ID and replace it in .post-2. (custom CSS) So that, the new static front page will hide.

    Condorino

    #31015

    Sorry it doesn’t work, because by replacing the post-2. with 1033 (this is the number of my blank page) I will have a random page at the top, then page number 2 at bottom and lastly the children pages.

    Anonymous

    #31016

    That shouldn’t be happening. Why does it show child pages for a new blank page with no child pages?

    Did you really create a blank page? And set it as Static Front Page in WP-Admin –> Settings –> Reading Settings?

    Condorino

    #31017

    I have created a called blank page the number for it is 1033

    Then included in the css

    .post-1033 {

    display: none;

    }

    Then included the random function in the function.php

    Lastly in the admin reading settings: selected the blank page as the static front page

    and as results the child pages are not displaying any longer.

    Anonymous

    #31018

    So, what’s the problem now?

Viewing 10 posts - 1 through 10 (of 18 total)

  • 1
  • 2
  • You must be logged in to reply to this topic.