• I know this is not going to be a easy thing, but I am making a theme and would like to have it so people can pick what pages to show on the home page.(front-page.php)

    I dont want the full page to show, just the title, maybe a featured image, read more link, and a few words from the page.

    I am looking to make it so this can be done from the back-end so it is easy to change what content is displayed on the home page.

    Thank you for any help you can give me, and I am kind of sure that I can not have a “featured” page but there has to be some way to set pages apart so that I can call them in code?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter chaos67731

    (@chaos67731)

    So I have found this code:

    <?php
    	$args=array(
    	'order' =>'asc',
    	'post_type' =>'page',
    	'post__in' => array(8,9,11),
    );
    	$page_query = new WP_Query($args); 
    
    	while ($page_query->have_posts()) : $page_query->the_post();
    ?>
    <div class="featured-pages">
    	<h2><?php the_title();?></h2>
    	<?php the_excerpt(); ?>
    </div>
    <?php endwhile; ?>

    This does just what I want it to do, the question now is, how do I make it so people can pick the pages from the back-end so there is no need to touch the code?

    Thread Starter chaos67731

    (@chaos67731)

    I am trying to make this code work for getting the ID’s needed but am having no luck. Hopping someone can help out.

    I am making a navagation in the back-end and trying to pull the ID’s from there.

    $menu_name = 'featured-page';
    
    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] )) {
        $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
        $menu_items = wp_get_nav_menu_items($menu->term_id);
    }
    
    foreach ( $menu_items as $menu_item ) {
        $numbers[] = $menu_item->ID;
    }
    
    $numbers[] = get_post_meta( $menu_item->ID, '_menu_item_object_id', true );
    
    echo $numbers;

    https://wordpress.stackexchange.com/questions/94787/get-page-ids-from-nav-items/

    Moderator bcworkz

    (@bcworkz)

    The assignment to $numbers[] in the foreach loop is not going to be useful, get rid of that line and move the get_post_meta() assignment in it’s place. The way it is now, your array will contain mostly useless menu IDs and the final post ID. Changing it as suggested will yield an array of associated post numbers.

    I assume the echo $numbers; is you just want to check the contents of the array before you proceed with the next coding task? Echoing an array does not yield useful information. Use var_dump() instead. Depending on where this code executes, WP often overwrites the var_dump() output. If don’t see your array dump, add the die() function after var_dump() to stop WP from overwriting, or doing anything else for that matter.

    Your next step once this part checks out is to generate a form by stepping through the post ID array and outputting a checkbox HTML and the post title each time. When the form is submitted, store the selected post IDs in an option as an array.

    When the front page loads, it grabs the option array value and steps through each ID in it, displaying each associated post in turn.

    Thread Starter chaos67731

    (@chaos67731)

    Yes the echo what just to test the output so I could find out what I was calling, I have

    <?php
    // Name of Custom Navagation in the back end
    $menu_name = 'featured-page';
    
    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] )) {
        $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
        $menu_items = wp_get_nav_menu_items($menu->term_id);
    }
    
    foreach ( $menu_items as $menu_item ) {
      $numbers[] = get_post_meta( $menu_item->ID, '_menu_item_object_id', true );
    }
    
    var_dump($numbers) ;
    
    ?>

    The var_bump will output this
    array(3) { [0]=> string(2) “11” [1]=> string(1) “9” [2]=> string(1) “8” }

    So my next step is to turn that in to “11,9,8” as you will see in my second post I am wanting to use that to get what pages I will display in the front-page.php

    Thank you so much for your help, I am not the best at php so I am still trying to understand what to do next, but non the less you have hellped me out for sure!

    Moderator bcworkz

    (@bcworkz)

    I’m pleased my advice was helpful. To be clear, my suggestion about the check box form was an approach to allow selection of posts to display in the back-end. With 3 posts as a result, you can bypass that feature.

    The code you have so far can reside on your front page template as well as anywhere. The next step is to create your own WP ‘loop’ on the template, except where you normally see while ( have_posts() ) to start the ‘loop’, you would have something like foreach($numbers as $id){//rest of 'loop'... .

    And instead of using template tags like the_title() you would need to use something like $post = get_post($id); to get each post object. Then you could do whatever you want with the post object, for example echo "<h2>{$post->post_title}</h2>";.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘A type of "featured" page and then have it displayed on front-page.php’ is closed to new replies.