Posted in 2 years and 4 months ago

WordPress Code List

I collected the parameters I used while creating a WordPress theme on one page. This content contains codes suitable for general use and includes all the necessary general codes when creating a theme. To help others as a WordPress lover…

  Title parameter

<?php
    global $page, $paged;
    bloginfo( 'name' );
    wp_title( '»', true, 'left' );
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        echo " » $site_description";
    if ( $paged -->= 2 || $page >= 2 )
        echo ' » ' . sprintf( __( 'Page %s', 'twentyten' ), max( $paged, $page ) );
?>

Blog name

<?php bloginfo('name'); ?>

Blog address

<?php bloginfo('url'); ?>

CSS file

<?php bloginfo('stylesheet_url'); ?>

Theme path

<?php bloginfo('template_url'); ?>

Call header.php

<?php get_header(''); ?>

Call sidebar.php

<?php get_sidebar(''); ?>

Call footer.php

<?php get_footer(''); ?>

Tag ends

/*  before */
<?php wp_head(); ?>

/* to end  */
<?php wp_footer(); ?>

Page template

<?php /* Template Name: Page Name */ ?>

Theme style text

/*
Theme Name: 
Theme URI: 
Description: 
Author: 
Author URI: 
Version: 1.0
*/
Menu Contexts

Lists pages

<?php wp_list_pages('title_li='); ?>

Removing certain pages from the menu

<?php wp_list_pages('exclude=id&title_li='); ?>

Lists only pages with entered IDs

<?php wp_list_pages('include=id,id&title_li='); ?>

List categories

<?php wp_list_cats('title_li='); ?>

Category listing (custom menu)

<?php 
    $category_ids = get_all_category_ids();
    foreach($category_ids as $cat_id) {
        $cat_name = get_cat_name($cat_id);
        $category_url = get_category_link($cat_id);
        echo '<a href="'.$category_url.'"><li class="list-group-item">' . $cat_name . '</li></a>';
    }
?>
Content Contexts

While loop

<?php while(have_posts()) the_post(); ?>
    //content
<?php endwhile; ?>

Category name on category page

<?php printf( __( '%s', 'solopine' ), single_cat_title( '', false ) ); ?>

Category description on category page

<?php echo category_description(); ?>

Image of the post (Adding a class to the post Thumbnail)

<?php echo get_the_post_thumbnail( $post_id, 'thumbnail', array('class' => 'img-responsive img-circle post-image')); ?>

List the articles belonging to the category

<?php if (have_posts()) ?>
<?php $query = new WP_Query("cat=1&showposts=10"); while($query->have_posts()) $query->the_post();?> 
//content
<?php endwhile; ?>
<?php endif; ?>

Latest articles

<?php query_posts('showposts=10'); ?>
<?php while (have_posts()) the_post();?>
// content
<?php endwhile;?>
<?php wp_reset_query(); ?>

Post title

<?php the_title(''); ?>

Post address

<?php the_permalink('') ?>

Post category

<?php the_category(', ') ?>

Post category – HTML

<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ', '; } ?>

Text content

<?php the_content(''); ?>

Text content limit

<?php echo wp_trim_words( get_the_content(), 40, '...' );?>

Posting Time of the Article

<?php the_time('H:i:s'); ?>

Number of comments on the article

<?php comments_popup_link(__('No comment'), __('1 comment'), __('% comments'), '', __('Comments closed')); ?>

Post read count (requires WP-PostViews plugin)

<?php if(function_exists('the_views')) { the_views(); } ?>

Text ID

<?php the_ID(); ?>

Post edit link

<?php edit_post_link(); ?>

Comment template

<?php comments_template(); ?>
Author contexts

Author name

<?php the_author(''); ?>

About the author

<?php the_author_description(''); ?>

Author name

<?php the_author_firstname(''); ?>

Author surname

<?php the_author_lastname(''); ?>

Number of articles by the author

<?php the_author_posts('');?>

Author page link

<?php the_author_link('');?>

Author’s website

<?php the_author_url(''); ?>

Author’s e-mail address

<?php the_author_email('');?>
Extra contexts

Special menu

// Functions
add_action('init', 'theme_menus');
function theme_menus() {
    register_nav_menus(
        array(
        'anamenu' => __('Primary Menu'),
        'sagmenu' => __('Side Menu')
        )
    );
}
// Calling menu
wp_nav_menu( array( 'container_id' => 'submenu', 'theme_location' => 'anamenu','menu_class'=>'sf-menu','fallback_cb'=> 'fallbackmenu' ) );

Adding elements to the beginning or end of a custom menu

// Add the Homepage link to the beginning of the menu elements
function menu_ekleme($content) {
    $content = '<li class="current colordefault home_class"glt;<a href="index.html"glt;<i class="icon-home"glt;</iglt;</aglt;</liglt;' . $content;
    return $content;
}
add_filter('wp_nav_menu_anamenu_items', 'menu_ekleme');

Calling the photo uploaded specifically to the ID

function valuePhoto($photoID){
    $imageSrc = $photoID;
    if (is_numeric($photoID)) {
        $imageAttachment = wp_get_attachment_image_src($photoID,'full');
        $imageSrc = $imageAttachment[0];
        return $imageSrc;
    }
}

Uninstalling the WordPress toolbar (functions.php)

/* Disable the admin bar feature */
    add_filter( 'show_admin_bar', '__return_false' );
      
    /* Remove Admin Bar options from user profile */
    remove_action( 'personal_options', '_admin_bar_preferences' );

Calling custom field tag to page

<?php echo get_post_meta($post->ID, "ozel_alan_etiketi", true); ?>

Next post

<?php next_post_link('') ?>

Front text

<?php previous_post_link('') ?>

Featured image use

/* Inside the while loop */
    <?php if ( has_post_thumbnail() ) {the_post_thumbnail();} else {}?>
  
/* will be added in funtions.php */
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 125, 125 );

Quoting part – using the expert

/* Inside While Loop */
    <?php the_excerpt(''); ?>
  
    /* will be added in functions.php */
    function new_excerpt_length($length) {
    return 12;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

Search form

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>">
    <input class="searchbg" type="text" name="s" id="s" onfocus="if (this.value == 'Ara ...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Ara ...';}">
    <input class="button" type="button" value="">
</form>

Pagination (Pagenavi plugin)

<?php wp_pagenavi(); ?>
Tags:
HTML PHP
Published on 12 Aug, 2021
Last edit on 15 Dec, 2023