PHP script for Fibonacci number

PHP script for first 20 Fibonacci numbers
<?php
$count = 0 ;
$c1 = 0;
$c2 = 1;
echo $c1.” , “;
echo $c2.” , “;
while ($count < 20 )
{
$c3 = $c2 + $c1 ;
echo $c3.” , “;
$c1 = $c2 ;
$c2 = $c3 ;
$count = $count + 1;
}
?>

<?php
/* Find number odd or even */
$num = 32;
$even = ($num % 2 == 0);
$odd = ($num % 2 != 0);
?>

Advertisement

Get MAX post ID from from wp_posts table

<?php
/* Get Max value from wp_posts */
global $wpdb;
$table_name = $wpdb->prefix . “posts”;
$post_ids = $wpdb->get_results(“SELECT MAX(ID) as max_id FROM $table_name”);

/* stdclass object to array in PHP */
foreach ($post_ids as $value)
$array[] = $value->max_id;
?>

How to redirect attachment permalink in WordPress

Redirected to attachment parent page

function parent_page_attachment_page() {
if ( is_attachment() ) {
global $post;
if ( $post && $post->post_parent ) {
wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
exit;
} else {
wp_redirect( esc_url( home_url( '/' ) ), 301 );
exit;
}
}
}
add_action( 'template_redirect', 'parent_page_attachment_page' );