Skip to content

Instantly share code, notes, and snippets.

@kachi
kachi / oneliners.js
Created April 1, 2019 16:11 — forked from mikowl/oneliners.js
👑 Awesome one-liners you might find useful while coding.
// By @coderitual
// https://twitter.com/coderitual/status/1112297299307384833
// Remove any duplicates from an array of primitives.
const unique = [...new Set(arr)]
// Sleep in async functions. Use: await sleep(2000).
const sleep = (ms) => (new Promise(resolve => setTimeout(resolve, ms)));
// Type this in your code to break chrome debugger in that line.
<?php
//https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
?>
<?php
add_filter('page_attributes_dropdown_pages_args', 'add_private_draft');
function add_private_draft($args) {
$args['post_status'] = 'publish,private,draft';
return $args;
}
?>
<?php
//ここからfunctions.php
function return_latest_id($cat_id=null) {
global $wpdb;
if(empty($cat_id)) {
// カスタムポストタイプ(foo、bar、hogeと仮定)を含む最新記事idの取得。
$row = $wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE ( post_type = 'post' OR post_type = 'foo' OR post_type = 'bar' OR post_type = 'hoge' ) AND post_status = 'publish' ORDER BY post_date DESC");
} else {
// カテゴリを指定した最新記事idの取得
<?php
// 指定したIDの子孫を階層でリスト表示
$parent = ここに親のID;
$args=array(
'child_of' => $parent
);
$pages = get_pages($args);
if ($pages) {
$pageids = array();
foreach ($pages as $page) {
@kachi
kachi / gist:1752836d249e45a32642
Created March 1, 2016 05:34
WordPressで投稿タイトル画からの場合の処理
<?php $title = get_the_title(); if (strlen($title) == 0)//タイトルが未入力かをチェック { ?>
<h2>タイトルがない場合の処理</h2>
<?php } else { ?>
<!--ある場合は普通に表示する-->
<h2><?php the_title(); ?></h2>
<?php } ?>
-----------------------------------------------------------
<?php //ループ内に書く
$previous_post = get_previous_post();
$next_post = get_next_post();
$prev_value = get_post_meta( $previous_post->ID, 'CUSTOM_FIELD', $single = true); //CUSTOM_FIELDが表示したいカスタムフィールドの名前
$next_value = get_post_meta( $next_post->ID, 'CUSTOM_FIELD', $single = true);
?>
<?php if ( $prev_value != '' ) : ?>
@kachi
kachi / 記事公開前にアラートを出す
Last active August 29, 2015 13:56
記事公開前にアラートを出す
$c_message = '記事を公開します。宜しいでしょうか?';
function confirm_publish(){
// JavaScriptを管理画面フッターに挿入する
global $c_message;
echo '<script type="text/javascript"><!--
var publish = document.getElementById("publish");
if (publish !== null) publish.onclick = function(){
return confirm("'.$c_message.'");
/// WPBeginner's YouTube Share Overlay Buttons
function add_share_buttons($atts) {
// ショーートコードでYoutubeのIDを取得
extract( shortcode_atts( array(
'video' => ''
), $atts ) );
// 動画を表示
@kachi
kachi / gist:5659859
Last active December 17, 2015 19:19
classを追加
<?php
add_filter ( 'wp_tag_cloud', 'tag_cloud_current_tag_highlight' );
function tag_cloud_current_tag_highlight( $return ) {
$post_tags = array();
if(is_single()) {
global $post;
$post_tags = get_the_terms($post->ID,'post_tag');
}
if(is_tag()) {
$tags = explode( '+', get_query_var('tag') );