Mitt lilla kod arkiv

Lite kod som jag konstat återanvänder, har gjort detta arkiv mest för min egen del, att ha en plats att lagra koden på.

Fetch in Did Mount

componentDidMount() {
       fetch('/api/menu')
           .then(res => res.json())
           .then(json => {
                this.setState({
                    isLoaded: true,
                    items: json,
                })</div>
            });
}


render() {
    var {isLoaded, items} = this.state;

       function openlink(e, link){</div>
                e.preventDefault();</div>
                console.log(e, link);</div>
        }


        return (
                 <ul id="primary-menu" className="menu">
                        {items.map(item => (
                            <li key={item.ID} id="menu-item-{item.ID}" className="menu-item">
                                    <a aria-current="page" onClick={(e) => openlink(e, item.title)}>
                                       {item.title}
                                    </a>
                             </li>
                          ))};
                    </ul>
        )
    }
}

Custom post menu

Loops thru Custom post category and get the post name and link.
You need to crate a Advanced custom group category, in this case i named it ”Lang”


<?php
function consult_hacks_tax(){
    $args = array(
            'labels' => array(
                'name' => 'Langs',
                'singular_name' => 'Lang',
            ),
                'public' => true,
                'hierarchical' => false,
                'query_var'    => true,
                'show_in_nav_menus' => true,
                'show_admin_column' => true,  
            );

    register_taxonomy('langs', array('hacks'), $args);
}
add_action('init', 'consult_hacks_tax', 0);


?>

<?php
global $wpdb;
$r = $wpdb->get_results( "SELECT * FROM  `wp_term_taxonomy` 
                                   INNER JOIN `wp_terms` 
                                   ON wp_term_taxonomy.term_id = wp_terms.term_id  
                                   WHERE `taxonomy`='langs'", OBJECT );
foreach($r as $row){
echo '<li id="row'.$row->term_taxonomy_id.'" class="entry-title"><strong>'.$row->name .'</strong><BR>';

$rel = $wpdb->get_results("SELECT * FROM `wp_table` WHERE `term_taxonomy_id`=$row->term_id", OBJECT);
echo '<ul class="ul-class">';
$z = 0;
foreach($rel as $re){

$pos = $wpdb->get_results("SELECT * FROM `wp_posts` WHERE `ID`=$re->object_id", OBJECT);

foreach($pos as $p){

    echo '<li class="hacks-link">';
    echo '<a href="/pagetype/'. $p->post_name .'">'.$p->post_title ."</a>";
    echo "</li>";
    $z++;
    }
}
?>

Get custom posts loop

<?php $hacks= get_posts( array( 'post_type' => 'hacks',
                    'orderby' => 'post_title',
                    'order' => 'ASC',
));
foreach($hacks as $hack){ ?>

<li>
      <a href="/wordpress/hacks/<?php echo $hack->post_name;?>">
            <?php echo $hack->post_title; ?>
      </a>
</li>

<?php } ?>


DOM Ready

document.addEventListener("DOMContentLoaded", function(event) {
      
     var el = document.getElementById("savepost");
     el.addEventListener("click", function(){

      });
 
});

Post

$.post( "test.php", { name: "John", time: "2pm" })
    .done(function( data ) {
    alert( "Data Loaded: " + data );
});

Loop CI

Basic query’s in Codeigniter

Loop

$q = $this->db->query("");
if ($q->num_rows() > 0){
 $r = $q->row();
      echo $r->id;
}


$q = $this->db->get_where("", array('' => ''));
if ($q->num_rows() > 0) {
      foreach ($q->result() as $r){
           echo $r->id;
      }
}


$q = $this->db->get_where("", array('' => ''));
if ($q->num_rows() > 0){
 $r = $q->row();
 echo $r->id;
}