WordPress snipets

wp_head();
wp_footer();

get_header();
get_footer();
wp_nav_menu(array(
  'theme_location' => 'hederMenu'
));

while(have_posts()):the_post();
  // code...
endwhile;

body:

bloginfo('charset');
language_attributes();
body_class();

 

menu:

function ws_register_menu()
{
  register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
  register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
  register_nav_menu( 'short', __( 'Short menu', 'theme-slug' ) );
}
add_action( 'after_setup_theme', 'ws_register_menu' );

<nav>
  <?php
  if ( has_nav_menu( 'primary' ) ) {
      wp_nav_menu( array("theme_location" => 'primary'));
  }
  ?>
</nav>

 

title:

the_title();
echo get_the_title();
the_archive_title();

    if ( is_category() ) single_cat_title();
    if ( is_author() ) echo "Posts by: "; the_author();

 

content:

the_content();
the_excerpt();
the_archive_ description();
echo wp_trim_words( get_the_content(), 15 );

  if ( has_excerpt() ) {
    echo get_the_excerpt();
  } else {
    echo '

' . wp_trim_words( get_the_content(), 15 ) . '

';
  }

 

posts:

the_author_posts_link();
the_time('n. j. y');
get_the_category_list(' and ');
get_permalink();
echo get_permalink($theID);
echo paginate_links();

 

ID:

the_ID();
echo get_the_id();

 

menu:

wp_list_pages();

 

url:

echo site_url();
echo site_url('/portfolio');

 

file:

echo get_theme_file_uri('/images/logo.png');
echo get_stylesheet_uri();

 

parents:

wp_get_post_parent_id(get_the_ID());

 

custom post type:

echo get_post_type_archive_link('event');

 

thumbnail:

get_the_post_thumbnail_url();

the_post_thumbnail('photoPortrait');
<img src="<?php the_post_thumbnail_url('photoLandscape'); ?>" alt="">

w functions.php

  add_theme_support('post-thumbnails');
  add_image_size('photoLandscape', 400, 260, array('left', 'top'));
  add_image_size('photoPortrait', 400, 650, true);

 

custome Query:

<?php $homePageposts = new WP_Query(array( 'posts_per_page' => 2
  ));

  while ( $homePageposts->have_posts() ) { //default query
    $homePageposts->the_post(); ?>
    

<li>


<h2><?php the_title(); ?></h2>


      

<?php echo wp_trim_words( get_the_content(), 15 ); ?>

    </li>


  <?php } wp_reset_postdata(); ?>

 

custome Query – parametry:

    'posts_per_page' => -1,
    'category_name' => 'news',
    'post_type' => 'event',
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
      array(
        'key' => 'event_date',
        'compare' => '>=',
        'value' => $today
      )
    )

 

Własne Query – posty wcześniejsze od dzisiejszej daty:

$today = date('Ymd');
$pastEvents = new WP_Query(array(
  'paged' => get_query_var('paged', 1),
  'posts_per_page' => 2,
  'post_type' => 'event',
  'meta_key' => 'event_date',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_query' => array(
    array(
      'key' => 'event_date',
      'compare' => '<', 'value' => $today,
      'type' => 'numeric'
    )
  )
));
while ( $pastEvents->have_posts() ) {
  $pastEvents->the_post(); ?>
    

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>


  <?php } echo paginate_links(array( 'total' => $pastEvents->max_num_pages
    ));
?>

 

not log in:

  if (!is_user_loggedin()) {
    wp_redirect(esc_url(site_url("/")));
    exit; 
  }

 

Search:


esc_html(get_search_query())

  if (have_posts()) {
    while(have_posts()) {
      the_post();
      get_template_part('template-parts/content', get_post_type());
    }
    echo paginate_links();
  } else {
    echo '

<h2 class="headline headline--small-plus">No results match that search.</h2>


';
  }

  get_search_form();

 

page links:


echo wp_logout_url();

echo wp_login_url();

echo wp_registration_url();

 

Pobranie wartości z custom post ACF:

$eventDate = new DateTime( get_field('event_date' ));
echo $eventDate -> format('M');
echo $eventDate -> format('d');

 

Przekierowanie na stronę home kiedy nie jest się zalogowanym:

  if (!is_user_logged_in()) {
    wp_redirect(esc_url(site_url('/')));
    exit;
  }

 

Wyświetlenie wartości z custom post ACF:

print_r($relatedPrograms);

kod dla konkretnej strony, kategorii

WordPress must use plugins

zamieszczone w specjalnym folderze mu-plugins, nie można ich dezaktywować i są niezależne od tematu.

 

add_menu_page();
add_option();
get_option();
update_option();
delete_option();

define(‚AUTOMATIC_UPDATER_DISABLED’, true);

add_filter(‚allow_dev_auto_core_update’ , ‚__return_true’); //enable development update
add_filter(‚allow_minor_auto_core_update’ , ‚__return_true’); //enable minor updates
add_filter(‚allow_major_auto_core_update’ , ‚__return_true’); //enable major updates


codex.wordpress.org
developer.wordpress.org

#Wordpress

Agnieszka Trefler

Agnieszka Trefler (Ruda) - freelancer, web developer, specjalistka od WordPressa z Wrocławia. Z pasją budowania stron, blogowania i fotografowania. Twórca Inspiracji fotograficznych.