Skip to content

Instantly share code, notes, and snippets.

@kas-cor
Last active January 21, 2019 15:51
Show Gist options
  • Save kas-cor/106baf64a5d5e5c59833217478e2bbb8 to your computer and use it in GitHub Desktop.
Save kas-cor/106baf64a5d5e5c59833217478e2bbb8 to your computer and use it in GitHub Desktop.
My WP functions.php
<?php
/* header menu */
register_nav_menu("menu", "Main menu");
/* footer menu */
register_nav_menu("menu_footer", "Footer menu");
/* thumbnails */
add_theme_support('post-thumbnails');
function my_get_post($post_id) {
// Custom fields
$ret['custom_fields'] = get_post_custom($post_id);
// Content
$content = get_extended(get_post_field('post_content', $post_id));
$ret['content'] = [
'title' => get_the_title($post_id),
'main' => apply_filters('the_content', $content['main']),
'extended' => apply_filters('the_content', $content['extended']),
];
$ret['content_free'] = [
'title' => get_the_title($post_id),
'main' => $content['main'],
'extended' => $content['extended'],
];
// Images
$ret['thumbnail'] = [
'thumbnail' => get_the_post_thumbnail_url($post_id, 'thumbnail'),
'medium' => get_the_post_thumbnail_url($post_id, 'medium'),
'large' => get_the_post_thumbnail_url($post_id, 'large'),
'full' => get_the_post_thumbnail_url($post_id, 'full'),
];
// Media
$thumbnail_id = get_post_thumbnail_id($post_id);
$medias = get_attached_media('image', $post_id);
foreach ($medias as $media) {
if ($media->ID != $thumbnail_id) {
$ret['media']['image'][] = [
'thumbnail' => wp_get_attachment_image_src($media->ID, 'thumbnail')[0],
'medium' => wp_get_attachment_image_src($media->ID, 'medium')[0],
'large' => wp_get_attachment_image_src($media->ID, 'large')[0],
'full' => wp_get_attachment_image_src($media->ID, 'full')[0],
'excerpt' => $media->post_excerpt,
];
}
}
return $ret;
}
function my_get_posts($post_type = 'post', $cat_id = 0, $tag = NULL) {
$ret = [];
$args = array(
'numberposts' => -1,
'category' => $cat_id,
'orderby' => 'date',
'order' => 'ASC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' => '',
'post_type' => $post_type,
'suppress_filters' => true,
'tag' => $tag ? $tag : '',
);
$posts = get_posts($args);
foreach ($posts as $post) {
setup_postdata($post);
$ret[$post->ID] = my_get_post($post->ID);
}
wp_reset_postdata();
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment