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();
he_title( '<h2>', '</h2>' );
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()); $post_type = get_post_type( get_the_ID() ); echo '<p>' . $post_type . '</p>'; $post_type = get_post_type_object( get_post_type($post) ); echo $post_type->label;
custom post type:
echo get_post_type_archive_link('event');
echo post_type_archive_title( '', false );
$post_type = get_post_type($post);
$archive_link = get_post_type_archive_link( $post_type );
$post_type_label = get_post_type_object( $post_type )->label;
previous_post_link( '%link','« Previous project' )
next_post_link( '%link','Next projects »' )
custom post type, create post type & taxonomy:
function create_pre_portfolio()
{
register_post_type(
'pre_portfolio',
[
'labels' => [
'name' => 'portfolio',
'singular_name' => 'portfolio',
],
'public' => true,
'exclude_from_search' => false,
'has_archive' => true,
'menu_position' => 11,
'supports' => ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'],
'menu_icon' => 'dashicons-format-image',
'taxonomies' => array( '' ),
'rewrite' => array('slug' => 'portfolio'),
]
);
}
add_action('init', 'create_pre_portfolio');
add_action( 'init', 'create_pre_portfolio_category_taxonomies', 0 );
function create_pre_portfolio_category_taxonomies()
{
$labels = array(
'name' => 'Categories',
'singular_name' => 'Category',
'menu_name' => __( 'Categories' ),
);
register_taxonomy('pre-porto','pre-portfolio',array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'photography' ),
));
}
add_action( 'init', 'create_pre_portfolio_tags_taxonomies', 0 );
function create_pre_portfolio_tags_taxonomies()
{
$labels = array(
'name' => 'Tags',
'singular_name' => 'Tag',
'menu_name' => __( 'Tags' ),
);
register_taxonomy('pre-tags','pre-portfolio',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'photo' ),
));
}
thumbnail:
get_the_post_thumbnail_url();
the_post_thumbnail('photoPortrait');
<img src="<?php the_post_thumbnail_url('photoLandscape'); ?>" alt="">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'thumbnail', array('alt' => get_the_title()));
} else {
echo('<img src="' . get_template_directory_uri() . '/images/blog-min.png" alt="<?php the_title(); ?>"/>');
} ?>
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();
Template page
stworzenie strony
<?php /** * Template Name: Inqury Page * The statict page template. * * @package Stripes * @author name <email> */ ?>
echo esc_html( get_page_template_slug( $post->ID ) );
is_page_template('name-tpl.php')
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
Hope this helps and happy coding :)