Last active
May 25, 2021 12:04
-
-
Save slavapas/4e52918323b1324359818b2e746bcc9a to your computer and use it in GitHub Desktop.
ACF functions.php and index.php how to call them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// in functions.php create the CUSTOM POST TYPE | |
// create CPT = reviews | |
add_action('init', 'my_first_post_type'); | |
function my_first_post_type() | |
{ | |
// for thumbnails to be recognised | |
add_theme_support('post-thumbnails'); | |
// CPT reviews | |
register_post_type('reviews', [ | |
'label' => null, | |
'labels' => [ | |
'name' => 'отзыв', // основное название для типа записи | |
'singular_name' => 'отзыв', // название для одной записи этого типа | |
'add_new' => 'Добавить отзыв', // для добавления новой записи | |
'add_new_item' => 'Добавление отзыв', // заголовка у вновь создаваемой записи в админ-панели. | |
'edit_item' => 'Редактирование отзыв', // для редактирования типа записи | |
'new_item' => 'Новое отзыв', // текст новой записи | |
'view_item' => 'Смотреть отзывы', // для просмотра записи этого типа. | |
'search_items' => 'Искать отзывы', // для поиска по этим типам записи | |
'not_found' => 'Не найдено', // если в результате поиска ничего не было найдено | |
'not_found_in_trash' => 'Не найдено в корзине', // если не было найдено в корзине | |
'menu_name' => 'REVIEWS', // название меню | |
], | |
'public' => false, | |
'show_ui' => true, // зависит от public | |
'menu_icon' => 'dashicons-sticky', | |
'supports' => ['title', 'editor', 'thumbnail'], // 'title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats' | |
]); | |
} | |
// install the plugin Advanced Custom Fields ACF, and add come custom fields | |
// buy default we call them in our the file, but | |
// to make less requests to DB call all ACFs for our CPT at once | |
// create getReviews() function | |
function getReviews() | |
{ | |
$args = [ | |
'orderby' => 'date', | |
'order' => 'ASC', | |
'post_type' => 'reviews' | |
]; | |
$reviews = []; // create an empty array | |
foreach (get_posts($args) as $post) { | |
$postID = $post->ID; // assign the ID of this post to $postID | |
$review = get_fields($postID); // get all the IDs of our registered CPT with name = reviews | |
//print_r(get_fields($postID)); | |
$review['name'] = $post->post_title; // we intentionally have added a default field = name (the title of our CPT) to our array, but we can ignore it | |
$review['description'] = $post->post_content; // we intentionally have added a default field = description (the title of our CPT) to our array, but we can ignore it | |
$review['img'] = get_the_post_thumbnail_url($post->ID, 'thumbnail'); // we use this code when we want to get the default feature image thumbnail url | |
$reviews[] = $review; // add IDs and ['name'] to our array | |
//print_r($reviews); | |
} | |
return $reviews; // return the array | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Reviews Section --> | |
<div class="container">REVIEWs | |
<div class="row"> | |
<div class="col-md-12"> | |
<hr> | |
<!-- <?php | |
//----Querying Custom Post Types | |
// we created 2 ACF = job and text | |
// name - we took it from the default post_title (the default post title) | |
$reviews = getReviews(); | |
foreach ($reviews as $review) { | |
echo '</br>'; | |
echo $review['name']; | |
echo '</br>'; | |
echo $review['job']; | |
echo '</br>'; | |
echo $review['description']; | |
echo '</br>'; | |
echo '<img src="' . $review['img'] . '">'; | |
echo '</br>'; | |
echo $review['text']; | |
echo '</br>'; | |
} | |
?> --> | |
</div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment