• Resolved anand454pandey

    (@anand454pandey)


    Hello Everyone
    My question is related to the Custom Post Type Calling on the Template.
    1.I have Registered a new post type on my function file and added some posts to that.
    2.I want to call that custom posts with there title and content on a template page.
    Here is My Code

    function register_mytestpage_post_types() {

    $labels=array(
    ‘name’ => ‘MyTestPages’,
    ‘singular_name’ => _x( ‘MyTestPage’, ‘post type singular name’, ‘your-plugin-textdomain’ ),
    ‘menu_name’ => _x( ‘MyTestPage’, ‘admin menu’, ‘your-plugin-textdomain’ ),
    ‘name_admin_bar’ => _x( ‘MyTestPage’, ‘add new on admin bar’, ‘your-plugin-textdomain’ ),
    ‘add_new’ => _x( ‘Add New’, ‘book’, ‘your-plugin-textdomain’ ),
    ‘add_new_item’ => __( ‘Add New MyTestPage’, ‘your-plugin-textdomain’ ),
    ‘new_item’ => __( ‘New MyTestPage’, ‘your-plugin-textdomain’ ),
    ‘edit_item’ => __( ‘Edit MyTestPage’, ‘your-plugin-textdomain’ ),
    ‘view_item’ => __( ‘View MyTestPage’, ‘your-plugin-textdomain’ ),
    ‘all_items’ => __( ‘All MyTestPage’, ‘your-plugin-textdomain’ ),
    ‘search_items’ => __( ‘Search MyTestPage’, ‘your-plugin-textdomain’ ),
    ‘parent_item_colon’ => __( ‘Parent MyTestPage:’, ‘your-plugin-textdomain’ ),
    ‘not_found’ => __( ‘No books found.’, ‘your-plugin-textdomain’ ),
    ‘not_found_in_trash’ => __( ‘No books found in Trash.’, ‘your-plugin-textdomain’ )
    );
    $arguments=array(
    ‘labels’=>$labels,
    ‘public’=>true
    );
    register_post_type( ‘Test_Page’,$arguments);
    }
    add_action(“init”,”register_mytestpage_post_types”);

    3.Now Calling it to my Template Page
    /**********************************************************
    <?php
    $argument=array(
    ‘posts_per_page’=>’3’,
    ‘post_type’=>’Test_Page’
    );
    $posts=new WP_Query($argument);
    ?>
    <?php while ( $posts->have_posts() ){ ?>
    <?php $posts->the_post(); ?>
    <div class=”col-md-4 animated growIn slow”>
    <div class=”text-center balack_columns”>
    <?php
    if (is_page(“Team of bio medical scientist”)) { ?>
    <div class=”icon_round i_heart”></div>
    <?php } elseif (is_page(“Robust App Development”)) { ?>
    <div class=”icon_round i_mobile”> </div>
    <?php } elseif (is_page(“Health, Fitness & Productivity”)) { ?>
    <div class=”icon_round i_run”> </div>
    <?php } else { echo ‘hello nothing found’; ?>
    <?php } ?>
    <h3 class=”header_red text-uppercase”><?php the_title(); ?></h3>
    <p><?php the_content(); ?></p>
    </div>
    </div>

    <?php } ?>
    <?php wp_reset_query(); ?>

    //******************************************************************

    Problem Description:

    If-else loop inside the while loop is reading only the last else part(echo part)
    I want to call the different div’s on each page title.
    Is There is Any Problem On Calling the Loop?

    Please suggest me what is the error

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter anand454pandey

    (@anand454pandey)

    can anybody tell me.

    Moderator bcworkz

    (@bcworkz)

    I think you loop is OK, it’s hard to read because you didn’t use backticks and there are no indents to see the nesting levels.

    Your main problem is using uppercase letters for the post type — only use lowercase and never spaces, follow the same rules as for slugs.

    I never trust is_page() with titles though it is acceptable. I recommend using IDs or slugs (aka names).

    Thread Starter anand454pandey

    (@anand454pandey)

    Thanks for your feedback #bcworkz
    Today i have found the solution the main problem was in calling the page
    i have used the get_the_title() method inside the conditions and the problem is solve.

    Next time i will write within correct format so that you can check the code respectively.
    thanks again cheers

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Calling the Custom post type on my template pages’ is closed to new replies.