WordPress

Używanie narzędzia dostosowywania motywu WordPress do wybierania szablonów stron, które aktualizują układ w podglądzie

  • 28 kwietnia, 2016
  • 4 min read
Używanie narzędzia dostosowywania motywu WordPress do wybierania szablonów stron, które aktualizują układ w podglądzie


Nie byłem pewien, gdzie umieścić to pytanie..

Niedawno odkryłem „Theme Customizer” WordPressa i używam go, aby ułatwić klientom aktualizację stron. Zamiast standardowego sposobu edytowania każdej pojedynczej strony, klikania aktualizacji, a następnie odwiedzania strony w celu zobaczenia zmian, podoba mi się sposób, w jaki Theme Customizer automatycznie wyświetla podgląd zmian po prawej stronie.

Próbuję zrozumieć, jak daleko mogę się posunąć w dostosowywaniu motywu, zanim zabiorę się za to wszystko…

Stworzyłem ustawienie/sekcję/kontrolkę „Strony głównej” przedstawione tutaj:

wprowadź tutaj opis obrazu

A oto kod do tego:

function get_page_templates_select() {
 $teh_cats = get_page_templates();
 foreach ( $teh_cats as $template_name => $template_filename ) {
     if (stripos(strtolower($template_filename), 'home') !== false) {
        $results[$template_filename] = $template_name;
     }
   }
   return $results;
   echo $results;
}

function get_categories_select() {
 $teh_cats = get_categories();
    $results;
    $count = count($teh_cats);
    for ($i=0; $i < $count; $i++) {
      if (isset($teh_cats[$i]))
        $results[$teh_cats[$i]->slug] = $teh_cats[$i]->name;
      else
        $count++;
    }
  return $results;
}

function prowordpress_customize_register( $wp_customize ) {

    // Settings, Sections, and Controls are defined here

    // HOME PAGE

    $wp_customize->add_setting( 'home_page_text' , array(
        'default'           => 'This is the home page text',
        'type'              => 'option',
        'transport'         => 'refresh',
    ));
    $wp_customize->add_section( 'prowordpress_content_customizations' , array(
        'title'       => __('Home Page', 'prowordpress'),
        'description' => __('Modify the Home Page', 'prowordpress'),
        'priority'    => 30,
    ));
    $wp_customize->add_control( 'home_page_text_control', array(
        'label'      => __( 'Home Page Text', 'prowordpress' ),
        'section'    => 'prowordpress_content_customizations',
        'settings'   => 'home_page_text',
        'type'       => 'textarea',
    ));

    $wp_customize->add_setting( 'home_page_template_select' , array(
        'default'           => 'test',
        'type'              => 'theme_mod',
        'transport'         => 'refresh',
    ));
    $wp_customize->add_control(
        new WP_Customize_Control(
            $wp_customize,
            'home_page_template_select',
            array(
                'label'          => __( 'Home page template:', 'blankwptheme' ),
                'section'        => 'prowordpress_content_customizations',
                'settings'       => 'home_page_template_select',
                'type'           => 'select',
                'choices'        => get_page_templates_select(),
            )
        )
    );

    $wp_customize->add_setting( 'home_page_posts_select' , array(
        'default'           => 'test',
        'type'              => 'theme_mod',
        'transport'         => 'refresh',
    ));
    $wp_customize->add_control(
        new WP_Customize_Control(
            $wp_customize,
            'home_page_posts_select',
            array(
                'label'          => __( 'Which post type to display on the home page?', 'blankwptheme' ),
                'section'        => 'prowordpress_content_customizations',
                'settings'       => 'home_page_posts_select',
                'type'           => 'select',
                'choices'        => get_categories_select(),
            )
        )
    );

}

add_action( 'customize_register', 'prowordpress_customize_register' );

Strona główna 1 szablon:

<?php /* Template Name: Home */

get_header();

echo "theme selected: " . get_theme_mod('home_page_template_select');

$page_id = 5;
update_post_meta( $page_id, '_wp_page_template',    get_theme_mod('home_page_template_select') );

?>

<div style="margin-top:60px;">

    <?php echo get_option('home_page_text'); ?>

</div>

<div style="margin-top:60px;">

    <?php
  $args = array(
    'category_name' => get_theme_mod('home_page_posts_select'),
    'posts_per_page' => 5
  );

  // Displays the category's name
  echo "<h3>" . get_category_by_slug($args['category_name'])->name . "    </h3>";
  $the_query = new WP_Query( $args );

   // The Loop
  if ( $the_query->have_posts() ) :
     //echo "<ul>";
    while ( $the_query->have_posts() ) : $the_query->the_post();
      if(has_post_thumbnail($post->ID)){ 
        $thumbsrc = get_the_post_thumbnail($post->ID,'medium'); 
      } else {
        $thumbsrc = " src=\"images/no_featured_image.jpg\" alt=\"" . get_the_title() . "\">";
      }
      $link = get_permalink();
      $title = get_the_title();
      $content = get_the_content();
      echo '<div style="width:302px;float:left;height:502px;margin- right:20px;">';
     echo $thumbsrc . '<br>';
      echo '<a href="' . $link . '">'  . $title . '</a><br>' . $content;
      echo '</div>';
    endwhile;
    //echo "</ul>";
  endif;

  // Reset Post Data
  wp_reset_postdata();

?>

</div>

<?php get_footer(); ?>

Na zrzucie ekranu widać, że dodałem menu wyboru dla „Szablonu strony głównej”…

Warto przeczytać!  Jak wyświetlać zdjęcia WordPress w kolumnach i wierszach

Czy to możliwe, że mógłbym to skonfigurować tak, aby klient mógł wybrać istniejący „szablon strony” z tego menu wyboru, a następnie wyświetlić podgląd/układ strony po prawej stronie, aby automatycznie odziedziczył ustawienia szablonu strony i dostosował układ w czasie rzeczywistym ?

Ponownie próbuję tylko zrozumieć, czy jest to wykonalne i czy ktoś już próbował czegoś podobnego. Zdaję sobie sprawę, że może to wymagać AJAX lub czegoś podobnego.

Dzięki za pomoc!


Źródło