WordPressに関する情報や技術紹介です

Funzione che restituisce l'id degli ultimi articoli (è possibile specificare categoria)

È una funzione che recupera l'id degli ultimi articoli. E 'anche possibile specificare da id categoria.

Vorrei ottenere l'ultima id della categoria, e stanno indagando WordPress forum ho trovato una alla grande. Questo è stato usato per farvi sapere che l'aggiunta di una piccola mano restituirà gli ultimi articoli nell'articolo di tutti, però.

Penso possa essere utile per definire il rammollimento del file functions.php tema le seguenti funzioni.
Vi è una domanda ... non lo so.

function return_latest_id($cat_id=null) {
	global $wpdb;

	if(empty($cat_id)) {
		// 最新記事idの取得
		$row = $wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC");
	} else {
		// カテゴリを指定した最新記事idの取得
		$cat_id = intval($cat_id);
		$row = $wpdb->get_row("SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->term_relationships r ON p.ID=r.object_id WHERE p.post_type = 'post' AND p.post_status = 'publish' AND r.term_taxonomy_id = '$cat_id' ORDER BY p.post_date DESC");
	}
	return !empty( $row ) ? $row->ID : '0';
}

以下のように呼び出せば最新の記事idを取得できます。
カテゴリidを指定すればそのカテゴリ内の最新記事idを返してくれます。

// 最新記事idの取得
$latest_id = return_latest_id();

// カテゴリを指定して最新記事idを取得
$latest_id = return_latest_id(3);
タグ
, , ,
トラックバックURL

Leave a Reply

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

ページTOPに戻る