从类别数组中获取帖子

时间:2011-05-04 07:20:44

标签: wordpress post categories

我有一些特定类别的ID。我想一次循环这个类别和最后3个帖子。我尝试这个,但只从阵列中得到一个类别。

<?php
    $args = array(
    'cat'      => 48,43,49,46,47,44,51,50,42,
    'order'    => 'ASC',
    'showposts' => 3
        );
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

5 个答案:

答案 0 :(得分:3)

这段代码不起作用:'cat' => 48,43,49,46,47,44,51,50,42,

您必须使用数组'cat' => array(48,43,49,46,47,44,51,50,42),

答案 1 :(得分:3)

出于某种原因'猫'不起作用。我们用过

'category__in' => array( 2, 6 ),

它工作得很好。

完成的工作代码:

<?php
// -----------------------------
$args = array(
    'post_type' => 'post',
    'order' => 'ASC',
    'category__in' => array(2,6)
    );
$query = new WP_Query( $args );
?>

答案 2 :(得分:0)

您可以在要发布的类别中获取所有帖子。

query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );

您可以根据自己的期望找到帖子。

query_posts( array ( 'category_name' => 'carousel', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC' ) );

答案 3 :(得分:0)

根据您的代码。更新-

let PresentationalLocationView = (props: PresentationalLocationViewProps) => {
  return (
    <View>
      <Input
        isValid={props.isValid}
        placeholder={props.placeholder}
        label={props.label}
        value={props.location.searchText}
        required
        name={props.name}
        error={props.touched && props.error}
        onTouch={props.setFieldTouched}
        onChange={(name, text) => {
          props.onChange(text)
          if (!text) {
            props.updateLocationAutocompletePlace({})
            props.updateShopShouldHideResults(true)
            props.updateLocationListDisplayed(false)
          }
        }}
        deleteText={() => {
          props.onChange('')
          props.updateLocationAutocompletePlace({})
          props.updateShopShouldHideResults(true)
          props.updateLocationListDisplayed(false)
        }}
      />
      <View style={autocompleteStyle.listView(props)}>
        <FlatList
          keyboardShouldPersistTaps={true}
          data={props.autocompleteResults.predictions}
          renderItem={(listItemValue) =>
            renderAutocompleteItem(props, listItemValue)
          }
        />
      </View>
    </View>
  )
}

// eslint-disable-next-line flowtype/no-weak-types
class ShopView extends React.Component<LocationViewProps | any> {
  componentDidUpdate(prevProps) {
    if (!isEqual(this.props.locationValue, prevProps.locationValue)) {
      this.setFormLocation(this.props)
    }
  }

  setFormLocation = (props) => {
    if (props && props.setFieldValue) {
      props.setFieldValue(props.name, props.locationValue)
    }
  }

  render(): React.Node {
    return (
      // $FlowFixMe
      <PresentationalLocationView
        value={this.props.location.searchText}
        location={this.props.location}
        placeholder={this.props.placeholder}
        isValid={this.props.isValid}
        label={this.props.label}
        required
        name={this.props.name}
        touched={this.props.touched}
        error={this.props.error}
        setFieldTouched={this.props.setFieldTouched}
        onTouch={this.props.onTouch}
        updateLocationAutocompletePlace={
          this.props.updateLocationAutocompletePlace
        }
        updateShopShouldHideResults={this.props.updateShopShouldHideResults}
        updateLocationListDisplayed={this.props.updateLocationListDisplayed}
        autocompleteResults={this.props.location.searchResults}
        shouldHideResults={this.props.location.shouldHideResults}
        onListItemSelect={(shop) => {
          this.props.getLocationAutocompletePlaceDetails(shop.place_id)
          this.props.updateLocationAutocompleteSearchText(shop.description)
          this.props.updateShopShouldHideResults(true)
          this.props.updateLocationListDisplayed(false)
        }}
        onChange={(text) => onChange(this.props, text)}
      />
    )
  }
}

答案 4 :(得分:-1)

实际应该是: 'cat'=&gt; '48,43,49,46,47,44,51,50,42'