0

PRECONDITION:

I created 2 pages:

  • red archive's page
  • white archive's page

I created Widget

<?php class listArchive extends WP_Widget {

/**
 * Register widget with WordPress.
 */
function __construct() {
    parent::__construct(
        'listarchive', // Base ID
        esc_html__( 'listarchive', 'text_domain' ), // Name
        array( 'description' => esc_html__( 'Widget regarding archive posts', 'text_domain' ), ) // Args
    );
}

/**
 * Front-end display of widget.
 *
 * @see WP_Widget::widget()
 *
 * @param array $args     Widget arguments.
 * @param array $instance Saved values from database.
 */
public function widget( $args, $instance ) {
    echo $args['before_widget'];
    if ( ! empty( $instance['title'] ) ) {
        echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
    }
    echo esc_html__( 'Best archive', 'text_domain' );
    echo $args['after_widget'];
}

/**
 * Back-end widget form.
 *
 * @see WP_Widget::form()
 *
 * @param array $instance Previously saved values from database.
 */
public function form( $instance ) {
    $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Best Archive', 'text_domain' );
    ?>
    <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label>
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
    </p>
    <?php
}

/**
 * Sanitize widget form values as they are saved.
 *
 * @see WP_Widget::update()
 *
 * @param array $new_instance Values just sent to be saved.
 * @param array $old_instance Previously saved values from database.
 *
 * @return array Updated safe values to be saved.
 */
public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';

    return $instance;
}

}

and located it on front-page.php

<?php
    get_header();
?>
<div class="container">
    <div class="widgets">
        <div class="table_bg width_fifty"><?php get_sidebar('listarchive'); ?></div>
    </div>
</div>
<?php
    get_footer();
?>

TASK: I need to list red archive's page and white archive's page in this widget.

PROBLEM: I don't know how to set widget to list red archive's page and white archive's page, pls help.

  • We could really do with seeing the code that you have used to add your widget with. Also, when you say "list" are you looking to simply list the pages link, show in a list, show all the content. Please elaborate. Thanks – Alex Knopp May 15 '21 at 10:58
  • 1. About code example: - edited question, see there 2. When I say "list" - it means that I want to see titles of the pages with href="#" , so when user click on title he is transferred to archive page. – Владислав Загородний May 15 '21 at 11:13

1 Answers1

0
<?php
/**
 * Query just the 2 pages
 **/

//EDIT FOR WIDGET TITLE
echo '<h3> Widget Title </h3>'; //Or whatever tag you want the title in

//Get the two posts from the DB that match these ID's, replace the IDs with your own.
foreach( get_posts(['include' => ['123','456']]) as $post ){

  //For each of these posts, spit the title. Change the p tag to whatever you need.
  echo '<p>' . $post->post_title . '</p>';

}

/**
 * Query posts by term
 * This will allow you to add posts to categories and query multiple categories for posts to show.
 **/
foreach( get_posts([
    'tax_query' => [
        [
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => ['term1', 'term2', 'term3'],
            'operator' => 'IN'
        ]        
    ],
    'exclude' => ['1','2','3'] //In case there is anything you want to ommit
]) as $post ){

//For each of these posts, spit the title. Change the p tag to whatever you need.
echo '<p>' . $post->post_title . '</p>';

}
?>
Alex Knopp
  • 907
  • 1
  • 10
  • 30
  • Alex Knopp, appreciate. Very nice solution, but maybe you have another suggestion, let's assume that site will grow and I will have not 2 pages to list there but fifty, probably there is possibility to query by tag (not justly hardcode with id)? – Владислав Загородний May 15 '21 at 11:24
  • Your OP was to list two pages and didnt reflect any scale or need for growth. If you only need two pages then it uses less resources to query just those two pages. If you want something that scales then you need to find a way to organise them better ie tags or categories. If you have 50 pages, you may be better to organise those as posts in a category as taxonomy tables are generally smaller than meta tables and take less time to query. Lets assume you want to put them into posts and query the category instead. I'll edit my answer. – Alex Knopp May 15 '21 at 11:28
  • Alex, and the last , where exactly in widget code I need to insert your code's part? – Владислав Загородний May 15 '21 at 11:55
  • This functionality would be placed in the widgets callback function. It would be better if you could add your widget code to your question. Another way would be to add the code inside a function in your functions.php file and then call that function within your template file. – Alex Knopp May 15 '21 at 12:35
  • To be honest I would ditch the widget and just place the code you need in directly into the template file in widgets div and include a widget title in there. I'll edit my first option to show you. – Alex Knopp May 15 '21 at 12:56
  • Alex, example of widget code is presented in question (the first snippet). – Владислав Загородний May 15 '21 at 14:06
  • If you want to put it into your widget then you need to add the code after echo esc_html__( 'Best archive', 'text_domain' ); – Alex Knopp May 15 '21 at 16:21