php 通过帖子名称而不是 id 获取帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12905763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:27:13  来源:igfitidea点击:

get post by post name instead of id

phpwordpress

提问by Juliver Galleto

Ok i have this code currently.

好的,我目前有这个代码。

<?php

$post_id = 266;
echo "<div id='widgets-wrapper3'><div id='marginwidgets' style='overflow: auto; max-    width: 100%; margin: 0 auto; border: none !important;'>";
$queried_post = get_post($post_id); 
echo "<div class='thewidgets'>";
echo substr($queried_post->post_content, 0, 500);
echo "<a href='".get_permalink( 26 )."' title='Read the whole post' class='rm'>Read     More</a>";
echo "</div>";

echo "</div></div>";

?>

As you can see to the above code, the routine is to get the post by ID, but my permalinks change into post name instead of post id for SEO purposes. How do I get the post by post name?

正如您在上面的代码中看到的那样,例程是通过 ID 获取帖子,但是出于 SEO 的目的,我的永久链接更改为帖子名称而不是帖子 ID。如何通过帖子名称获取帖子?

Hope someone here could figure it out. Thank you.

希望这里有人能弄清楚。谢谢你。

回答by Tom Auger

get_page_by_path()

get_page_by_path()

WordPress has a built-in function that might help, with a few words of caution.

WordPress 有一个内置功能,可能会有所帮助,但请注意几句话。

<?php get_page_by_path( $page_path, $output, $post_type ) ?>

<?php get_page_by_path( $page_path, $output, $post_type ) ?>

Here's the relevant Codex entry.

这是相关的 Codex 条目

To get a post, rather than a page, you just need to supply 'post' as the $post_typeargument, and usually OBJECT(with no quotes) as the $outputtype, like this:

要获取帖子而不是页面,您只需要提供 'post' 作为$post_type参数,通常OBJECT(不带引号)作为$output类型,如下所示:

<?php get_page_by_path( 'my_post_slug', OBJECT, 'post' ) ?>

<?php get_page_by_path( 'my_post_slug', OBJECT, 'post' ) ?>

Notethis function does not check the published or private status of the matched post. This is great if the item you're looking for is and attachment, but can be problematic for posts and pages (ie drafts, private posts etc.)

请注意,此功能不会检查匹配帖子的已发布或私人状态。如果您要查找的项目是附件,这很好,但对于帖子和页面(即草稿、私人帖子等)可能会有问题

Noteif it isa page you're looking for, and that page is hierarchical (ie: it has a parent), then you need to supply the entire path, that is: 'parent_page_slug/my_page_slug'.

请注意,如果它您要查找的页面,并且该页面是分层的(即:它有一个父页面),那么您需要提供整个路径,即:'parent_page_slug/my_page_slug'。

WP_Query / get_posts()

WP_Query / get_posts()

If either of these are a problem for you, then you should consider just using the WP_Queryclass to get your post by name:

如果其中任何一个对您来说都是问题,那么您应该考虑仅使用WP_Query该类来获取您的帖子name

$found_post = null;

if ( $posts = get_posts( array( 
    'name' => 'my_post_slug', 
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 1
) ) ) $found_post = $posts[0];

// Now, we can do something with $found_post
if ( ! is_null( $found_post ) ){
    // do something with the post...
}

回答by Knase

function get_post_by_name($post_name, $output = OBJECT) {
    global $wpdb;
        $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type='post'", $post_name ));
        if ( $post )
            return get_post($post, $output);

    return null;
}

Something this.

这东西。