How to add submenu Custom post type as a child of an existing Custom post type menu.
Tag: wordpress

di wordpress kita mengenal beberapa post_type default, yaitu post, pages, media, & menus. setiap post_type ini mempunyai jenis yang berbeda/ kegunaan khusus. pages adalah satu-satunya post_type default yang mempunyai feature page attribute. dengan fitur ini kita bisa membuat sebuah page konten memiliki hirarki, seperti tree.
fitur page attribut ini memungkinkan kita untuk membuat page parent & page child. dengan level depth unlimited, sesuai kebutuhan kita. tapi kadang level depth parent page ini menjadi kendala ketika kita ingin menampilkan semua child dr sebuah parent.
ada beberapa cara yang bisa di jadikan solusi untu menampilkan semua page child. pendekatan khusus yang akan kita coba kali ini dengan memanfaatkan pengulangan & array().
berikut adalah function untuk menampilkan semua child page wordpress dari sebuah page. adapun parameter yang dibutuhkan yaitu ID dari parent tersebut.
<?php function get_posts_children($parent_id){ $children = array(); // grab the posts children $posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'page', 'post_parent' => $parent_id, 'suppress_filters' => false, // optional if you want order by meta value //'meta_key' => 'start_time', //'orderby' => 'meta_value', //'order' => 'ASC' )); // now grab the grand children foreach( $posts as $child ){ // recursion!! hurrah $gchildren = get_posts_children($child->ID); // merge the grand children into the children array if( !empty($gchildren) ) { $children = array_merge($children, $gchildren); } } // merge in the direct descendants we found earlier $children = array_merge($children,$posts); return $children; }
function di atas bisa untuk custome post_type dengan argument standard untuk get_posts().

ini adalah tutorial singkat bagaimana membuat plugin WordPress yang sederhana. adapun plugin yang akan kita buat adalah menampilkan gambar di akhir setiap konten detail artikel.
adapun langkah langkahnya sebagai berikut:
- buat sebuah file, kita ambil contoh plugin contoh-plugin-wplokal.php
- buka file tersebut lalu masukkan header text seperti berikut:
<?php /* Plugin Name: Contoh Plugin WPLOKAL Plugin URI: https://www.wplokal.com/blog/membuat-plugin-wordpress-sederhana/ Description: Tutorial membuat plugin sederhana Version: 1.0.0 Author: Dodi Hidayatullah Author URI: https://www.langitstudio.com/ License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: wplokal Domain Path: /languages */
note: ketika membuat plugin, kita di haruskan menambahkan keterangan seperti di atas, agar plugin kita dapat di load/ di baca oleh wordpress.
untuk keterangan detail bisa merujuk ke sini.
3. setelah plugin kita dapat di baca oleh WordPress langkah selanjutnya adalah melakukan filter terhadap fungsi tampilan konten di WordPress. adapun fungsi yang di pakai untuk menampilkan konten adalah the_content();
berikut code nya:
/** * hook filter the_content() */ function wplokal_the_content_filter($content) { //ganti https://www.wplokal.com/demo/my-image.jpg dengan link image kalian $image = '<br><img src="https://www.wplokal.com/demo/my-image.jpg">'; return $content.$image; } add_filter( 'the_content', 'wplokal_the_content_filter' );
tambahkan code di atas pada contoh-plugin-wplokal.php. setelah itu simpan di folder
websitekamu.com/wp-content/plugins/ <- simpan di folder ini
lalu buka websitekamu.com/wp-admin/plugins.php
maka plugin yang kalian buat akan muncul seperti gambar di bawah ini :
[gambar 1]
setelah kalian aktifkan maka di setiap akhir halaman detail artikel akan muncul image seperti yang telah di masukkan di plugin. seperti gambar di bawah ini:
[gambar 2]
source code lengkap bisa di liat di github di bawah ini:
This file contains 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 | |
/* | |
Plugin Name: Contoh Plugin WPLOKAL | |
Plugin URI: https://www.wplokal.com/blog/membuat-plugin-wordpress-sederhana/ | |
Description: Tutorial membuat plugin sederhana | |
Version: 1.0.0 | |
Author: Dodi Hidayatullah | |
Author URI: <a href="https://www.wplokal.com">https://www.langitstudio.com/</a> | |
License: GPL2 | |
License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
Text Domain: wplokal | |
Domain Path: /languages | |
*/ | |
/** | |
* hook filter the_content() | |
*/ | |
function wplokal_the_content_filter($content) { | |
//ganti https://www.wplokal.com/demo/my-image.jpg dengan link image kalian | |
$image = '<br><img src="https://www.wplokal.com/demo/my-image.jpg">'; | |
return $content.$image; | |
} | |
add_filter( 'the_content', 'wplokal_the_content_filter' ); |