简体   繁体   中英

wordpress blog index is post page

I have a custom page named 'Journal', which I use as a blog index page for my wordpress website. I've run into a rather strange problem. When I enter <?php echo get_the_title(); ?> <?php echo get_the_title(); ?> or whatever in home.php, it returns the title of a post, instead of the page title 'Journal'. Is anyone familiar with this problem?

Thanks!

Try title with page id. Like this get_the_title($page_id);

This is the expected behavior for this page. When you set a page to be your "blog", you can't access the template tags for that page. Instead, the template tags are for the loop of the posts to be displayed on that page.

To get the title, you have to first get the id of that page, and then you can pass it to a function:

<?php
$page_for_blog = get_option( 'page_for_posts' );
$page_title = get_the_title( $page_for_blog );
?>

Now you can print the $page_title and you should see "Journal".

Updated with Advanced Custom Fields

Now that you have the Journal page's id ( $page_for_blog ), you can get your field values with:

$field_value = get_field( 'field_name', $page_for_blog );

Obviously, replace 'field_name' with whatever field you're trying to retrieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM