简体   繁体   English

获取自定义帖子而不是默认的 Wordpress 帖子

[英]Fetch custom post instead of default Wordpress posts

I am using a plugin in which default wordpress posts are getting displayed on 'posts' tab, this code is fetching the posts, I am looking to fetch custom post type 'properties' instead of default wordpress post.我正在使用一个插件,其中默认的 wordpress 帖子显示在“帖子”选项卡上,此代码正在获取帖子,我希望获取自定义帖子类型“属性”而不是默认的 wordpress 帖子。

loop-post.php:循环post.php:

   <?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

$the_query = isset( $args['template_args']['the_query'] ) ? $args['template_args']['the_query'] : '';
$title = isset( $args['template_args']['title'] ) ? $args['template_args']['title'] : '';
?>
<h3><?php echo $title; ?></h3>
<div class="uwp-profile-item-block">
    <?php
    // The Loop
    if ($the_query && $the_query->have_posts()) {

        echo '<ul class="uwp-profile-item-ul">';
        while ($the_query->have_posts()) {
            $the_query->the_post();
            uwp_get_template('posts-post.php', $args);
        }
        echo '</ul>';

        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
        echo "<p>".sprintf( __( "No %s found.", 'userswp' ), $title )."</p>";
    }
    do_action('uwp_profile_pagination', $the_query->max_num_pages);
    ?>
</div>

posts-post.php:帖子post.php:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
global $post;
$user = uwp_get_displayed_user();
?>
<li class="uwp-profile-item-li uwp-profile-item-clearfix">
    <div class="uwp_generic_thumb_wrap">
        <a class="uwp-profile-item-img" href="<?php echo esc_url_raw( get_the_permalink() ); ?>">
            <?php
            if ( has_post_thumbnail() ) {
                $thumb_url = get_the_post_thumbnail_url( get_the_ID(), array( 80, 80 ) );
            } else {
                $thumb_url = uwp_get_default_thumb_uri();
            }
            ?>
            <img class="uwp-profile-item-alignleft uwp-profile-item-thumb"
                 src="<?php echo esc_url_raw( $thumb_url ); ?>">
        </a>
    </div>

    <h3 class="uwp-profile-item-title">
        <a href="<?php echo esc_url_raw( get_the_permalink() ); ?>"><?php echo get_the_title(); ?></a>
    </h3>
    <time class="uwp-profile-item-time published" datetime="<?php echo get_the_time( 'c' ); ?>">
        <?php echo get_the_date(); ?>
    </time>
    <div class="uwp-profile-item-summary">
        <?php
        do_action( 'uwp_before_profile_summary', get_the_ID(), $post->post_author, $post->post_type );
        $excerpt = strip_shortcodes( wp_trim_words( get_the_excerpt(), 15, '...' ) );
        echo esc_attr( $excerpt );
        do_action( 'uwp_after_profile_summary', get_the_ID(), $post->post_author, $post->post_type );
        ?>
    </div>
</li>

Can anyone help to fetch custom post type 'properties' posts only instead of default wordpress posts?任何人都可以帮助获取自定义帖子类型“属性”帖子而不是默认的 wordpress 帖子吗? Thanks in advance.提前致谢。

You can define new WP_Query and pass 'post_type' => 'properties' as arguments.您可以定义new WP_Query并传递'post_type' => 'properties'作为参数。 Try the below code.试试下面的代码。

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

$args = array(
    'post_type' => 'properties'
);

$properties = new WP_Query( $args );
$title = isset( $args['template_args']['title'] ) ? $args['template_args']['title'] : '';
?>
<h3><?php echo $title; ?></h3>
<div class="uwp-profile-item-block">
    <?php
    // The Loop
    if ($properties->have_posts()) {

        echo '<ul class="uwp-profile-item-ul">';
        while ($properties->have_posts()) {
            $properties->the_post();
            uwp_get_template('posts-post.php', $args);
        }
        echo '</ul>';

        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
        echo "<p>".sprintf( __( "No %s found.", 'userswp' ), $title )."</p>";
    }
    do_action('uwp_profile_pagination', $properties->max_num_pages);
    ?>
</div>

TO Fetch Custom Post You can use like below, update arguments as per your requirement:获取自定义帖子您可以使用如下所示,根据您的要求更新参数:

$args = array(  
        'post_type' => 'services',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby’ => 'title', 
        'order’ => 'ASC', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
        print the_title(); 
        the_excerpt(); 
    endwhile;

    wp_reset_postdata(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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