You get a bonus - 1 coin for daily activity. Now you have 1 coin

WORDPRESS 9. Using Else, Post ID, Link Title.

Lecture



This lesson reveals the concepts of the following options, which you can add to each post of your blog: else, Post ID, value link title. Although they are not mandatory, all three are present in almost every one of the free themes, because you never know for what purpose people will use these themes.

First, do not forget to enable Denwer .

Step 1:

Type the following code under <? Php endwhile; ?> .

?
one
2
3
four
<?php else : ?>
<div class = "post" >
<h2><?php _e( 'Not Found' ); ?></h2> ); ?></h2>
</div>

Here it is: (do not forget about indents for proper code organization)

  WORDPRESS 9. Using Else, Post ID, Link Title.

Save it, right away you will not notice any changes - in order to understand what you just typed, we need to return to lesson number 6 - Cycle.

Here is the loop:

?
one
2
3
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?> (have_posts()) : the_post(); ?>
<?php endwhile ; ?> ; ?>
<?php endif ; ?> ; ?>

First, if (have_posts ()) checks if there are any posts on your blog. Secondly, while (have_posts ()) performs the the_post () function to call your posts for as long as they are . The else option implies alternative actions that will be performed if there are no posts in your blog. Imagine that you put while () and endwhile; between if () and else: That is why the <? Php else:?> Code should follow <? Php endwhile; ?> .

Now that you know what else means, what will you say to WordPress when you don't have any posts or when it can't find any posts? You will tell the system to display the message Not Found . This can be any message - look at different sites in search of examples.

The message itself and the code for Not Found are surrounded by the <h2> and </ h2> tags. This is also optional. You can simply use:

?
one
2
3
<div class = "post" >
Not Found
</div>

What are <div class = "post"> and </ div> for ? Well, you don't want the error message to hang in the void, right? You entered all data entered by tags <div class = ”post”> and </ div> . Here is the same thing - although the error message is not content in essence, it is also text, just like the record.

Step 2:

Add id = ”post - <? Php the_ID (); ?> " To <div class =" post ">

  WORDPRESS 9. Using Else, Post ID, Link Title.

Save the file and refresh the browser. Now go to View> Page Source or Source . You will see that now each post has a number, or ID. The the_ID () function simply calls the ID of each entry.

Why use it? In case of need to alter the look of any post to fit your needs. Later, when you use the style.css file to style the posts, all posts will look the same. And with a unique ID assigned to each post, you can easily select a separate post and make it different from the rest. Without an ID, you will not have a way to differentiate posts in the style.css file.

So how can you assign both class and id to the same div or invisible block? DIV is a tag, class is an attribute. id is not an attribute. Each tag can have several attributes, including the DIV tag can have both class and id . ( Note : id is an xhtml attribute. While the_ID () is a PHP function. Do not confuse - these are different concepts.)

Step 3:

Add a title = ”<? Php the_title (); ?> " To the link to the post title.

  WORDPRESS 9. Using Else, Post ID, Link Title.

The screenshot above is a partial screenshot so you can see where to put the code. Save it and refresh your browser. Look at the source code again. Find all links to post headers. If the link to the title of the Hello World post, then to the left of it should be the title = "Hello World" .

title = "" is another xhtml attribute for the <a> (link tag) tag. All that is in quotes is the link description. In this case, the title of each post is also a description of the link. This is why you used the PHP function the_title () again.

If you do not use the_title () as a value for title = "" , then all links to titles will have the same description. For example, instead of the_title () , you use title = "Click me" , then each link to the title will have a description of Click me .

Now go back to the web page. Hover over the title of the post - a pop-up window appears with a description. This is what you just did. It will be useful to add a description to the links, if other sites will crawl your blog. For example, Technorati.com, every time you post a note, WordPress notifies Technorati and other sites about updates to your blog. After this Technorati scans your blog, compiles a summary, which includes a description of the link to the title of the note.

In the next lesson we will look at the links to the Next and Previous pages.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Content Management Systems (CMS)

Terms: Content Management Systems (CMS)