Last active
November 11, 2022 08:23
-
-
Save uruly/bbf5f56d972426bc8d91266ff5e64471 to your computer and use it in GitHub Desktop.
Make WordPress custom post title an external link
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 /* | |
https://ドメイン名/archives/external-link | |
で表示される画面(パーマリンク設定による) | |
*/ ?> | |
<?php get_header(); ?> | |
<div class="main-wrap"> | |
<div class="main-content single-page card"> | |
<?php | |
if(have_posts()): | |
while ( have_posts() ) : the_post();?> | |
<?php get_template_part('content', 'archive-external-link'); ?> | |
<?php endwhile;?> | |
<?php wp_reset_postdata(); ?> | |
<?php endif; ?> | |
</div> | |
<?php get_sidebar(); ?> | |
</div> | |
<?php get_footer(); ?> |
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 //リンク設定があれば外部へリンク | |
$linknourl = get_post_meta( $post->ID, 'linkurl', true ); | |
if ( !empty( $linknourl ) ) : ?> | |
<?php | |
//htmlを除去 | |
$linknourl = htmlspecialchars($linknourl,ENT_QUOTES,'UTF-8'); | |
?> | |
<a href="<?php echo $linknourl; ?>"> | |
<?php else : ?> | |
<a href="<?php the_permalink(); ?>" > | |
<?php endif; ?> | |
<?php the_title() ;?> | |
</a> |
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 | |
/* カスタム投稿タイプを登録 */ | |
function external_link_post_type() { | |
$post_name = '外部リンク'; | |
$label = 'external-link'; | |
register_post_type( | |
$label, //投稿タイプ名(識別子) | |
array( | |
'label' => $post_name, | |
'labels' => array( | |
'add_new_item' => $post_name . 'を追加', | |
'edit_item' => $post_name . 'を編集', | |
'view_item' => $post_name . 'を表示', | |
'search_items' => $post_name . 'を検索', | |
), | |
'public' => true, // 管理画面に表示しサイト上にも表示する | |
'hierarchicla' => false, // コンテンツを階層構造にするかどうか(投稿記事と同様に時系列に) | |
'has_archive' => true, // trueにすると投稿した記事のアーカイブページを生成 | |
'supports' => array( // 記事編集画面に表示する項目を配列で指定することができる | |
'title', // タイトル | |
'editor', // 本文(の編集機能) | |
'thumbnail', // アイキャッチ画像 | |
'custom-fields', // カスタムフィールド | |
'excerpt' // 抜粋 | |
), | |
'menu_position' => 5 //「投稿」の下に追加 | |
) | |
); | |
} | |
add_action('init', 'external_link_post_type'); | |
//リンク先URLを追加するカスタムフィールド作成 | |
add_action('admin_menu', 'add_linkurl'); | |
add_action('save_post', 'save_linkurl'); | |
function add_linkurl() { | |
if (function_exists('add_linkurl')) { | |
add_meta_box('linkurl1', 'リンク先URL', 'insert_linkurl', 'post', 'normal', 'high'); | |
add_meta_box('linkurl1', 'リンク先URL', 'insert_linkurl', 'external-link', 'normal', 'high'); | |
} | |
} | |
function insert_linkurl(){ | |
global $post; | |
wp_nonce_field(wp_create_nonce(__FILE__), 'my_nonce'); | |
echo '<label class="hidden" for="linkurl">リンク先URL</label><input type="text" name="linkurl" size="60" value="'.esc_html(get_post_meta($post->ID, 'linkurl', true)).'" />'; | |
echo '<p>リンクしたいURLを入力します。</p>'; | |
} | |
function save_linkurl($post_id){ | |
$my_nonce = isset($_POST['my_nonce']) ? $_POST['my_nonce'] : null; | |
if(!wp_verify_nonce($my_nonce, wp_create_nonce(__FILE__))) { | |
return $post_id; | |
} | |
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } | |
if(!current_user_can('edit_post', $post_id)) { return $post_id; } | |
$data = $_POST['linkurl']; | |
if(get_post_meta($post_id, 'linkurl') == ""){ | |
add_post_meta($post_id, 'linkurl', $data, true); | |
}elseif($data != get_post_meta($post_id, 'linkurl', true)){ | |
update_post_meta($post_id, 'linkurl', $data); | |
}elseif($data == ""){ | |
delete_post_meta($post_id, 'linkurl', get_post_meta($post_id, 'linkurl', true)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment